#!/bin/sh

VERSION=2.13
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="yes"
useaudacious="yes"
useuadefs="yes"
useuade123="yes"
useuadecore="yes"
xmmsplugindir=""
audaciousplugindir=""
pkgrequirements="ao"
textscope="no"

set_all_no() {
    usexmms="no"
    useaudacious="no"
    useuadefs="no"
    useuade123="no"
    useuadecore="no"
}

for opt in "$@" ; do
	case $opt in

# General compilation options

	--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'`
                ;;
	--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=""
		;;

# Options for frontends

	--audacious-plugin-dir=*)
		audaciousplugindir=`echo $opt | sed -n 's/--audacious-plugin-dir=\(.*\)/\1/p'`
		;;

	--no-debug)
		debug_flags=""
		;;

        --user)
		user="yes"
		;;

	--without-audacious)
		useaudacious="no"
		;;
	--only-audacious)
		set_all_no
		useaudacious="yes"
		;;

	--with-text-scope)
		textscope="yes"
		;;

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

	--without-uade123)
		useuade123="no"
		;;
	--only-uade123)
		set_all_no
		useuade123="yes"
		;;

	--without-xmms)
		usexmms="no"
		;;
	--only-xmms)
		set_all_no
		usexmms="yes"
		;;

	--without-uadefs)
		useuadefs="no"
		;;
	--only-uadefs)
		set_all_no
		useuadefs="yes"
		;;

	--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'`
		;;

	--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 " --bindir=dir           Install executables into this directory"
		echo " --includedir=dir       Add this directory to compilation include path"
		echo " --make=exe             Use 'exe' as the make command"
		echo " --mandir=dir           Install man page to this directory"
		echo " --no-debug             Do not compile debug stuff into executables"
		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 "Plugins and tools to compile:"
		echo " --without-audacious    Do not compile an Audacious plugin"
		echo " --with-text-scope      Enable text scope support (uade123 --scope)"
		echo " --without-uade123      Do not compile uade123"
		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 " --without-uadefs       Do not compile uadefs filesystem"
		echo " --without-xmms         Do not compile an XMMS plugin"
		echo " --only-audacious       Compile only Audacious plugin"
		echo " --only-uade123         Compile only uade123 plugin"
		echo " --only-uadecore        Compile only uadecore plugin"
		echo " --only-uadefs          Compile only uadefs plugin"
		echo " --only-xmms            Compile only XMMS plugin"
		echo " --audacious-plugin-dir=PATH  Install Audacious plugin to PATH"
		echo " --xmms-config=exe      Use 'exe' as xmms-config"
		echo " --xmms-plugin-dir=PATH Install XMMS plugin to PATH"
		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 "$PKG_CONFIG" ; then
    echo "The system doesn't have pkg-config. You may not be able to compile some frontends (audacious, uadefs, ...)"
fi

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

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

if test "$useaudacious" = "yes" ; then
    useaudacious="no"
    if test -n "$PKG_CONFIG" ; then
	if test -n "`$PKG_CONFIG audacious --libs 2>/dev/null`"; then
		useaudacious="yes"
		AUDACIOUSFLAGS="`$PKG_CONFIG audacious --cflags`"
		AUDACIOUSLIBS="`$PKG_CONFIG audacious --libs`"
	fi
    fi
    if test "$useaudacious" = "no" ; then
	echo "Couldn't find pkg-config information for audacious plugin -> not compiling it"
    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
	if test -z "$xmmsplugindir" ; then
	    xmmsplugindir="`xmms-config --input-plugin-dir`"
	fi
    fi
    if test "$useaudacious" = "yes" ; then
	if test -z "$audaciousplugindir" ; then
	    audaciousplugindir="`$PKG_CONFIG audacious --variable=input_plugin_dir`"
	fi
    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="$HOME/bin"
    fi
    if test "$usexmms" = "yes" ; then
	if test -z "$xmmsplugindir" ; then
	    xmmsplugindir="$HOME/.xmms/Plugins/Input"
	fi
    fi
    if test "$useaudacious" = "yes" ; then
	if test -z "$audaciousplugindir" ; 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
    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

$NATIVECC -v 2>/dev/null >/dev/null
if test "$?" != "0"; then
    echo Native CC "$NATIVECC" not found, please install a C compiler
    exit 1
fi

$TARGETCC -v 2>/dev/null >/dev/null
if test "$?" != "0"; then
    echo Target CC "$TARGETCC" not found, please install a C compiler
    exit 1
fi

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=""
    AOLIBS="-lao"
    if test -n "$PKG_CONFIG" ; 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
fi

pkgconfigdir="$prefix/lib/pkgconfig"
rm -f uade.pc
if test -n "$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
Requires: $pkgrequirements
EOF
fi

if test "$useuadefs" = "yes" ; then
    useuadefs="no"
    if test -n "$PKG_CONFIG" ; then
	if test -n "$($PKG_CONFIG fuse --libs 2>/dev/null)" ; then
	    useuadefs="yes"
	fi
    fi
fi

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

#include "unixsupport.c"
EOF

cat > src/include/ossupport.h <<EOF
#ifndef _UADE_OSSUPPORT_H_
#define _UADE_OSSUPPORT_H_

#include "unixsupport.h"

EOF
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

$TARGETCC $ARCHFLAGS $ARCHLIBS -o strltest strltest.c 2>/dev/null
if test -e "strltest" ; then
    echo "#include <string.h>" >> ../src/include/ossupport.h
else
    cat strlrep.c >> ../src/ossupport.c
    cat strlrep.h >> ../src/include/ossupport.h
    echo "Using strlcpy/cat() replacements"
fi

$TARGETCC $ARCHFLAGS $ARCHLIBS -o canontest canontest.c 2>/dev/null
if test ! -e "canontest" ; then
    cat canonrep.c >> ../src/ossupport.c
    cat canonrep.h >> ../src/include/ossupport.h
    echo "Using canonical_file_name() replacement"
fi

echo "#endif" >> ../src/include/ossupport.h

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"

test "$textscope" = "yes" && echo "#define UADE_CONFIG_TEXT_SCOPE" >> "$conffile"

echo ""
echo "UADE and frontends is installed to      : $bindir"
echo "Data directory                          : $uadedatadir"
echo "Uadecore directory                      : $uadelibdir"
echo "Man (documentation) directory           : $mandir"
echo "Installer                               : $INSTALLTEST"
echo "Make                                    : $MAKE"
if test "$TARGETCC" != "$NATIVECC" ; then
    echo "Native CC                               : $NATIVECC"
fi
echo "Target CC                               : $TARGETCC"
echo "uadecore                                : $useuadecore"
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 "uadefs                                  : $useuadefs"
echo "Text scope support                      : $textscope"
echo

uadecorerule=""
uadecoreinstallrule=""
uade123rule=""
uade123installrule=""
xmmspluginrule=""
xmmsplugininstallrule=""
audaciouspluginrule=""
audaciousplugininstallrule=""
uadefsrule=""
uadefsinstallrule=""

if test "$useuadecore" = "yes" ; then
    uadecorerule="uadecore"
    uadecoreinstallrule="uadecoreinstall"
fi
if test "$useuade123" = "yes" ; then
    uade123rule="uade123"
    uade123installrule="uade123install"
fi
if test "$usexmms" = "yes" ; then
    xmmspluginrule="xmmsplugin"
    xmmsplugininstallrule="xmmsplugininstall"
fi
if test "$useaudacious" = "yes" ; then
    audaciouspluginrule="audaciousplugin"
    audaciousplugininstallrule="audaciousplugininstall"
fi
if test "$useuadefs" = "yes" ; then
    uadefsrule="uadefs"
    uadefsinstallrule="uadefsinstall"
fi

replace_with_sed() {
    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|{SHAREDLIBRARYFLAGS}|$SHAREDLIBRARYFLAGS|g" \
	-e "s|{VERSION}|$VERSION|g" \
	-e "s|{MAKE}|$MAKE|g" \
	-e "s|{DATADIR}|$uadedatadir|g" \
	-e "s|{BINDIR}|$bindir|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|{UADEFS}|$uadefsrule|g" \
	-e "s|{UADEFSINSTALL}|$uadefsinstallrule|g" \
	-e "s|{PKGCONFIGDIR}|$pkgconfigdir|g" \
	"$@"
}

replace_with_sed Makefile.in > Makefile

for file in src/Makefile.in src/frontends/*/Makefile.in src/frontends/mod2ogg/mod2ogg2.sh.in ; do
    dst="`echo $file |sed -e "s|\.in||"`"
    replace_with_sed "$file" > "$dst"
done

# Make mod2ogg script executable
chmod u+x src/frontends/mod2ogg/mod2ogg2.sh
