#! /bin/bash -e

#####################################################################
#                                                                   #
#               Search where Faust is installed and defines   		#
#				$FAUSTLIB and $FAUSTINC so that we can use			#
#				-L$FAUSTLIB and -I$FAUSTINC where needed.			#
#               (c) Grame, 2013                                    #
#                                                                   #
#####################################################################

#-------------------------------------------------------------------------------
# Search where Faust is installed. Store '.../share/faust/' in $FAUSTLIB
# and '.../include/(faust)' in $FAUSTINC 
#
# USAGE :
# add line << . faustpath >> to your script
# and use -L$FAUSTLIB and -I$FAUSTINC where needed

FAUSTLDDIR=$(faust -libdir); 
FAUSTLIB=$(faust -dspdir); 
FAUSTINC=$(faust -includedir); 
[ -n "$FAUSTARCH" ] || FAUSTARCH=$(faust -archdir); 

[ -n "$MAXSDK" ] || MAXSDK=/usr/local/include/c74support/
CSOUND_MACSDK=/Library/Frameworks/CsoundLib64.framework
# try and guess a good place for Pd header files. Taking the first entry in /Applications/Pd- and taking the folder containing m_pd.h
[ -n "$PUREDATA_MACSDK" ] || PUREDATA_MACSDK=`ls -d /Applications/Pd-* 2> /dev/null | head -n1 | xargs -I REPL find REPL -name m_pd.h -exec dirname {} \; | head -n1`
[ -n "$PUREDATA_LINUXSDK" ] || PUREDATA_LINUXSDK=`find /usr/include/pd /usr/include/libpd/ /usr/include/pdextended /usr/local/include/pd /usr/local/include/libpd -name m_pd.h -exec dirname {} \; 2>/dev/null| head -n1`
VSTSDK=""
[ -n "$SUPERCOLLIDER_HEADERS" ] || SUPERCOLLIDER_HEADERS=/usr/local/include/SuperCollider/

FPATH="$FAUST_INSTALL /usr/local /usr /opt /opt/local"; # <- where to search
if [ -z "$FAUSTLIB" -o -z "$FAUSTINC" ]; then 
for f in $FPATH; do
	if [ -e $f/share/faust ]; 	then FAUSTLIB=$f/share/faust;	fi
	if [ -e $f/include/faust ];	then FAUSTINC=$f/include/; fi
done
fi

for f in $FPATH; do
	if [ -e $f/lib/libmicrohttpd.a ]; 	then HTTPLIB=$f/share/faust;	fi
done

if [ -e /etc/faust/faustpath ]; then
 . /etc/faust/faustpath
fi
if [ -e ${HOME}/.faust/faustpath ]; then
 . ${HOME}/.faust/faustpath
fi

if [ -z "$FAUSTLIB" -o -z "$FAUSTINC" ]; then 
	echo "ERROR : $0 cannot find Faust directories (normally /usr/local/include/faust and /usr/local/share/faust)"; 
	exit 1;
fi
