#!/bin/sh

# help message function
help_message()
{
    echo "usage: configure [options]"
    echo "available options:"
    echo
    echo "    -prefix dir            install into directory"
    echo "    -search dir            add search directory for headers/libraryes"
    echo
    echo "    -enable-xingmp3        enable XingMP3 decoder"
    echo "    -disable-xingmp3       disable XingMP3 decoder"
    echo
    echo "    -enable-mad            enabled MAD decoder (mp3)"
    echo "    -disable-mad           disable mad decoder"
    echo
    echo "    -enable-oggvorbis      enable OggVorbis decoder"
    echo "    -disable-oggvorbis     disable OggVorbis decoder"
    echo
    echo "    -enable-fftw           enable FFTW support"
    echo "    -disable-fftw          disable FFTW support"
    echo
    echo "    -help                  this help text"
    echo
    echo "NOTE: The XingMP3 and MAD decoders are mutually exclusive.  In other"
    echo "words, you can have one or the other, but not both."
    echo
}

find_qt3()
{
    # this check needs to be improved
    if [ '!' -z "$QTDIR" ]
    then
	# ahh good - the user has QTDIR set
	if [ -f "$QTDIR/.qmake.cache" -a -f "$QTDIR/bin/qmake" ]
	then
	    QMAKE=$QTDIR/bin/qmake
	    QMAKECACHE=$QTDIR/.qmake.cache
	    foundqt="yes"
	fi
    fi

    if [ "$foundqt" != "yes" ]
    then
	echo "Sorry, you should set QTDIR to a Qt 3 source tree."
	exit 1
    fi
}

# for Qt
foundqt="no"
QMAKE=
QMAKECACHE=

# installing/building
prefix="auto"
extra_search=

# features
madsupport="auto"
xingsupport="auto"
oggsupport="auto"
fftwsupport="auto"

# external libs
madlocation=
ogglocation=
fftwlocation=

# parse options
for option
do
    if test -n "${previous}"; then
	eval "${previous}=\$option"
        previous=
        continue;
    fi

    case "${option}" in
    -prefix)
        prefix=
        previous=prefix
        ;;
    -search)
        previous=extra_search
        ;;

    -enable-*)
	feat=`echo ${option} | sed -e "s,-enable-,,"`
	case "${feat}" in
	xingmp3)
	    if [ "$madsupport" = "yes" ];
	    then
		echo
		echo "WARNING: MAD decoder support already enabled, disabling XingMP3."
		echo
		xingsupport=no
	    else
		xingsupport=yes
		madsupport=no
	    fi
	    ;;
	mad)
	    if [ "$xingsupport" = "yes" ];
	    then
		echo
		echo "WARNING: XingMP3 decoder support already enabled, disabling MAD."
		echo
		madsupport=no
	    else
		madsupport=yes
		xingsupport=no
	    fi
	    ;;
	oggvorbis)
	    oggsupport=yes
	    ;;
	fftw)
	    fftwsupport=yes
	    ;;
	*)
	    help_message
	    exit 1
	    ;;
	esac
	;;

    -disable-*)
	feat=`echo ${option} | sed -e "s,-disable-,,"`
	case "${feat}" in
	xingmp3)
	    xingsupport=no
	    ;;
	mad)
	    madsupport=no
	    ;;
	oggvorbis)
	    oggsupport=no
	    ;;
	fftw)
	    fftwsupport=no
	    ;;
	*)
	    help_message
	    exit 1
	    ;;
	esac
	;;

    -help)
        help_message
	exit 0
        ;;
    *)
        help_message
        exit 1
        ;;
    esac
done

# find Qt >= 3 (look for qmake and it's cache file)
find_qt3


# get install directory
if [ "$prefix" = "auto" -o "x$prefix" = "x" ]
then
    prefix=/usr/local
fi
INSTALLDIR=${prefix}


# detect various features
if [ "$xingsupport" != "yes" ]
then
    # only detect mad if xingsupport is auto or no (ie, the user didn't explicitly
    # enable xingmp3 support)
    if [ "$madsupport" != "no" ]
    then
	madsupport=no
	madsearch="${extra_search} /usr /usr/local"
	for dir in `echo ${madsearch}`
	do
	    if [ -f "${dir}/include/mad.h" -a -f "${dir}/lib/libmad.so.?" ]
	    then
		madlocation=${dir}
		madsupport=yes
		xingsupport=no
		break
	    elif [ -f "${dir}/include/mad.h" -a -f "${dir}/lib/libmad.a" ]
	    then
		madlocation=${dir}
		madsupport=yes
		xingsupport=no
		break 
	    fi
	done
    fi
fi

if [ "$xingsupport" = "auto" -a "$madsupport" != "yes" ]
then
    xingsupport=yes
    madsupport=no
fi

if [ "$oggsupport" != "no" ]
then
    oggsupport=no
    oggsearch="${extra_search} /usr /usr/local"
    for dir in `echo ${oggsearch}`
    do
	if [ -f "${dir}/include/vorbis/vorbisfile.h" -a -f "${dir}/lib/libogg.so.?" -a -f "${dir}/lib/libvorbis.so.?" -a -f "${dir}/lib/libvorbisfile.so.?" ]
	then
	    ogglocation=${dir}
	    oggsupport=yes
	    break
	elif [ -f "${dir}/include/vorbis/vorbisfile.h" -a -f "${dir}/lib/libogg.a" -a -f "${dir}/lib/libvorbis.a" -a -f "${dir}/lib/libvorbisfile.a" ]
	then
	    ogglocation=${dir}
	    oggsupport=yes
	    break 
	fi
    done
fi

if [ "$fftwsupport" != "no" ]
then
    fftwsupport=no
    fftwsearch="${extra_search} /usr /usr/local"
    for dir in `echo ${fftwsearch}`
    do 
	if [ -f "${dir}/include/rfftw.h" -a -f "${dir}/lib/librfftw.so.?" -a -f "${dir}/lib/libfftw.so.?" ]
	then
	    fftwlocation=${dir}
	    fftwsupport=yes
	    break
	elif [ -f "${dir}/include/rfftw.h" -a -f "${dir}/lib/librfftw.a" -a -f "${dir}/lib/libfftw.a" ]
	then
	    fftwlocation=${dir}
	    fftwsupport=yes
	    break
	fi
    done
fi

# copy the .qmake.cache file from where we found Qt
cat $QMAKECACHE | sed -e "s,dll,,g" > ./.qmake.cache

if [ "$xingsupport" = "yes" ]
then
    echo "CONFIG+=xingmp3" >> ./.qmake.cache
fi

if [ "$madsupport" = "yes" ]
then
    echo "CONFIG+=mad" >> ./.qmake.cache
    echo "INCLUDEPATH+=$madlocation/include" >> ./.qmake.cache
    echo "unix:LIBS+=-L$madlocation/lib" >> ./.qmake.cache
fi

if [ "$oggsupport" = "yes" ]
then
    echo "CONFIG+=oggvorbis" >> ./.qmake.cache
    echo "INCLUDEPATH+=$ogglocation/include" >> ./.qmake.cache
    echo "unix:LIBS+=-L$ogglocation/lib" >> ./.qmake.cache
fi

if [ "$fftwsupport" = "yes" ]
then
    echo "CONFIG+=fftw" >> ./.qmake.cache
    echo "INCLUDEPATH+=$fftwlocation/include" >> ./.qmake.cache
    echo "unix:LIBS+=-L$fftwlocation/lib" >> ./.qmake.cache
fi

echo "DEFINES+=QT_CLEAN_NAMESPACE" >> ./.qmake.cache
echo "MQ3_BINPATH=$prefix/bin" >> ./.qmake.cache
echo "MQ3_LIBPATH=$prefix/lib" >> ./.qmake.cache
echo "MQ3_INCPATH=$prefix/include/mq3" >> ./.qmake.cache
echo "MQ3_PLGPATH=$prefix/lib/mq3/plugins" >> ./.qmake.cache
echo "MQ3_ICNPATH=$prefix/share/mq3/icons" >> ./.qmake.cache

echo "#ifndef CONFIG_H" > ./config.h
echo "#  define INSTALLDIR \"$prefix\"" >> ./config.h
echo "#endif" >> ./config.h

# report what we found
echo "Configuration for mq3:"
echo
echo "   install prefix - $prefix"
echo
echo "            qmake - $QMAKE"
echo "       cache file - $QMAKECACHE"
echo
echo "  XingMP3 Support - $xingsupport"
echo "      MAD Support - $madsupport"
echo "OggVorbis Support - $oggsupport"
echo "     FFTW Support - $fftwsupport"
echo
echo -n "Creating Makefiles, please wait."

for i in `find ./ -name "*.pro"`
do
    p=`pwd`
    d=`dirname $i`
    f=`basename $i`
    ( cd $d ; $QMAKE $f )
    echo -n "."
done

echo ""
echo "Done, type \"make\" to build mq3."
echo ""

rm -f config.status
cat > config.status <<EOF
#!/bin/sh

${0} $* \$@

EOF

chmod u+x config.status
