#!/bin/bash
#
# 'lp2flac.sh'
#
# A script to rename and encode WAV files of "raw" LP recordings
# to FLAC. Metadata are taken from a playlist file.
#
# Arguments: switches (see below)
#
# -----------------------------------------------------------------
# History:
#   2008-08-14, JHa, first working version
# -----------------------------------------------------------------
# Copyright (c) 2008 Joerg Hau <joerg.hau(at)dplanet.ch>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# 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.
# -----------------------------------------------------------------

# set defaults
#
PLAYLIST=""
MODE=""

# -----------------------------------------------------------------
# subroutine to check for some required executables
# argument: (list of) programs to test for
# will exit with rc=1 if any program was not found
# -----------------------------------------------------------------
function checkfor()
{
for i in $*; do
    PROG=`which $i`
    if [ $? != 0 ]; then
        echo "'$i' command not found, exiting."
        exit 1
    fi
done
}

# check for some mandatory stuff
#
checkfor grep cut sed awk wc echo ls seq


# ------------------------------------------------------------
# subroutine to print usage mode
# ------------------------------------------------------------
function usage()
{
cat << eof
${0##*/} - a script to control the conversion of WAV audio files into FLAC format.

Copyright (c) 2008 Joerg Hau <joerg.hau(at)dplanet.ch>.

This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.

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.

Usage: ${0##*/} fileset1 [fileset2 ...]

The .wav files and their playlist file must have names according
to *.X.wav, where "X" is always a one-digit number indicating the
side (track) number.

Invoking "${0##*/}" with no options will do nothing.

eof
}

# --------------------------------------------------------------
# if script is called w/o remaining arguments, give instructions
# --------------------------------------------------------------
if [ -z $1 ] ; then
    usage
    exit 1
fi

# --------------------------------------------------------------
# run for all names given on the cmd line
# --------------------------------------------------------------
for FNAM in "$@"; do

    PLAYLIST=${FNAM}.playlist


    # --------------------------------------------------------------
    # check for existence of playlist file
    # --------------------------------------------------------------
    if [ ! -r $PLAYLIST ] ; then
        echo "Playlist file '$PLAYLIST' not found! Exiting."
        exit 2
    fi

    # --------------------------------------------------------------
    # parse playlist file and extract info
    # The format of this file is as follows (without the '#' character!):
    #
    # Artist	Al Stewart
    # Album	Year of the Cat
    # Year	1976
    # Genre	Rock
    # 01	Lord Grenville
    # 02	On the Border
    # 03	Midas Shadow
    # 04	Sand in Your Shoes
    # 05	If it doesn't come naturally, leave it
    # 06	Flying Sorcery
    # 07	Broadway Hotel
    # 08	One Stage Before
    # 09	Year of the Cat
    #
    # (todo: error checking ...)
    # I'm invoking sed instead of cut since the file may be composed
    # using whitespace instead of tab
    # --------------------------------------------------------------
    ARTIST=`grep "^Artist" $PLAYLIST | sed 's/^Artist[ \t]*//'`
    ALBUM=`grep "^Album" $PLAYLIST | sed 's/^Album[ \t]*//'`
    YEAR=`grep "^Year" $PLAYLIST | sed 's/^Year[ \t]*//'`
    GENRE=`grep "^Genre" $PLAYLIST | sed 's/^Genre[ \t]*//'`

    # --------------------------------------------------------------
    # obtain total number of titles
    # --------------------------------------------------------------
    NUMTITLES=`ls ${FNAM}.*.wav | wc -l`

    # --------------------------------------------------------------
    # now run the actual conversion
    # --------------------------------------------------------------
    for i in `seq 1 $NUMTITLES`; do
        flac --best --tag=Artist="$ARTIST" --tag=Album="$ALBUM" --tag=Date="$YEAR" \
            --tag=Title="Side ${i}" --tag=Genre="$GENRE" ${FNAM}.${i}.wav
    done

done    # end of the 'outer' loop

exit 0

