#!/bin/bash
##############################################################################
#
# Copyright (C) 2014 - 2016 x-odos GmbH
#
##############################################################################

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"

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

echolog "Getting cover art for $flacdir" 
/opt/x-stream/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/x-stream/rescan.php

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

/opt/x-stream/auto_mirror_aiff.sh "$flacdir"
/opt/x-stream/auto_mirror_alac.sh "$flacdir"
/opt/x-stream/auto_mirror_mp3.sh "$flacdir"