#!/usr/bin/zsh -f

if ! (type id3ed &>/dev/null) ; then
    echo "Need id3ed installed"
    exit 1
fi
if ! (type lame &>/dev/null) ; then
    echo "Need lame installed"
    exit 1
fi

if [ $# -eq 0 ] ; then
    echo "Usage: ${0##*/} file.mp3"
    exit 2
fi

if ! test -f $1 ; then
    echo "Input file '$1' does not exist"
    exit 3
fi

if ! ( file $1 | grep -q MP3) ; then
    echo "'$1' does not seem to be a MP3 file"
    exit 4
fi

id3ed -i "$1" | tail -8 | head -4 | while read line ; do
  echo ${line#*: }
done | tr '\n' '^' | IFS="^" read song artist album year dummy

lame --mp3input -b 128 -h --tt $song --ta $artist --tl $album --ty $year "$1" "new_$1"
