#!/bin/sh

VERSION=1.50

if test -n "$CC"; then
    CC=$CC
    echo "Forcing compiler to be $CC"
else
    CC="gcc"
fi

ARCHFLAGS=""
if test -n "$CFLAGS"; then
    ARCHFLAGS="$ARCHFLAGS $CFLAGS"
    echo "Adding $ARCHFLAGS to ARCHFLAGS"
fi

ARCHLIBS=""
if test -n "$LDFLAGS"; then
    ARCHLIBS="$ARCHLIBS $LDFLAGS"
    echo "Adding $ARCHLIBS to ARCHLIBS"
fi

user="no"
packageprefix=""
usexmms="auto"
useuade123="yes"
useuadecore="yes"
xmmsplugindir=""
debug_flags=""

for opt in "$@" ; do
	case $opt in
	--prefix=*)
		prefix=`echo $opt | sed -n 's/--prefix=\(.*\)/\1/p'`
		;;
	--package-prefix=*)
		packageprefix=`echo $opt | sed -n 's/--package-prefix=\(.*\)/\1/p'`
		;;

        --bindir=*)
                bindir=`echo $opt | sed -n 's/--bindir=\(.*\)/\1/p'`
                ;;
	--docdir=*)
		docdir=`echo $opt | sed -n 's/--docdir=\(.*\)/\1/p'`
		;;
	--includedir=*)
		includedir=`echo $opt | sed -n 's/--includedir=\(.*\)/\1/p'`
		;;
	--libdir=*)
		libdir=`echo $opt | sed -n 's/--libdir=\(.*\)/\1/p'`
		;;
	--make=*)
	        MAKE=`echo $opt | sed -n 's/--make=\(.*\)/\1/p'`
		;;
	--mandir=*)
		mandir=`echo $opt | sed -n 's/--mandir=\(.*\)/\1/p'`
		;;
	--sharedir=*)
		sharedir=`echo $opt | sed -n 's/--sharedir=\(.*\)/\1/p'`
		;;
	--pkg-config=*)
	        PKG_CONFIG=`echo $opt | sed -n 's/--pkg-config=\(.*\)/\1/p'`
		;;
	--xmms-config=*)
	        XMMS_CONFIG=`echo $opt | sed -n 's/--xmms-config=\(.*\)/\1/p'`
		;;
	--xmms-plugin-dir=*)
		xmmsplugindir=`echo $opt | sed -n 's/--xmms-plugin-dir=\(.*\)/\1/p'`
		;;

	--without-uadecore)
		useuadecore="no"
		;;

	--with-uade123)
		useuade123="yes"
		;;
	--without-uade123)
		useuade123="no"
		;;

	--with-xmms)
		usexmms="yes"
		;;
	--without-xmms)
		usexmms="no"
		;;

        --user)
		user="yes"
		;;
	--debug)
		debug_flags="-g"
		;;
	--help)
		echo
		echo "Installation control:"
                echo " --user                 Install uade to user's home directory. Do not run"
		echo "                        'make install' as root!"
                echo " --prefix=path          Install program under 'path'"
	        echo " --package-prefix=path  File installation prefix (for package maintainers)"
		echo " --make=exe             Use 'exe' as the make command"
		echo " --bindir=dir           Install executables into this directory"
		echo " --docdir=dir           Install documentation into this directory"
		echo " --includedir=dir       Add this directory to compilation include path"
		echo " --mandir=dir           Install man page to this directory"
		echo " --libdir=dir           Add this dircetory to linking path"
		echo " --sharedir=dir         Install data files into this directory"
		echo
		echo "Frontend options:"
		echo " --without-uadecore     Do not compile uadecore. This is useful for"
		echo "                        distribution makers who want to compile new frontends"
		echo "                        without re-compiling the emulator binary."
		echo " --with-uade123         Compile uade123 (auto detect)"
		echo " --without-uade123      Do not compile uade123"
		echo " --with-xmms            Compile an XMMS plugin (auto detect)"
		echo " --without-xmms         Do not compile an XMMS plugin"
		echo " --xmms-config=exe      Use 'exe' as xmms-config"
		echo
		echo "Library control:"
		echo " --pkg-config=exe       Use 'exe' as pkg-config executable"
		echo
		exit
		;;
	--x-libraries=*)
		;;
	--x-includes=*)
		;;
	*)
		echo "ignoring option $opt"
		;;
	esac
done

if test -z "$PKG_CONFIG" ; then
    PKG_CONFIG="pkg-config"
fi
"$PKG_CONFIG" --version 2> /dev/null > /dev/null || PKG_CONFIG=""

if test -z "$XMMS_CONFIG" ; then
    XMMS_CONFIG="xmms-config"
fi

if test "$usexmms" = "auto" ; then
    if test -x "`which $XMMS_CONFIG`" ; then
	usexmms="yes"
	XMMSFLAGS="`$XMMS_CONFIG --cflags`"
	XMMSLIBS="`$XMMS_CONFIG --libs`"
    else
	usexmms="no"
	echo "Couldn't find xmms-config -> not compiling xmms-plugin!"
    fi
fi

# set kernel type (such as AmigaOS) with environment variable $UADEKERNEL
OS="$UADEKERNEL"
if test -z "$OS" ; then
    OS="`uname`"
fi

UNIXSHELL="yes"
UADE123NAME="uade123"
UADECORENAME="uadecore"

if test -n "$OS" && test "$OS" = "MorphOS"; then
    echo
    echo "Configuring for MorphOS / AmigaOS."
    ARCHFLAGS="$ARCHFLAGS -noixemul"
    usexmms="no"
elif test -n "$OS" && test "$OS" = "AmigaOS"; then
    echo
    echo "Configuring for AmigaOS 4."
    ARCHFLAGS="$ARCHFLAGS"
    usexmms="no"
elif test -n "$OS" && test "$OS" = "Darwin"; then
    echo
    echo "Configuring for MacOSX"
    ARCHFLAGS="$ARCHFLAGS -no-cpp-precomp"
fi

grep -i cygwin 2>/dev/null >/dev/null <<EOF
$OS
EOF
if test "$?" = "0" ; then
    OS="Cygwin"
    UADE123NAME="uade123.exe"
    UADECORENAME="uadecore.exe"
fi

SOUNDSOURCE="sd-sound-generic.c"
SOUNDHEADER="sd-sound-generic.h"
echo "#include \"$SOUNDSOURCE\"" > src/sd-sound.c;
echo "#include \"$SOUNDHEADER\"" > src/sd-sound.h;

MACHINE="`uname -m`"
if test "$MACHINE" = "parisc"; then
    ARCHFLAGS="$ARCHFLAGS -ffunction-sections"
fi

INSTALLTEST="`which ginstall 2>/dev/null`"

if test ! -x "$INSTALLTEST"; then
 if test -n "$OS" && test "$OS" = "SunOS"; then
  echo
  echo "Warning: ginstall not found, install might not work."
  echo "If you do have ginstall make sure it is in your path."
  echo
  INSTALLTEST="`which install`"
 else
  INSTALLTEST="`which install`"
 fi
fi

if test -z "$MAKE" ; then
    if test -n "$OS" && test "$OS" = "MorphOS"; then
	MAKE="make"
    else
	MAKE="`which gmake`"
	if test ! -x "$MAKE"; then
	    MAKE="`which make`"
	fi
	if test ! -x "$MAKE"; then
	    echo FATAL: cannot find make
	    exit 1
	fi
    fi
fi

if test "$user" = "no"; then
    # global installation
    if test -z "$prefix"; then
	prefix="/usr/local"
    fi
    uadedatadir="$prefix/share/uade2"
    uadelibdir="$prefix/lib/uade2"
    if test -z "$bindir"; then
	bindir="$prefix/bin"
    fi
    if test "$usexmms" = "yes" ; then
	xmmsplugindir="`xmms-config --input-plugin-dir`"
    fi
    if test -z "$docdir"; then
	docdir="$prefix/share/doc/uade-$VERSION"
    fi
    if test -z "$mandir"; then
	mandir="$prefix/share/man/man1"
    fi
else
    # user installation
    if test -z "$prefix"; then
	prefix="$HOME/.uade2"
    fi
    uadedatadir="$prefix"
    uadelibdir="$prefix"
    if test -z "$bindir"; then
	bindir="$prefix"
    fi
    if test "$usexmms" = "yes" ; then
	xmmsplugindir="$HOME/.xmms/Plugins/Input"
    fi
    if test -z "$docdir"; then
	docdir="$prefix/uade-$VERSION"
    fi
    if test -z "$mandir"; then
	mandir="$prefix/man/man1"
    fi
fi

if test -z "$libdir" ; then
    libdir="$prefix/lib"
fi
if test -z "$includedir" ; then
    includedir="$prefix/include"
fi
if test -n "$sharedir"; then
    uadedatadir="$sharedir"
fi

cd compat
echo "#include <string.h>" > ../src/include/strlrep.h
echo "" > ../src/strlrep.c
$CC -o strltest strltest.c 2>/dev/null
if test "$?" != "0" ; then
    cp strlrep.c ../src/
    cp strlrep.h ../src/include/
fi
cd ..

if test "$useuade123" = "yes" ; then
    AOFLAGS="`$PKG_CONFIG --cflags ao`"
    if test "$?" != "0"; then
	echo ""
	echo "Can not compile uade123. Please install libao (including development kit)."
	echo ""
	useuade123="no"
    fi
    AOLIBS="`$PKG_CONFIG --libs ao`"
fi

echo ""
echo "UADE and frontends will be installed to : $bindir"
echo "Data directory will be                  : $uadedatadir"
echo "Documentation directory will be         : $docdir"
echo "Man directory will be                   : $mandir"
echo "Installer that will be used             : $INSTALLTEST"
echo "Make that will be used during the build : $MAKE"
echo "uade123                                 : $useuade123"
if test "$useuade123" = "yes" ; then
    echo "uade123 sound output                    : AO"
fi
echo "XMMS plugin                             : $usexmms"
if test "$usexmms" = "yes" ; then
    echo "XMMS plugin directory                   : $xmmsplugindir"
fi
echo

cat > src/ossupport.c <<EOF
#include "unixsupport.c"
EOF
cat > src/include/ossupport.h <<EOF
#include <unixsupport.h>
EOF
OSSUPPORTMODULES="unixsupport.c include/unixsupport.h"

conffile=src/include/uadeconfig.h
echo > "$conffile"
if test "$user" = "yes" ; then
    echo "#define UADE_CONFIG_USER_MODE (1)" >> "$conffile"
else
    echo "#define UADE_CONFIG_USER_MODE (0)" >> "$conffile"
fi
echo "#define UADE_CONFIG_BASE_DIR \"$uadedatadir\"" >> "$conffile"
echo "#define UADE_CONFIG_UADE_CORE \"$uadelibdir/$UADECORENAME\"" >> "$conffile"
test -e /dev/urandom && echo "#define UADE_CONFIG_HAVE_URANDOM" >> "$conffile"
echo "#define UADE_VERSION \"$VERSION\"" >> "$conffile"

if test "$useuadecore" = "yes" ; then
    uadecorerule="uadecore"
    uadecoreinstallrule="uadecoreinstall"
else
    uadecorerule=""
    uadecoreinstallrule=""
fi
if test "$useuade123" = "yes" ; then
    uade123rule="uade123"
    uade123installrule="uade123install"
else
    uade123rule=""
    uade123installrule=""
fi
if test "$usexmms" = "yes" ; then
    xmmspluginrule="xmmsplugin"
    xmmsplugininstallrule="xmmsplugininstall"
else
    xmmspluginrule=""
    xmmsplugininstallrule=""
fi

sed -e "s|{MAKE}|$MAKE|g" \
    -e "s|{DATADIR}|$uadedatadir|g" \
    -e "s|{BINDIR}|$bindir|g" \
    -e "s|{DOCDIR}|$docdir|g" \
    -e "s|{MANDIR}|$mandir|g" \
    -e "s|{LIBDIR}|$uadelibdir|g" \
    -e "s|{PACKAGEPREFIX}|$packageprefix|g" \
    -e "s|{UADECORENAME}|$UADECORENAME|g" \
    -e "s|{UADECORE}|$uadecorerule|g" \
    -e "s|{UADECOREINSTALL}|$uadecoreinstallrule|g" \
    -e "s|{UADE123NAME}|$UADE123NAME|g" \
    -e "s|{UADE123}|$uade123rule|g" \
    -e "s|{UADE123INSTALL}|$uade123installrule|g" \
    -e "s|{XMMSPLUGIN}|$xmmspluginrule|g" \
    -e "s|{XMMSPLUGININSTALL}|$xmmsplugininstallrule|g" \
    -e "s|{XMMSPLUGINDIR}|$xmmsplugindir|g" \
    Makefile.in > Makefile

for file in src/Makefile.in src/frontends/*/Makefile.in ; do
    dst="`echo $file |sed -e "s|\.in||"`"
    sed -e "s|{ARCHFLAGS}|$ARCHFLAGS|g" \
	-e "s|{ARCHLIBS}|$ARCHLIBS|g" \
	-e "s|{AOFLAGS}|$AOFLAGS|g" \
	-e "s|{AOLIBS}|$AOLIBS|g" \
	-e "s|{XMMSFLAGS}|$XMMSFLAGS|g" \
	-e "s|{XMMSLIBS}|$XMMSLIBS|g" \
	-e "s|{CC}|$CC|g" \
	-e "s|{SOUNDSOURCE}|$SOUNDSOURCE|g" \
	-e "s|{SOUNDHEADER}|$SOUNDHEADER|g" \
	-e "s|{DEBUGFLAGS}|$debug_flags|g" \
	-e "s|{OSSUPPORTMODULES}|$OSSUPPORTMODULES|g" \
	"$file" > "$dst"
done
