#!/bin/sh
#
# I wrote this shell script long ago to be able to master CDs from modules
# in Un*x using gmodplay.  -Ben

CMD=$(basename $0)

if [ x = x$1 ]
then
	echo "
Usage: $CMD <module>
for example \"$CMD funky.xm\" would create funky.xm.wav
"
	exit
fi

sox -V 2>&1 | grep sox >/dev/null 2>&1
if [ $? -ne 0 ]
then
	echo "
FATAL: I can't find sox in your path.  Is it installed?
"
	exit
fi

WAVDIR=$(dirname $1)
WAVNAME=$(basename $1|tr 'A-Z' 'a-z').wav

echo -e "$1 -> $WAVDIR/$WAVNAME\c"


echo -e " [step 1]\c"
gmodplay -n -w $1 >/dev/null 2>&1
if [ $? -ne 0 ]
then
	echo "
FATAL: gmodplay returned an error.
"
	exit
fi

echo -e " [step 2]\c"
chmod a+r output.raw
sox -t raw -r 44100 -s -w -c 2 output.raw $WAVDIR/$WAVNAME >/dev/null 2>&1
if [ $? -ne 0 ]
then
	echo "
FATAL: sox returned an error.
You may need to remove output.raw
"
	exit
fi

echo -e " [step 3]\c"
rm -f output.raw

echo " Finished!"
