#!/bin/bash --
# Conversion program from FLAC provided on the command line to mp3
# Version 1.2, Licenced under the GPL v2

if file --help > /dev/null && grep --help > /dev/null && flac -h > /dev/null && lame --help > /dev/null && metaflac --help > /dev/null
    then echo "Core tool detection test passed."
else
    echo "Failed to find all essential tools.  Exiting."
    exit 10
fi

if file "$1" | grep FLAC\ audio\ bitstream\ data > /dev/null
    then echo -e "Convert starting for '$1'."
    flac -c -d "$1" | lame --preset standard - "$1.mp3" --id3v2-only --ta "`metaflac --show-tag=artist \"$1\" | sed s/^.......//`" --tl "`metaflac --show-tag=album \"$1\" | sed s/^......//`" --tt "`metaflac --show-tag=title \"$1\" | sed s/^......//`" --ty "`metaflac --show-tag=date \"$1\" | sed s/^.....//`" --tn "`metaflac --show-tag=tracknumber \"$1\" | sed s/^............//`"
    rename "s/\.flac\.mp3$/.mp3/" "$1.mp3" && rm "$1"
else
 echo "File not found or not FLAC. Exiting."
 exit 20
fi


