#!/bin/sh
##############################################################################
# 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.
##############################################################################

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"

# 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-images "$1" > /dev/null 2>&1
        #echo "Adding $2 to $1"
        echo "Replacing cover art in $1"
        /usr/bin/eyeD3 --to-v2.3 --no-tagging-time-frame --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-images "$1" > /dev/null 2>&1
    /usr/bin/eyeD3 --to-v2.3 --no-tagging-time-frame --add-image="$2":FRONT_COVER "$1" > /dev/null 2>&1
fi
rm -rf $TMPDIR
