#!/bin/bash
#
# Sad little script, huh?
#
# The main compatability issue is going to be the sounddriver version. We need
# to look for some version of ALSA, and create an include file that has a
# suitable define statement for the version. Then we get rid of the rather
# unhelpful ADLIB definition in the make calls.
#

buildheaders() {
	echo "#ifndef ADLIB" > slab/include/slabaudio.h
	echo "#define ADLIB $1" >> slab/include/slabaudio.h
	echo "#endif" >> slab/include/slabaudio.h

	echo "#ifndef ADLIB" > bristol/include/bristolaudio.h
	echo "#define ADLIB $1" >> bristol/include/bristolaudio.h
	echo "#endif" >> bristol/include/bristolaudio.h

	if [ $1 == -1 ]; then
		cp /dev/null slab/Makefile.autodefs
		cp /dev/null bristol/Makefile.autodefs
	else
		echo "LIBASOUND=-lasound" > slab/Makefile.autodefs
		echo "LIBASOUND=-lasound" > bristol/Makefile.autodefs
	fi
}

if [ $1 ]; then
	if [ $1 = "-noalsa" ]; then
		buildheaders -1
	fi
elif [ -f /usr/include/alsa/asoundlib.h ]; then
	buildheaders 2
elif [ -f /usr/include/sys/asoundlib.h ]; then
	buildheaders 1
elif [ -f /usr/include/alsa/asoundlib.h ]; then
	buildheaders 0
elif [ -f /usr/lib/libasound.so ]; then
	buildheaders 1
else
	buildheaders -1
fi

( cd brighton ; make )
#( cd slab/libslabcontrol ; make)
#( cd slab/libslabc ; make)
( cd slab/libslabaudio ; make)
( cd bristol ; make )

cp bristol/bristol/bristolengine ../bin
cp bristol/brightongui/bristol ../bin
cp bristol/libbristol/lib* ../lib
cp bristol/libbristolmidi/lib* ../lib
cp brighton/brighton/lib* ../lib
cp brighton/libbrightonX11/lib* ../lib
cp slab/libslabaudio/lib* ../lib
#cp slab/libslabc/lib* ../lib

echo
echo "You may want to make bin/bristolengine suid root. This will allow for config"
echo "of SCHED_FIFO for low latency audio operations...."
echo

