#!/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: mirror-cd-to-m4a
#
# Usage: mirror-cd-to-m4a flacdir
#
# Get coverart for the FLAC album in flacdir, if necessary,
# then mirror the FLAC files in flacdir to m4a.
#
# Author: Ron Olsen <ronolsen@comcast.net>
#
# Date: Fri May 20 2011
# Version 1.0
#
##############################################################################

# Mirror specified FLAC directory to m4a


if [ $# -ne 1 ]
then
    echo "Usage: mirror-cd-to-m4a flacdir"
    exit 1
fi

if [ ! -d "$1" ]
then
    echo "Error: $1 is not a directory"
    exit 2
fi

#echo "/usr/local/sbin/mirror-cd-to-m4a $1"

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

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

# Get cover art for FLAC dir

#echo "Getting cover art for $flacdir"
if [ ! -f "$flacdir/cover.jpg" ]
then
    /opt/vortexbox/covergrabber.php "$flacdir"
fi

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

#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 "$m4adir/cover.jpg" "$m4adir/folder.jpg"
fi

echo "$(/bin/date): Mirror of FLAC files in $flacdir to m4a in $m4adir started."
/usr/local/sbin/flac2m4a.sh "$flacdir" "$m4adir" 
echo "$(/bin/date): Mirror of FLAC files in $flacdir to m4a in $m4adir completed."
