#!/bin/sh

VERSION=0.80

prefix=$HOME/.xmms
packageprefix=""
mymods="\$ENV{HOME}"
usexmms=yes
xmmsinputplugindir=automatic
SDL=no
interaction=no
debug_flags=""

for opt in "$@" ; do
	case $opt in
	--prefix=*)
		prefix=`echo $opt | sed -n 's/--prefix=\(.*\)/\1/p'`
		;;
        --binprefix=*)
                binprefix=`echo $opt | sed -n 's/--binprefix=\(.*\)/\1/p'`
                ;;
	--docprefix=*)
		docprefix=`echo $opt | sed -n 's/--docprefix=\(.*\)/\1/p'`
		;;
	--package-prefix=*)
		packageprefix=`echo $opt | sed -n 's/--package-prefix=\(.*\)/\1/p'`
		if test "$prefix" = "$HOME/.xmms"; then
			echo
			echo "with --package-prefix you must give a global installation prefix first..."
			echo "example: --prefix=/usr/local --package-prefix=/tmp/foobar"
			echo
			exit
		fi
		;;
	--input-plugin-dir=*)
		xmmsinputplugindir=`echo $opt | sed -n 's/--input-plugin-dir=\(.*\)/\1/p'`
		;;
	--mymods=*)
		mymods=`echo $opt | sed -n 's/--mymods=\(.*\)/\1/p'`
		mod=1
		;;
	--no-plugin)
		usexmms=no
		;;
	--with-sdl)
		SDL=yes
		;;
	--interaction)
		interaction=yes
		;;
	--no-interaction)
		interaction=nooo
		;;
	--debug)
		debug_flags="-g"
		;;
	--help)
		echo
		echo "Usage: --prefix=path if you don't want this installed in home dir."
	        echo "       --package-prefix=path file installation prefix (for package maintainers)"
		echo "       --no-plugin if you don't want to have an xmms plugin"
		echo "       --input-plugin-dir=path where path is the xmms input plugin"
		echo "                               directory"
		echo "       --mymods=path where path is the default mod directory for "
		echo "                     pwraps tk interface. Default path is homedir."
		echo "       --with-sdl use SDL rather than native audio for sound output."
		echo
		exit
		;;
	--x-libraries=*)
		;;
	--x-includes=*)
		;;
	*)
		;;
	esac
done

if test "$usexmms" = "yes"; then
if test -x "`which xmms-config`"; then
 echo > /dev/null
else
usexmms=no
echo "Couldn't find xmms-plugin => not compiling xmms-plugin!"
fi
fi

if [ -f src/sd-sound.c ];then
rm src/sd-sound.c
fi

if [ -f src/sd-sound.h ];then
rm src/sd-sound.h
fi

if [ -f osdep/uade-os.c ];then
rm osdep/uade-os.c
fi

if [ -f osdep/uade-os.h ];then
rm osdep/uade-os.h
fi

cp configure.h config.h
chmod u+w config.h

OS="`uname`"

UNIXSHELL=yes
SOUNDLIBS=""
ARCHFLAGS= 
ARCHLIBS=

if test -n "$OS" && test "$OS" = "SunOS"; then
    echo
    echo "Configuring for Solaris." 
    echo '#include "sd-sound-solaris.c"' > src/sd-sound.c;
    echo '#include "sd-sound-generic.h"' > src/sd-sound.h;
    echo '#include "uade-os-unix.c"' > osdep/uade-os.c;
    echo '#include "uade-os-unix.h"' > osdep/uade-os.h;
    SOLARIS=yes
elif test -n "$OS" && test "$OS" = "IRIX"; then
	echo
	echo "Configuring for IRIX."
	echo '#include "sd-sound-sgi.c"' > src/sd-sound.c;
	echo '#include "sd-sound-sgi.h"' > src/sd-sound.h;
	echo '#include "uade-os-unix.c"' > osdep/uade-os.c;
	echo '#include "uade-os-unix.h"' > osdep/uade-os.h;
	IRIX=yes
	SOUNDLIBS="$UADEEXTRALIBS -lgen -laudio"
elif test -n "$OS" && test "$OS" = "MorphOS"; then
	echo
	echo "Configuring for MorphOS / AmigaOS."
	echo '#include "sd-sound-ahi.c"' > src/sd-sound.c
	echo '#include "sd-sound-ahi.h"' > src/sd-sound.h
	echo '#include "uade-os-amiga.c"' > osdep/uade-os.c;
	echo '#include "uade-os-amiga.h"' > osdep/uade-os.h;
	ARCHFLAGS="$ARCHFLAGS -noixemul"
	UNIXSHELL=no
	usexmms=no
elif test -n "$OS" && test "$OS" = "Darwin"; then
	echo
	echo "Configuring for MacOSX"
	SDL=yes
	ARCHFLAGS="$ARCHFLAGS -no-cpp-precomp"
	ARCHLIBS="-lpoll"
	echo "#define HAVE_MACOSX" >> config.h
	echo '#include "uade-os-unix.c"' > osdep/uade-os.c;
	echo '#include "uade-os-unix.h"' > osdep/uade-os.h;
elif test -n "$OS" && test "$OS" = "Linux"; then
	if test "$interaction" != "nooo"; then
	    interaction=yes
	fi
	echo '#include "sd-sound-oss.c"' > src/sd-sound.c;
	echo '#include "sd-sound-generic.h"' > src/sd-sound.h;
	echo '#include "uade-os-unix.c"' > osdep/uade-os.c;
	echo '#include "uade-os-unix.h"' > osdep/uade-os.h;
else
	echo '#include "sd-sound-oss.c"' > src/sd-sound.c;
	echo '#include "sd-sound-generic.h"' > src/sd-sound.h;
	echo '#include "uade-os-unix.c"' > osdep/uade-os.c;
	echo '#include "uade-os-unix.h"' > osdep/uade-os.h;
fi


if test "$SDL" = "yes";then
SDLFLAGS=`sdl-config --cflags`
SDLLIBS=`sdl-config --libs`
echo '#include "sd-sound-sdl.c"' > src/sd-sound.c;
echo '#include "sd-sound-sdl.h"' > src/sd-sound.h;
else
SDLFLAGS=""
SDLLIBS=""
fi


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

PERLDIR="`which perl`"
INSTALLTEST="`which ginstall`"

if test -x "$INSTALLTEST";then
echo
elif test "$SOLARIS" = "yes";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
if [ -f uadeinstall ];then
rm uadeinstall
fi

MAKE="`which gmake`"
if test -x "$MAKE"; then
echo 
else
MAKE="`which make`"
fi
if test -n "$OS" && test "$OS" = "MorphOS"; then
MAKE=make
fi

if test "$prefix" = "$HOME/.xmms"; then
    uadedatadir="$prefix/uade"
    bindir="$prefix/uade"
    plugin="$HOME/.xmms/Plugins/Input"
    docdir="$prefix/uade/uade-$VERSION"
else
    uadedatadir="$prefix/share/uade"
    bindir="$prefix/bin"
    if test "$usexmms" = "yes"; then
	plugin=`xmms-config --input-plugin-dir`
    else
	plugin="$HOME/.xmms/Plugins/Input"
    fi
    docdir="$prefix/doc/uade-$VERSION"
fi

if test "$xmmsinputplugindir" != "automatic"; then
plugin=$xmmsinputplugindir
fi

libdir="$prefix/lib"
includedir="$prefix/include"
sharedir="$prefix/share"

LHA="`which lha`"
if test -x "$LHA"; then
LHAREPLY="yes"
else
echo
echo "LHA archiver not found: UADE won't take advantage of lha archives"
echo "until 'lha' executable is placed into command execution path."
echo "You do not need to reinstall and/or recompile UADE if you later add"
echo "lha to command execution path.."
echo
LHAREPLY="no"
fi

cd osdep
echo "" > strl.c
gcc -o strltest strltest.c 2>/dev/null || cp strlrep.c strl.c
cd ..

echo "UADE and frontends will be installed to : $bindir"
echo "Data directory will be                  : $uadedatadir"
echo "XMMS plugin directory will be           : $plugin"
echo "Documentation directory will be         : $docdir"
if test "$mod" = "1"; then
echo "Default module dir for pwrap.pl will be : $mymods"
else
echo "Default module dir for pwrap.pl will be : Users \$HOME"
fi
echo "Installer that will be used             : $INSTALLTEST"
echo "Make that will be used during the build : $MAKE"
echo "Sound output with SDL                   : $SDL"
echo "Use LHA                                 : $LHAREPLY"
echo "Using GNOME / KDE                       : who cares?"
echo "Using some totally useless feature      : no"
echo


sed -e "s|{DATADIR}|$uadedatadir|" \
    -e "s|{MAKE}|$MAKE|" \
    -e "s|{BINDIR}|$bindir|" \
    -e "s|{DOCDIR}|$docdir|" \
    -e "s|{USEXMMS}|$usexmms|" \
    -e "s|{PACKAGEPREFIX}|$packageprefix|" \
    Makefile.in >Makefile

sed -e "s|{SCORENAME}|$uadedatadir/score|" \
    -e "s|{UAERCNAME}|$uadedatadir/uaerc|" \
    -e "s|{FORMATSNAME}|$uadedatadir/players/uadeformats|" \
    -e "s|{UADEDATADIR}|$uadedatadir/|" \
    -e "s|{UADEEXENAME}|$bindir/uade|" \
    -e "s|{UADEVERSION}|$VERSION|" \
    osdep/uadeconfig-unix.h.in > osdep/uadeconfig.h

GLIBFLAGS=`glib-config --cflags`
GLIBLIBS=`glib-config --libs`
GTKFLAGS=`gtk-config --cflags`
GTKLIBS=`gtk-config --libs`
XMMSFLAGS=`xmms-config --cflags`
XMMSLIBS=`xmms-config --libs`
INCLUDEFLAG="-I$includedir"
LIBFLAG="-L$libdir"

sed -e "s|{INSTALLER}|$INSTALLTEST|" \
    -e "s|{GLIBFLAGS}|$GLIBFLAGS|" -e "s|{GLIBLIBS}|$GLIBLIBS|" \
    -e "s|{GTKFLAGS}|$GTKFLAGS|" -e "s|{GTKLIBS}|$GTKLIBS|" \
    -e "s|{XMMSFLAGS}|$XMMSFLAGS|" -e "s|{XMMSLIBS}|$XMMSLIBS|" \
    -e "s|{INCLUDEFLAG}|$INCLUDEFLAG|" -e "s|{LIBFLAG}|$LIBFLAG|" \
    -e "s|{PLUGINDIR}|$plugin|" \
    -e "s|{PACKAGEPREFIX}|$packageprefix|" \
    -e "s|{ARCHFLAGS}|$ARCHFLAGS|" \
    -e "s|{VERSION}|$VERSION|" \
    -e "s|{DEBUGFLAGS}|$debug_flags|" \
 plugindir/Makefile.in >plugindir/Makefile

sed -e "s|{UADEDATADIR}|$uadedatadir|" \
    -e "s|{PERLDIR}|$PERLDIR|" \
    -e "s|{MYMODDIR}|$mymods|" \
    -e "s|{BINDIR}|$bindir|" \
    frontends/pwrap/pwrap.pl.in >pwrap.pl
chmod 755 pwrap.pl
sed -e "s|{UADE_BINARY}|$bindir/uade|" frontends/euade/euade.in >euade
chmod 755 euade

sed -e "s|{ARCHFLAGS}|$ARCHFLAGS|" -e "s|{ARCHLIBS}|$ARCHLIBS|" \
    -e "s|{SDLFLAGS}|$SDLFLAGS|" -e "s|{SDLLIBS}|$SDLLIBS|" \
    -e "s|{SOUNDLIBS}|$SOUNDLIBS|" \
    -e "s|{DEBUGFLAGS}|$debug_flags|" \
  src/Makefile.in > src/Makefile

if test "$UNIXSHELL" = "yes"; then
    echo "#define HAVE_UNIX_SHELL" >> config.h
    echo "#include \"unix-shell.c\"" > src/local-slave.c
    echo "#include \"unix-shell.h\"" > src/local-slave.h
    echo "" >> src/Makefile
    echo "local-slave.o: local-slave.c local-slave.h unix-shell.c unix-shell.h" >> src/Makefile
    echo "" >> src/Makefile
elif test -n "$OS" && test "$OS" = "MorphOS"; then
    echo "#define HAVE_AMIGA_SHELL" >> config.h
    echo "#include \"amiga-shell.c\"" > src/local-slave.c
    echo "#include \"amiga-shell.h\"" > src/local-slave.h
    echo "" >> src/Makefile
    echo "local-slave.o: local-slave.c local-slave.h amiga-shell.c amiga-shell.h" >> src/Makefile
    echo "" >> src/Makefile
else
    echo "" > src/local-slave.c
    echo "" > src/local-slave.h
    echo "" >> src/Makefile
    echo "local-slave.o: local-slave.c local-slave.h" >> src/Makefile
    echo "" >> src/Makefile
fi

if test "$usexmms" = "yes"; then
    echo "#define HAVE_XMMS_SLAVE" >> config.h
    echo "#include \"xmms-slave.c\"" > src/remote-slave.c
    echo "#include \"xmms-slave.h\"" > src/remote-slave.h
    echo "" >> src/Makefile
    echo "remote-slave.o: remote-slave.c remote-slave.h xmms-slave.c xmms-slave.h" >> src/Makefile
    echo "" >> src/Makefile
else
    echo "" > src/remote-slave.c
    echo "" > src/remote-slave.h
    echo "" >> src/Makefile
    echo "remote-slave.o: remote-slave.c remote-slave.h" >> src/Makefile
    echo "" >> src/Makefile
fi

if test "$interaction" = "yes"; then
    echo "#define HAVE_SHELL_INTERACTION" >> config.h
    echo "#include \"unix-shell-int.c\"" > src/interaction.c
    echo "#include \"unix-shell-int.h\"" > src/interaction.h
    echo "" >> src/Makefile
    echo "interaction.o: interaction.c interaction.h unix-shell-int.c unix-shell-int.h" >> src/Makefile
    echo "" >> src/Makefile
else
    echo "" > src/interaction.c
    echo "" > src/interaction.h
    echo "" >> src/Makefile
    echo "interaction.o: interaction.c interaction.h" >> src/Makefile
    echo "" >> src/Makefile
fi
