#!/bin/sh
#
#  nspluginwrapper configure script (C) 2005-2006 Gwenole Beauchesne
#  derived from qemu configure script, (C) 2003 Fabrice Bellard
#
PACKAGE=nspluginwrapper

# set temporary file name
if test ! -z "$TMPDIR" ; then
    TMPDIR1="${TMPDIR}"
elif test ! -z "$TEMPDIR" ; then
    TMPDIR1="${TEMPDIR}"
else
    TMPDIR1="/tmp"
fi

TMPC="${TMPDIR1}/npw-conf-${RANDOM}-$$-${RANDOM}.c"
TMPO="${TMPDIR1}/npw-conf-${RANDOM}-$$-${RANDOM}.o"
TMPE="${TMPDIR1}/npw-conf-${RANDOM}-$$-${RANDOM}"
TMPS="${TMPDIR1}/npw-conf-${RANDOM}-$$-${RANDOM}.S"

# default parameters
prefix="/usr"
lib=""
cc="gcc -std=c99"
cxx="g++"
host_os=`uname -s`
host_cpu=`uname -m`
target_cpu="i386"
case "$host_cpu" in
i386|i486|i586|i686|i86pc|BePC)
    cpu="i386"
    ;;
ia64)
    cpu="ia64"
    ;;
"Power Macintosh"|ppc)
    cpu="powerpc"
    ;;
ppc64)
    cpu="powerpc64"
    ;;
sparc)
    cpu="sparc"
    ;;
sparc64)
    cpu="sparc64"
    ;;
x86_64|amd64)
    cpu="x86_64"
    ;;
*)
    cpu="unknown"
    ;;
esac
bigendian="no"

# find source path
# XXX: we assume an absolute path is given when launching configure, 
# except in './configure' case.
source_path=${0%configure}
source_path=${source_path%/}
source_path_used="yes"
if test -z "$source_path" -o "$source_path" = "." ; then
    source_path=`pwd`
    source_path_used="no"
fi

for opt do
case "$opt" in
--prefix=*)
    prefix=`echo $opt | cut -d '=' -f 2`
    ;;
--target-cpu=*)
    target_cpu=`echo $opt | cut -d '=' -f 2`
    ;;
--with-lib=*)
    lib=`echo $opt | cut -d '=' -f 2`
    ;;
--with-cc=*)
    cc=`echo $opt | cut -d '=' -f 2`
    ;;
--wuth-cxx=*)
    cxx=`echo $opt | cut -d '=' -f 2`
    ;;
esac
done

# check for libdir name
if test -z "$lib"; then
    case $host_os in
    Linux)
	# test if the compiler is 64bit
        echo 'int i;' > $TMPC
	nspluginwrapper_64bit_output=no
	if $cc -o $TMPO -c $TMPC; then
	    case `/usr/bin/file $TMPO` in
	    *"ELF 64"*)
		nspluginwrapper_64bit_output=yes
		;;
	    esac
	fi
	rm -f $TMPC $TMPO
	;;
    esac
    case $host_cpu:$nspluginwrapper_64bit_output in
    powerpc64:yes | s390x:yes | sparc64:yes | x86_64:yes)
	lib="lib64"
	;;
    *)
	lib="lib"
	;;
    esac
fi

# check for CFLAGS and CXXFLAGS
if test -z "$CFLAGS"; then
    CFLAGS="-O0 -g"
fi
if test -z "$CXXFLAGS"; then
    CXXFLAGS="$CFLAGS"
fi

# check for pkg-config
pkgconfig=`which pkg-config`
if test -z "$pkgconfig"; then
    echo "pkg-config not found"
    exit 1
fi

# check for Glib 2.0 compile CFLAGS
if $pkgconfig --exists glib-2.0; then
    GLIB_CFLAGS=`$pkgconfig --cflags glib-2.0`
    GLIB_LDFLAGS=`$pkgconfig --libs glib-2.0`
else
    echo "GLIB 2.0 environment not found"
    exit 1
fi

# check for GTK+ 2.0 compile CFLAGS
if $pkgconfig --exists gtk+-2.0; then
    GTK_CFLAGS=`$pkgconfig --cflags gtk+-2.0`
    GTK_LDFLAGS=`$pkgconfig --libs gtk+-2.0`
else
    echo "GTK+ 2.0 environment not found"
    exit 1
fi

# big/little endian test
cat > $TMPC << EOF
#include <inttypes.h>
int main(int argc, char ** argv){
	volatile uint32_t i=0x01234567;
	return (*((uint8_t*)(&i))) == 0x67;
}
EOF

if $cc -o $TMPE $TMPC 2>/dev/null ; then
    $TMPE && bigendian="yes"
else
    echo "big/little test failed"
fi

rm -f $TMPO $TMPC $TMPE $TMPS

# print configure help
if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
cat << EOF

Usage: configure [options]
Options: [defaults in brackets after descriptions]

EOF
echo "Standard options:"
echo "  --help                      print this message"
echo "  --prefix=PREFIX             install in PREFIX [$prefix]"
echo "  --target-cpu=CPU            set plugin support for target CPU [$target_cpu]"
echo ""
echo "Advanced options (experts only):"
echo "  --source-path=PATH          path of source code [$source_path]"
echo "  --with-lib=NAME             use NAME as library dir name [$lib]"
echo "  --with-cc=CC                use C compiler CC [$cc]"
echo "  --with-cxx=CXX              use C++ compiler CXX [$cxx]"
echo ""
echo "NOTE: The object files are build at the place where configure is launched"
exit 1
fi

echo "Install prefix    $prefix"
echo "Library dir name  $lib"
echo "Source path       $source_path"
echo "C compiler        $cc"
echo "C++ compiler      $cxx"
echo "host CPU          $cpu"
echo "host big endian   $bigendian"
echo "target CPU        $target_cpu"

config_mak="config-host.mak"
echo "# Automatically generated by configure - do not modify" > $config_mak

config_h="config-host.h"
echo "/* Automatically generated by configure - do not modify */" > $config_h

echo "CC=$cc" >> $config_mak
echo "CXX=$cxx" >> $config_mak
echo "CFLAGS=$CFLAGS" >> $config_mak
echo "CXXFLAGS=$CXXFLAGS" >> $config_mak
echo "GLIB_CFLAGS=$GLIB_CFLAGS" >> $config_mak
echo "GLIB_LDFLAGS=$GLIB_LDFLAGS" >> $config_mak
echo "GTK_CFLAGS=$GTK_CFLAGS" >> $config_mak
echo "GTK_LDFLAGS=$GTK_LDFLAGS" >> $config_mak
if test "$cpu" = "i386" ; then
    echo "ARCH=i386" >> $config_mak
    echo "#define HOST_I386 1" >> $config_h
	echo "#define HOST_ARCH \"i386\"" >> $config_h
elif test "$cpu" = "x86_64" ; then
    echo "ARCH=x86_64" >> $config_mak
    echo "#define HOST_X86_64 1" >> $config_h
	echo "#define HOST_ARCH \"x86_64\"" >> $config_h
elif test "$cpu" = "powerpc" ; then
    echo "ARCH=ppc" >> $config_mak
    echo "#define HOST_PPC 1" >> $config_h
	echo "#define HOST_ARCH \"ppc\"" >> $config_h
elif test "$cpu" = "powerpc64" ; then
    echo "ARCH=ppc64" >> $config_mak
    echo "#define HOST_PPC64 1" >> $config_h
	echo "#define HOST_ARCH \"ppc64\"" >> $config_h
elif test "$cpu" = "sparc" ; then
    echo "ARCH=sparc" >> $config_mak
	echo "#define HOST_SPARC 1" >> $config_h
	echo "#define HOST_ARCH \"sparc\"" >> $config_h
elif test "$cpu" = "sparc64" ; then
    echo "ARCH=sparc64" >> $config_mak
    echo "#define HOST_SPARC64 1" >> $config_h
	echo "#define HOST_ARCH \"sparc64\"" >> $config_h
elif test "$cpu" = "ia64" ; then
    echo "ARCH=ia64" >> $config_mak
    echo "#define HOST_IA64 1" >> $config_h
	echo "#define HOST_ARCH \"ia64\"" >> $config_h
else
    echo "Unsupported CPU"
    exit 1
fi
if test "$bigendian" = "yes" ; then
    echo "WORDS_BIGENDIAN=yes" >> $config_mak
    echo "#define WORDS_BIGENDIAN 1" >> $config_h
fi
if test -f "/usr/include/byteswap.h" ; then
    echo "#define HAVE_BYTESWAP_H 1" >> $config_h
fi

echo "SRC_PATH=$source_path" >> $config_mak
echo "lib=$lib" >> $config_mak
echo "prefix=$prefix" >> $config_mak
libdir="$prefix/$lib"
echo "libdir=$libdir" >> $config_mak
echo "#define LIBDIR \"$libdir\"" >> $config_h

VERSION=`sed < $source_path/$PACKAGE.spec -n '/^\%define version[	]*/s///p'`
echo "VERSION=$VERSION" >>$config_mak
echo "#define NPW_VERSION \"$VERSION\"" >> $config_h

pkglibdir="$prefix/lib/$PACKAGE"
echo "pkglibdir=$pkglibdir" >> $config_mak
echo "#define NPW_LIBDIR \"$pkglibdir\"" >> $config_h

config_mak="config.mak"
echo "# Automatically generated by configure - do not modify" > $config_mak
echo "include config-host.mak" >> $config_mak

config_h="config.h"
echo "/* Automatically generated by configure - do not modify */" > $config_h
echo "#include \"config-host.h\"" >> $config_h

if test "$target_cpu" = "i386" ; then
    echo "TARGET_ARCH=i386" >> $config_mak
    echo "#define TARGET_ARCH \"i386\"" >> $config_h
    echo "#define TARGET_I386 1" >> $config_h
elif test "$target_cpu" = "ppc" ; then
    echo "TARGET_ARCH=ppc" >> $config_mak
    echo "#define TARGET_ARCH \"ppc\"" >> $config_h
    echo "#define TARGET_PPC 1" >> $config_h
else
    echo "Unsupported target CPU"
    exit 1
fi

# build tree in object directory if source path is different from current one
if test "$source_path_used" = "yes" ; then
    case $source_path in
    ..*)
	sp1=..
	;;
    esac
    ln -sf $source_path/Makefile Makefile
    mkdir -p tests
    ln -sf $source_path/$sp1/tests/Makefile tests/Makefile
fi
