#!/usr/bin/sh
#  midi2mp3 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.
#
#  midi2mp3 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.
#
#  You should have received a copy of the GNU General Public License
#  along with midi2mp3.  If not, see <http://www.gnu.org/licenses/>.
#

# Make sure prerequisites are installed
for i in timiditya lamea
do 
  which $i >/dev/null 2>&1 || { 
    kdialog --error "$i was not found. You must install it to convert MIDI to MP3."
    exit 1
  }
done

# help print
EXPECTED_ARGS=2
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
   echo "Usage: `basename $0` from.mid to.mp3"
   exit $E_BADARGS
fi

format=`file -b --mime-type "$1"`
if [ $format != "audio/midi" ]; then
	echo "Unsupported format : $format"
	exit 1
else
	timidity -idqq -Ow "$1" -o - | lame --quiet - "$2"
fi
