#!/bin/sh
# Copyright (C) 2009 - 2012 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.
#

function echolog {
echo "`date +'%m/%d/%Y %H:%M:%S'` - $1"
}

if [ $# -ne 1 ]
then
    echo "Usage: post-rip-process flacdir"
    exit 1
fi

flacdir="$1"
mp3dir=`echo $flacdir | sed -e "s;/flac/;/mp3/;" -`
m4adir=`echo $flacdir | sed -e "s;/flac/;/m4a/;" -`

#echo "FLAC dir is $flacdir"
#echo "mp3dir dir is $mp3dir"
#echo "m4a dir is $m4adir"

############################################################
# Step 1 - Get cover art for the CD we just ripped.
#
############################################################

echolog "Getting cover art for $flacdir" 
/opt/vortexbox/covergrabber.php "$flacdir"

if [ -f "$flacdir/cover.jpg" ]; then
   #echo "copying $flacdir/cover.jpg to $flacdir/folder.jpg"
   cp -a "$flacdir/cover.jpg" "$flacdir/folder.jpg"
fi

############################################################
# Step 2 - Resync the music database in SqueeseBox server
#          and Sonos.
#
############################################################

echolog "Rescaning music folder to add $flacdir to your collection."
/usr/bin/php /opt/vortexbox/rescan.php

############################################################
# Step 3 - Mirror to mp3 and/or ALAC based on user 
#          selection from the web GUI
#
############################################################

# Wait for other mirroring process to finish
while [ -f /etc/vortexbox/mirror.running ]; do 
	sleep 30
done

# Block new mirroring process from starting
touch /etc/vortexbox/mirror.running

# Mirror to ALAC
if [ -f /etc/vortexbox/autoaac ]; then
	#echo "Mirroring FLAC files in $flacdir to m4a in $m4adir"

	if [ ! -d "$m4adir" ]; then
      mkdir -p "$m4adir"
	fi
	if [ -f "$flacdir/cover.jpg" ]; then
      cp -a "$flacdir/cover.jpg" "$m4adir/cover.jpg"
      cp -a "$flacdir/cover.jpg" "$m4adir/folder.jpg"
	fi

	echolog "Mirror of FLAC files in $flacdir to m4a in $m4adir started."
	/usr/local/sbin/flac2m4a.sh "$flacdir" "$m4adir" >> /var/log/mirror.log
	echolog "Mirror of FLAC files in $flacdir to m4a in $m4adir completed."
fi	

# Mirror to mp3
if [ -f /etc/vortexbox/automp3 ]; then
	#echo "Mirroring FLAC files in $flacdir to mp3 in $mp3dir"

	if [ ! -d "$mp3dir" ]; then
      mkdir -p "$mp3dir"
	fi
	if [ -f "$flacdir/cover.jpg" ]; then
      cp -a "$flacdir/cover.jpg" "$mp3dir/cover.jpg"
      cp -a "$flacdir/cover.jpg" "$mp3dir/folder.jpg"
	fi

	LAMEOPT=`cat /etc/vortexbox/lameopt.txt`
	lameargs="--noreplaygain -h --nohist --quiet $LAMEOPT"

	if [ ! -f /etc/vortexbox/covermp3 ]; then
      mirrorargs="--nocoverart"
	else
	  mirrorargs=
	fi
    
	#echo "Invoke flac2mp3.pl $mirrorargs --lameargs=$lameargs $flacdir $mp3dir"
	echolog "Mirror of FLAC files in $flacdir to mp3 in $mp3dir started."
	/usr/local/sbin/flac2mp3.pl $mirrorargs --lameargs="$lameargs" "$flacdir" "$mp3dir"
	echolog "Mirror of FLAC files in $flacdir to mp3 in $mp3dir completed."
fi	

rm /etc/vortexbox/mirror.running