#!/bin/bash
#

engine=1
quiet=0
verbose=0

# ALSA audio doesn't work on my SBLive, fall back to OSS
audioflags="-audio oss"

# ALSA sequencer support is always a nice feature
midiflags="-midi alsa -seq"



checkdriver() 
{
    if test ! -d /proc/asound/seq; then
        echo "ALSA sequencer not installed, falling back to OSS midi"
        midiflags="-midi oss"
    fi
}

# ALSA audio doesn't work on my SBLive, fall back to OSS
audioflags="-audio oss"

# ALSA sequencer support is always a nice feature
midiflags="-midi alsa -seq"



checkdriver() 
{
    if test ! -d /proc/asound/seq; then
        echo "ALSA sequencer not installed, falling back to OSS midi"
        midiflags="-midi oss"
    fi
}

showhelp()
{
	if [ $verbose = 1 ]; then
		# let the bristol gui give a verbose help text.
		engine=0
	else
		echo
		echo "startBristol [-explorer|-mini|-hammond|-b3|-dx|-juno|-prophet|-mixer|-vox|-pro10|-rhodes] [-oss|-alsa] [-libtest] [[-verbose|-v] -help|-h]"
		echo
		echo "Press 'q' in any synth window to exit application"
		echo "bristol -v -h for more verbose help output"
		echo "You may want to give the /usr/bin/bristolengine suid root permissions"
		echo
		exit 0
	fi
}

for index in $*; do
	if [ $index = "-engine" ]; then
		engine=0
	fi
	if [ $index = "-v" ]; then
		verbose=1
	fi
	if [ $index = "--v" ]; then
		verbose=1
	fi
	if [ $index = "-verbose" ]; then
		verbose=1
	fi
	if [ $index = "--verbose" ]; then
		verbose=1
	fi
	if [ $index = "-h" ]; then
		showhelp
	fi
	if [ $index = "--h" ]; then
		showhelp
	fi
	if [ $index = "-help" ]; then
		showhelp
	fi
	if [ $index = "--help" ]; then
		showhelp
	fi
	if [ $index = "-quiet" ]; then
		quiet=1
	fi
	if [ $index = "-q" ]; then
		quiet=1
	fi
done


# Set the needed environment variables

if [ -z "$BRISTOL" ]; then
	declare -x BRISTOL=/usr/share/bristol
fi

if [ ! -d $BRISTOL ]; then
    echo "incomplete installation of bristol"
fi

declare -x BRIGHTON=$BRISTOL
declare -x LD_LIBRARY_PATH=/usr/lib/bristol


if [ ${EUID} -ne 0 ]; then
    echo "	You may want to make bristolengine a suid-root executable"
fi

checkdriver

#
# bristolengine has flags for -oss or -alsa drivers, -preload buffers of
# -bufsize bytes (not samples):
#	bristolengine -preload 4 -bufsize 1024 [-oss]
#
if [ $engine = 1 ]; then
	if [ $quiet = 0 ]; then
		bristolengine -preload 4 -bufsize 1024 $audioflags $midiflags $* &
	else
		bristolengine -preload 4 -bufsize 1024 $audioflags $midiflags $* > /dev/null 2>&1 &
	fi
fi
if [ $quiet = 0 ]; then
	bristol.real $* -engine
else
	bristol.real $* -engine > /dev/null 2>&1
fi
