#!/bin/sh

#
# Wrapper script for running amSynth
# used to perform installation on first run
#
#			http://amsynthe.sourceforge.net
#			(c) 2003 Nick Dowell <nixx@nixx.org.uk>


#
# these should be modified at compile time to reflect installation dir
#
PREFIX=/usr
BINDIR=$PREFIX/lib/amsynth
DATADIR=$PREFIX/share/amsSnth


INST=no
EDFAIL=no

#
# copy default config file if needed, and launch editor to edit it
#
if test -f ~/.amSynthrc; then
INST=no
else
echo ""
echo "This appears to be your first time with amSynth, welcome!"
echo "*********************************************************"
echo ""
echo "copying default configuration file to ~/.amSynthrc"
echo "launching editor now, please set config file up for your system"
cp $DATADIR/rc ~/.amSynthrc
gedit 1>/dev/null 2>/dev/null 3>/dev/null ~/.amSynthrc ||\
kwrite 1>/dev/null 2>/dev/null 3>/dev/null ~/.amSynthrc ||\
nedit 1>/dev/null 2>/dev/null 3>/dev/null ~/.amSynthrc ||\
xedit 1>/dev/null 2>/dev/null 3>/dev/null ~/.amSynthrc ||\
EDFAIL=yes
fi

#
# quit if we tried to launch an editor and it failed
#
if test $EDFAIL = "yes"; then
echo "couldnt launch an editor, please edit config file by hand before next run"
exit
fi

#
# copy default controller setup if required
#
if test -f ~/.amSynthControllersrc; then
	INST=no
else
	echo "Copying default MIDI controller config to ~/.amSynthControllersrc"
	cp $DATADIR/Controllersrc ~/.amSynthControllersrc
fi

#
# copy default bank if required
#
if test -f ~/.amSynth.presets; then
	INST=no
else
	echo "copying default preset bank to ~/.amSynth.presets"
	echo "installation complete, launching amSynth..."
	echo ""
	cp $DATADIR/presets ~/.amSynth.presets
fi

#
# run the binary
#
$BINDIR/amSynth.bin $*

#
# check error code for errors
#
case $? in
130)
	exit
	;;
0)
	exit
	;;
60)
	echo
	echo "**** There was an error connecting to the JACK server! ****"
	echo
	echo "please make sure you have the jack server running!"
	echo
	exit
	;;
255)
	echo
	echo "**** There was an error starting the audio system ****"
	echo
	echo "please check your config in ~/.amSynthrc"
	echo
	exit
	;;
*)
	echo
	echo "********** there was an error! **********"
	echo
	echo "please check your config in ~/.amSynthrc"
	echo
	exit
	;;
esac
