#!/bin/bash

# Laurie hAs Unfriendly, Rabid, Injurious Electronics...
# ... that won't play ogg files, so we have to convert them to mp3

version=0.1

function help {
    # copyright and summary of options
    : ## TODO
}

# parse options
while getopts "s:d:rpvh" opt ; do
    case $opt in
	s) source="$OPTARG" ;;
	d) dest="$OPTARG" ;;
	r) replace=0 ;;

        p) pretend=0 ;;
        v) verbose=0 ;;

        h) help ;;
    esac
done

source=${source:-.}
dest=${dest:-$source} ## TODO

function error { # This is called when any important command called by the
                 # script fails
    exit 1
}

function execute {
    # By using this function as a wrapper, we are able to work in normal, 
    # verbose, or don't-do-anything modes
    if [ $pretend ]; then
        echo "[not executed] $@"
        read -p "[ press return ]"
    elif [ $verbose ];then
        echo " + $@"
        bash -c "$@" || error
    else
        bash -c "$@" || error
    fi
}

# We need ogginfo, oggdec and lame
if OGGINFO="$(which ogginfo)" &&
    echo "ogginfo... $OGGINFO" &&
    OGGDEC="$(which oggdec)" &&
    echo "oggdec... $OGGDEC"&&
    LAME="$(which lame)" &&
    echo "lame... $LAME" ; then
    echo "Found needed tools, processing..."
else
    echo "You need ogginfo, oggdec and lame to run this script"
    exit 1
fi

function extract_tags {
    # extract tags from ogg file
    ogg_artist=$($OGGINFO "$@" | grep -i artist=)
    ogg_artist=$(echo $ogg_artist | sed s/artist=//i)

    ogg_album=$($OGGINFO "$@" | grep -i album=)
    ogg_album=$(echo $ogg_album | sed s/album=//i)
    
    ogg_title=$($OGGINFO "$@" | grep -i title=)
    ogg_title=$(echo $ogg_title | sed s/title=//i)
    
    ogg_tracknum=$($OGGINFO "$@" | grep -i tracknumber=)
    ogg_tracknum=$(echo $ogg_tracknum | sed s/tracknumber=//i)

    ogg_tags="${ogg_artist} / ${ogg_album} - ${ogg_tracknum} ${ogg_title}"
    echo $ogg_tags
}

function calc_timestamp {
    # generate a timestamp for the temp PCM file
    echo $(date +%Y%m%d%H%M%S%N)
}

function ogg_decode {
    # decode ogg file to PCM
    # remove ogg file if $replace is set
    echo "    Decoding..."
    execute "oggdec \"$1\" -o $2"
    if [ $replace ] ; then
        echo "    Deleting original file"
        execute "rm -f \"$1\""
    fi
}

function mp3_encode {
    # encode PCM to MP3
    # remove PCM file
    if [[ $ogg_artist != '' ]] ; then
        LAME_OPTS="--ta \"$ogg_artist\""
    fi
    if [[ $ogg_title != '' ]] ; then
        LAME_OPTS="$LAME_OPTS --tt \"$ogg_title\""
    fi
    if [[ $ogg_album != '' ]] ; then
        LAME_OPTS="$LAME_OPTS --tl \"$ogg_album\""
    fi
    if [[ $ogg_tracknum != '' ]] ; then
        LAME_OPTS="$LAME_OPTS --tn \"$ogg_tracknum\""
    fi
    execute "lame $LAME_OPTS \"$1\" \"$2\""

    execute "rm \"$1\""
}

function rec_dir {
    # enter $source if [ -d $source ]
    # enter directories inside $source
    # convert files in current dir
    cd "$@"
    for file in * ; do
        if [ -d "$file" ] ; then
            rec_dir "$file"
        elif file "$file" | grep Ogg ; then
            tmp_file=Laurie.sh-$(calc_timestamp)
            dst_file="${file%%.ogg}.mp3"
            extract_tags "$file"
            ogg_decode "$file" $tmp_file
            mp3_encode $tmp_file "$dst_file"
        fi
    done
    cd -
}

if [ -d "$source" ] ; then
    rec_dir "$source"
else
    echo "You specified a inexistant directory !"
    exit 1
fi
