#!/bin/sh

VERSION=0.71

prefix=$HOME/.xmms
mymods=$HOME
xmmsinputplugindir=automatic

for arg
do 

if expr "$arg" : "--prefix=.*" > /dev/null;then 
prefix=`echo $arg | sed -n 's/--prefix=\(.*\)/\1/p'`
fi

if expr "$arg" : "--mymods=.*" > /dev/null;then 
mymods=`echo $arg | sed -n 's/--mymods=\(.*\)/\1/p'`
fi

if expr "$arg" : "--input-plugin-dir=.*" > /dev/null;then 
xmmsinputplugindir=`echo $arg | sed -n 's/--input-plugin-dir=\(.*\)/\1/p'`
fi

if test "$arg" = "--help";then
echo
echo "Usage: --prefix=path if you don't want this installed in /usr/local."
echo "       --mymods=path where path is the default mod directory for "
echo "                pwraps tk interface. Default path is homedir."
echo
exit 1
break
fi

done

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

OS="`uname`"

if test -n "$OS" && test "$OS" = "SunOS"; then
echo
echo "Configuring for Solaris." 
echo '#include "sd-sound-solaris.c"' >> src/sd-sound.c;
SOLARIS=yes
else
echo '#include "sd-sound-oss.c"' >> src/sd-sound.c;
fi

MACHINE="`uname -m`"
ARCHFLAGS= 
if test "$MACHINE" = "parisc"; then
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 -n "$MAKE"; then
echo 
else
MAKE="`which make`"
fi
cp configure.h config.h
chmod u+w config.h

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"
plugin=`xmms-config --input-plugin-dir`
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 -n "$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

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"
echo "Default module dir for pwrap.pl will be : $mymods"
echo "Installer that will be used             : $INSTALLTEST"
echo "Make that will be used during the build : $MAKE"
echo "Use LHA                                 : $LHAREPLY"
echo


sed -e "s|{SYSDATADIR}|$uadedatadir|" \
    -e "s|{MAKE}|$MAKE|" \
    -e "s|{BINDIR}|$bindir|" \
    -e "s|{DOCDIR}|$docdir|" \
    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|{VERSION}|$VERSION|" \
 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|{ARCHFLAGS}|$ARCHFLAGS|" src/Makefile.in > src/Makefile
