#!/bin/bash
#

engine=1
quiet=0
verbose=0

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 "startBristol -v -h for more verbose help output"
		echo "You may want to give the 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

# Nokey: Don't override BRISTOL if it's not already set
if [ -z "$BRISTOL" ]; then
	declare -x BRISTOL=/usr/share/bristol
fi

# Nokey: Does the BRISTOL directory actually exist?
if [ ! -d $BRISTOL ]; then
	# Apparently not. Let's try to make a guess as to where it is.

	# Try to find out how we were started. If this script was in
	#  the PATH of the user, the $0 should have a leading '/'.
	#  This leading '/' would also be there if this script was
	#  executed as /full/path/name/startBristol
	STARTUP=$0
	# If this script was started as ./"*whatever*" then we need
	#  to strip off the './' in order to accurately determine
	#  the BRISTOL directory
	if [ "$(echo $STARTUP | cut -c 1-2)" = "./" ]; then
		STARTUP=$(echo $STARTUP | cut -c 3-)
	fi
	
	if [ "$(echo $STARTUP | cut -c 1)" = "/" ]; then
		BIN_DIR=$(dirname $STARTUP)
	else
		BIN_DIR=$(dirname $PWD/$STARTUP)
	fi

	declare -x BRISTOL=$(dirname $BIN_DIR)
	unset BIN_DIR STARTUP
fi

#
# If slabhome already exists, we should take it rather than this definition.
#
declare -x SLAB_HOME=$BRISTOL
declare -x BRIGHTON=$BRISTOL

declare -x LD_LIBRARY_PATH=/usr/lib/bristol:${BRISTOL}/lib

declare PATH=${PATH}:$BRISTOL/bin

# see if we should advise making the binary a suid root for FIFO scheduling
if [ ${EUID} -ne 0 ]; then
	if [ `ls -l ${BRISTOL}/bin/bristolengine | awk '{printf $3}'` != "root" ]; then
		echo "	You may want to make bristolengine a suid-root executable"
	elif [ ! -u ${BRISTOL}/bin/bristolengine ]; then
		echo "	You may want to make bristolengine a suid-root executable"
	fi
fi

#
# 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 $* &
	else
		bristolengine -preload 4 -bufsize 1024 $* > /dev/null 2>&1 &
	fi
fi
if [ $quiet = 0 ]; then
	bristol $* -engine
else
	bristol $* -engine > /dev/null 2>&1
fi
