#!/bin/bash
##############################################################################
# Copyright (C) 2008 - 2013 vortexbox.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Name: check-mp3-coverart
# Usage: check-mp3-coverart file.mp3 cover.jpg
#
# Script to check the cover art embedded in an mp3 file to see if it needs to be updated.
# The script extracts the cover art from file.mp3 (if any), and compares it to cover.jpg.

# If there is no cover art in file.mp3, cover.jpg is added.

# If the extracted cover art differs from cover.jpg, the cover art is deleted
# from file.mp3 and replaced by cover.jpg.

# If the extracted cover art is the same as cover.jpg, no changes are made to file.mp3.

# Author: Ron Olsen <ronolsen@comcast.net>
#
# Date: Sat May 7 2011
# Version: 1.0
#
# Date: Mon May 9 2011
# Version: 1.1
# Fix for file and directory names with embedded spaces.
#
# Date: Tues Jan 31 2012
# Version: 1.2
# * Use unique directory to store temp image files.
# * Remove all cover art images from mp3 files before adding new art.
#
# Date: Sat Apr 19 2014
# Version 1.3
# Fixes for VB 2.3
# eyeD3 options have changed in Fedora 20:
# Use --remove-all-images instead of --remove-images
# Delete --no-tagging-time-frame option which does not exist in F20 version of eyeD3
#
# Date: Sat Apr 26 2013
# Version 1.4
# Use --encoding=latin1 when adding/replacing images in mp3 files so cover art will appear in iTunes.
#
# Date: Sat Jul 5 2014
# Version 1.5
# Set and export LANG from /etc/locale.conf so eyeD3 will work on filenames with non-ASCII characters
#
##############################################################################

if [ $# -ne 2 ]
then
    echo "Usage: check-mp3-coverart file.mp3 cover.jpg"
    exit 1
fi

#echo "check-mp3-coverart invoked with args $1 and $2"

# Set and export LANG, which is not set when FLAC-to-mp3 mirror is invoked from VB GUI.
# This is necessary so eyeD3 will work on filenames with non-ASCII characters.
. /etc/locale.conf
export LANG

# Extract cover art (if any) from file.mp3 with output to /tmp/cover$$.
# If file.mp3 contains cover art embedded by the VB flac2mp3.pl script,
# then name of the extracted cover art will be /tmp/cover$$/FRONT_COVER.jpeg.

TMPDIR=/tmp/cover$$
rm -rf $TMPDIR
mkdir $TMPDIR
/usr/bin/eyeD3 --write-images=$TMPDIR "$1" > /dev/null 2>&1

if [ -f $TMPDIR/FRONT_COVER.jpeg ]
then
    #echo "$1 contains FRONT_COVER image"
    # Compare extracted cover art with cover.jpg
    if `cmp -s $TMPDIR/FRONT_COVER.jpeg "$2"`
    then
        #echo "extracted cover art is the same as $2"
        continue
    else
        #echo "extracted cover art differs from $2"
        # Remove cover art from mp3 file
        #echo "Removing cover art from $1"
        /usr/bin/eyeD3 --remove-all-images "$1" > /dev/null 2>&1
        #echo "Adding $2 to $1"
        echo "Replacing cover art in $1"
        /usr/bin/eyeD3 --encoding=latin1 --to-v2.3 --add-image="$2":FRONT_COVER "$1" > /dev/null 2>&1
    fi
else
    #echo "$1 does not contain cover art"
    #echo "Adding $2 to $1"
    echo "Adding cover art to $1"
    /usr/bin/eyeD3 --remove-all-images "$1" > /dev/null 2>&1
    /usr/bin/eyeD3 --encoding=latin1 --to-v2.3 --add-image="$2":FRONT_COVER "$1" > /dev/null 2>&1
fi
rm -rf $TMPDIR
