#!/bin/sh

VERSION=2.07
IPC_PROTOCOL_VERSION=1

if test -n "$CC"; then
    CC=$CC
    echo "Forcing compiler to be $CC"
else
    CC="gcc"
fi
NATIVECC="$CC"
TARGETCC="$NATIVECC"
CC="false"
TARGETOBJCOPY="objcopy"

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

debug_flags="-g"
packageprefix=""
user="no"
usexmms="auto"
useaudacious="auto"
useuade123="yes"
useuadecore="yes"
xmmsplugindir=""
audaciousplugindir=""
pkgrequirements="ao"

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'`
		;;
	--target-cc=*)
	        TARGETCC=`echo $opt | sed -n 's/--target-cc=\(.*\)/\1/p'`
		;;
	--target=*)
	        tmpvar=`echo $opt | sed -n 's/--target=\(.*\)/\1/p'`
		if test -z "$TARGETCC" ; then
		    TARGETCC="$tmpvar-gcc"
		    TARGETOBJCOPY="$tmpvar-objcopy"
		fi
		tmpvar=""
		;;
	--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'`
		;;
	--audacious-plugin-dir=*)
		audaciousplugindir=`echo $opt | sed -n 's/--audacious-plugin-dir=\(.*\)/\1/p'`
		;;

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

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

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

	--with-audacious)
		useaudacious="yes"
		;;
	--without-audacious)
		useaudacious="no"
		;;

        --user)
		user="yes"
		;;
	--no-debug)
		debug_flags=""
		;;
	--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 directory to linking path"
		echo " --sharedir=dir         Install data files into this directory"
		echo " --target=tarch         Specify target architecture. The compiler will be"
		echo "                        tarch-gcc. (cross compiling)"
		echo " --target-cc=tcc        Specify target compiler. (cross compiling)"
		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 " --with-audacious       Compile an Audacious plugin (auto detect)"
		echo " --without-audacious    Do not compile an Audacious plugin"
		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" -o "$usexmms" = "yes" ; then
    if test -x "`which $XMMS_CONFIG`" ; then
	usexmms="yes"
	XMMSFLAGS="`$XMMS_CONFIG --cflags`"
	XMMSLIBS="`$XMMS_CONFIG --libs`"
    else
	if test "$usexmms" = "yes" ; then
	    echo "Sorry, compiling XMMS plugin is impossible."
	    exit -1
	fi
	usexmms="no"
	echo "Couldn't find xmms-config -> not compiling xmms-plugin!"
    fi
fi

if test "$useaudacious" = "auto" -o "$useaudacious" = "yes" ; then
    if test -n "$PKG_CONFIG" ; then
	useaudacious="no"
	if test -n "`$PKG_CONFIG audacious --libs 2>/dev/null`"; then
		useaudacious="yes"
		AUDACIOUSFLAGS="`$PKG_CONFIG audacious --cflags`"
		AUDACIOUSLIBS="`$PKG_CONFIG audacious --libs`"
	fi
    else
	if test "$useaudacious" = "yes" ; then
	    echo "Sorry, compiling Audacious plugin is impossible."
	    exit -1
	fi
	useaudacious="no"
	echo "Couldn't find pkg-config audacious -> not compiling 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"

SHAREDLIBRARYFLAGS="-fPIC -shared"

cat >src/include/sysincludes.h <<EOF
#include <netinet/in.h>
#include <sys/select.h>
EOF

if test -n "$OS" && test "$OS" = "MorphOS"; then
    echo
    echo "Configuring for MorphOS / AmigaOS."
    ARCHFLAGS="$ARCHFLAGS -noixemul"
    usexmms="no"
    useaudacious="no"    
elif test -n "$OS" && test "$OS" = "AmigaOS"; then
    echo
    echo "Configuring for AmigaOS 4."
    ARCHFLAGS="$ARCHFLAGS -lnet -lauto"
    usexmms="no"
    useaudacious="no"
elif test -n "$OS" && test "$OS" = "Darwin"; then
    echo
    echo "Configuring for MacOSX"
    ARCHFLAGS="$ARCHFLAGS -no-cpp-precomp"
    SHAREDLIBRARYFLAGS="-dynamic -bundle -undefined suppress -force_flat_namespace"
elif test -n "$OS" && test "$OS" = "OpenBSD"; then
    # Force gmake for OpenBSD.
    MAKE="gmake"
elif test -n "$OS" && test "$OS" = "FreeBSD"; then
    cat > src/include/sysincludes.h <<EOF
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <strings.h>
EOF
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 "$useaudacious" = "yes" ; then
	audaciousplugindir="`$PKG_CONFIG audacious --variable=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 "$useaudacious" = "yes" ; then
        if $PKG_CONFIG --max-version=1.2.2 audacious ; then
	  audaciousplugindir="$HOME/.audacious/Plugins/Input"
	else
	  audaciousplugindir="$HOME/.local/share/audacious/Plugins"	
	fi
    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
rm -f strltest
$TARGETCC $ARCHFLAGS $ARCHLIBS -o strltest strltest.c 2>/dev/null
if test -e "strltest" ; then
    echo "#include <string.h>" > ../src/include/strlrep.h
    echo "" > ../src/strlrep.c
else
    cp strlrep.c ../src/
    cp strlrep.h ../src/include/
fi
cd ..

cd compat
rm -f ../src/include/stdint.h
cat > stdinttest.c <<EOF
#include <stdint.h>
int main(void) { int8_t a = 0; int16_t b = 0; int32_t c = 0; int64_t d = 0; uint8_t e = 0; uint16_t f = 0; uint32_t g = 0; uint64_t h = 0; intptr_t i = 0; return 0; }
EOF
$TARGETCC $ARCHFLAGS $ARCHLIBS -o stdinttest stdinttest.c 2>/dev/null
if test "$?" != "0" ; then
    echo "#include <inttypes.h>" > ../src/include/stdint.h
    echo "Potential hazard detected: stdint.h is missing (needed for portable types)"
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

pkgconfigdir="$prefix/lib/pkgconfig"
rm -f uade.pc
if test "$PKG_CONFIG" != "" ; then
    installuadepcrule=""
    if test "$useuade123" = "no" ; then
	pkgrequirements=""
    fi
    cat > uade.pc <<EOF
uadecore=$uadelibdir/$UADECORENAME
data_directory=$uadedatadir
ipc_protocol_version=$IPC_PROTOCOL_VERSION

Name: UADE (Unix Amiga Delitracker Emulator)
Description: A music player for Amiga music formats
Version: $VERSION
Requirements: $pkgrequirements
EOF
fi

echo ""
echo "UADE and frontends will be installed to : $bindir"
echo "Data directory will be                  : $uadedatadir"
echo "Uadecore directory:                     : $uadelibdir"
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"
if test "$TARGETCC" != "$NATIVECC" ; then
    echo "Native CC                               : $NATIVECC"
fi
echo "Target CC                               : $TARGETCC"
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 "Audacious plugin                        : $useaudacious"
if test "$useaudacious" = "yes" ; then
    echo "Audacious plugin directory              : $audaciousplugindir"
fi
echo

# Target is UNIX.
cat > src/ossupport.c <<EOF
#include "unixsupport.c"
EOF

OSSUPPORTMODULES="unixsupport.c include/ossupport.h"

cp src/include/unixsupport.h src/include/ossupport.h
cd compat
$TARGETCC $ARCHFLAGS $ARCHLIBS -o memmemtest memmemtest.c 2>/dev/null
if test "$?" != "0" ; then
    cat memmemrep.h >> ../src/include/ossupport.h
    cat memmemrep.c >> ../src/ossupport.c
    echo "Using memmem() replacement."
fi

csfile="../src/include/compilersupport.h"
echo "#ifndef _UADE_COMPILER_SUPPORT_H_" > "$csfile"
echo "#define _UADE_COMPILER_SUPPORT_H_" >> "$csfile"
$TARGETCC $ARCHFLAGS $ARCHLIBS -o unlikelytest unlikelytest.c 2>/dev/null
if test "$?" = "0" ; then
    cat >> "$csfile" <<EOF
#define likely(x)	__builtin_expect(!!(x), 1)
#define unlikely(x)	__builtin_expect(!!(x), 0)
EOF
else
    cat >> "$csfile" <<EOF
#define likely(x) (!!(x))
#define unlikely(x) (!!(x))
EOF
fi
echo "#endif" >> "$csfile"
cd ..

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 -c /dev/urandom && echo "#define UADE_CONFIG_HAVE_URANDOM" >> "$conffile"
echo "#define UADE_VERSION \"$VERSION\"" >> "$conffile"
test "$OS" = "Cygwin" && echo "#define UADE_HAVE_CYGWIN" >> "$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

if test "$useaudacious" = "yes" ; then
    audaciouspluginrule="audaciousplugin"
    audaciousplugininstallrule="audaciousplugininstall"
else
    audaciouspluginrule=""
    audaciousplugininstallrule=""
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" \
    -e "s|{AUDACIOUSPLUGIN}|$audaciouspluginrule|g" \
    -e "s|{AUDACIOUSPLUGININSTALL}|$audaciousplugininstallrule|g" \
    -e "s|{AUDACIOUSPLUGINDIR}|$audaciousplugindir|g" \
    -e "s|{PKGCONFIGDIR}|$pkgconfigdir|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|{AUDACIOUSFLAGS}|$AUDACIOUSFLAGS|g" \
	-e "s|{AUDACIOUSLIBS}|$AUDACIOUSLIBS|g" \
	-e "s|{CC}|$TARGETCC|g" \
	-e "s|{OBJCOPY}|$TARGETOBJCOPY|g" \
	-e "s|{NATIVECC}|$NATIVECC|g" \
	-e "s|{SOUNDSOURCE}|$SOUNDSOURCE|g" \
	-e "s|{SOUNDHEADER}|$SOUNDHEADER|g" \
	-e "s|{DEBUGFLAGS}|$debug_flags|g" \
	-e "s|{OSSUPPORTMODULES}|$OSSUPPORTMODULES|g" \
	-e "s|{SHAREDLIBRARYFLAGS}|$SHAREDLIBRARYFLAGS|g" \
	"$file" > "$dst"
done
