#!/bin/sh
#
#  nspluginwrapper configure script (C) 2005-2009 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"

# 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

# determine versions
VERSION=`sed < $source_path/$PACKAGE.spec -n '/^\%define version[	]*/s///p'`
RELEASE=`sed < $source_path/$PACKAGE.spec -n '/^\%define release[	]*/s///p'`
SVNDATE=`sed < $source_path/$PACKAGE.spec -n '/^\%define svndate[ 	]*/s///p'`
if test -z "$SVNDATE"; then
    SVNDATE=`date '+%Y%m%d'`
fi

MAJOR_VERSION=`echo "$VERSION"|cut -d'.' -f1`
MINOR_VERSION=`echo "$VERSION"|cut -d'.' -f2`
MICRO_VERSION=`echo "$VERSION"|cut -d'.' -f3`

# development snapshots are officially generated tarballs for testing
# ("odd" minor and micro versions)
is_odd() {
    local rem=`expr "$1" % 2`
    test $rem -eq 1 && return 0 || return 1
}

if is_odd $MINOR_VERSION || is_odd $MICRO_VERSION; then
    SNAPSHOT=1
else
    SNAPSHOT=0
fi

# snapshots can also be unofficially generated tarballs
# (Release: 0.1 in specfile)
if echo "$RELEASE" | grep -q ^0; then
    SNAPSHOT=2
fi

if test $SNAPSHOT -ge 1; then
    yes_for_snapshots="yes"
else
    yes_for_snapshots="no"
fi

# default parameters
prefix="/usr"
lib32=""
lib64=""
x_base_dirs=""
build_viewer="guess"
build_player="yes"
build_generic="guess"
build_biarch="guess"
strip="no"
cc="gcc"
cxx="g++"
host_os=`uname -s | tr '[A-Z]' '[a-z]'`
host_cpu=`uname -m`
target_os="linux"
target_cpu="i386"
rpc_init_timeout=5
malloc_hooks="glib,libc"
enable_malloc_check="$yes_for_snapshots"
enable_thread_check="$yes_for_snapshots"

normalize_cpu() {
local cpu="$1"
case "$cpu" in
arm*)
    cpu="arm"
    ;;
i[3456]86|k[678]|i86pc|BePC)
    cpu="i386"
    ;;
ia64)
    cpu="ia64"
    ;;
"Power Macintosh"|ppc)
    cpu="ppc"
    ;;
ppc64)
    cpu="ppc64"
    ;;
sparc)
    cpu="sparc"
    ;;
sparc64)
    cpu="sparc64"
    ;;
x86_64|amd64)
    cpu="x86_64"
    ;;
esac
echo "$cpu"
}

normalize_os() {
local os="$1"
case "$os" in
sunos*)
    os="solaris"
    ;;
esac
echo "$os"
}

for opt do
case "$opt" in
--prefix=*)
    prefix=`echo "$opt" | cut -d '=' -f 2`
    ;;
--pkglibdir=*)
    pkglibdir=`echo "$opt" | cut -d '=' -f 2`
    ;;
--viewer-paths=*)
    viewer_paths=`echo "$opt" | cut -d '=' -f 2`
    ;;
--target-os=*)
    target_os=`echo "$opt" | cut -d '=' -f 2 | tr '[A-Z]' '[a-z]'`
    ;;
--target-cpu=*)
    target_cpu=`echo "$opt" | cut -d '=' -f 2`
    ;;
--enable-generic)
    build_generic="yes"
    ;;
--disable-generic)
    build_generic="no"
    ;;
--enable-strip)
    strip="yes"
    ;;
--disable-strip)
    strip="no"
    ;;
--enable-biarch)
    build_biarch="yes"
    ;;
--disable-biarch)
    build_biarch="no"
    ;;
--enable-viewer)
    build_viewer="yes"
    ;;
--disable-viewer)
    build_viewer="no"
    ;;
--enable-player)
    build_player="yes"
    ;;
--disable-player)
    build_player="no"
    ;;
--enable-thread-check)
    enable_thread_check="yes"
    ;;
--disable-thread-check)
    enable_thread_check="no"
    ;;
--enable-malloc-check)
    enable_malloc_check="yes"
    ;;
--disable-malloc-check)
    enable_malloc_check="no"
    ;;
--with-lib32=*)
    lib32=`echo "$opt" | cut -d '=' -f 2`
    ;;
--with-lib64=*)
    lib64=`echo "$opt" | cut -d '=' -f 2`
    ;;
--with-x11-prefix=*)
    x_base_dirs=`echo "$opt" | cut -d '=' -f 2`
    ;;
--with-cc=*)
    cc=`echo "$opt" | cut -d '=' -f 2`
    ;;
--with-cxx=*)
    cxx=`echo "$opt" | cut -d '=' -f 2`
    ;;
--with-malloc=*)
    malloc_hooks=`echo "$opt" | cut -d '=' -f 2`
    ;;
--rpc-init-timeout=*)
    rpc_init_timeout=`echo "$opt" | cut -d '=' -f 2`
    ;;
esac
done

host_cpu=`normalize_cpu "$host_cpu"`
host_os=`normalize_os "$host_os"`
target_cpu=`normalize_cpu "$target_cpu"`
target_os=`normalize_os "$target_os"`

# check for linux only build
if test "$build_generic" = "guess"; then
    if test "$host_os" = "linux" -a "$target_os" = "linux"; then
	build_generic="no"
    else
	build_generic="yes"
    fi
fi

# check for biarch build (Linux only)
# XXX: biarch builds require LSB headers for now
build_biarch_possible="no"
if test "$host_os" = "linux" -a "$target_os" = "linux"; then
    case $host_cpu:$target_cpu in
    x86_64:i386 | ppc64:ppc)
	build_biarch_possible="yes"
	;;
    esac
fi
if test "$build_biarch" = "guess"; then
    build_biarch="$build_biarch_possible"
elif test "$build_biarch" = "yes"; then
    if test "$build_biarch_possible" = "no"; then
	echo "WARNING: bi-arch build is not possible, disabling"
	build_biarch="no"
    fi
fi

# check for viewer build
if test "$build_viewer" = "guess"; then
    build_viewer="no"
    case $host_os in
    linux)
	if test "$host_cpu" = "$target_cpu" -o "$build_biarch" = "yes"; then
	    build_viewer="yes"
	fi
	;;
    esac
fi

# check for libdir name
if test -z "$lib64"; 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
    ppc64:yes | s390x:yes | sparc64:yes | x86_64:yes)
	lib64="lib64"
	;;
    *)
	lib64="lib"
	;;
    esac
fi
if test -z "$lib32"; then
    lib32="lib"
fi

# check for installation root
if test -z "$pkglibdir"; then
    pkglibdir="$prefix/lib/$PACKAGE"
fi

# check for viewer paths
default_viewer_paths="$pkglibdir/%ARCH%/%OS%"
if test -z "$viewer_paths"; then
    viewer_paths="$default_viewer_paths"
fi

# check for __attribute__((visibility())) support
cat > $TMPC << EOF
int foo __attribute__((visibility("hidden"))) = 1;
int bar __attribute__((visibility("protected"))) = 1;
EOF
has_visibility_attribute=no
if $cc -Werror -S $TMPC -o $TMPS >/dev/null 2>&1; then
    if grep '\.hidden.*foo' $TMPS >/dev/null; then
	if grep '\.protected.*bar' $TMPS >/dev/null; then
	    has_visibility_attribute=yes
	fi
    fi
fi
rm -f $TMPC $TMPS

# check for .init_array/.fini_array support
cat > $TMPC << EOF
static int x = -1;
int main(void) { return x; }
int foo(void) { x = 0; }
int (*fp)(void) __attribute__((section(".init_array"))) = foo;
EOF
has_initfini_array=no
if $cc -Werror $TMPC -o $TMPE >/dev/null 2>&1; then
    if $TMPE; then
	has_initfini_array=yes
    fi
fi
rm -f $TMPC $TMPE

# check for sockaddr_un::sun_len member
cat > $TMPC << EOF
#include <sys/types.h>
#include <sys/un.h>
int main(void) {
  struct sockaddr_un addr;
  addr.sun_len = 0;
  return 0;
}
EOF
has_sockaddr_un_sun_len=no
if $cc -Werror $TMPC -o $TMPE >/dev/null 2>&1; then
    if $TMPE; then
	has_sockaddr_un_sun_len=yes
    fi
fi
rm -f $TMPC $TMPE

# check for egrep program
if echo a | (grep -E '(a|b)') >/dev/null 2>&1; then
    EGREP='grep -E'
else
    EGREP='egrep'
fi

# check for __stack_chk_fail() in target GNU C library
if test "$build_biarch" = "yes"; then
    glibc_header_dir="/usr/include"
    libc_provides_ssp=no
    if test -f $glibc_header_dir/features.h \
	&& $EGREP '^[ 	]*#[ 	]*define[ 	]+__GNU_LIBRARY__[ 	]+([1-9][0-9]|[6-9])' \
	$glibc_header_dir/features.h > /dev/null; then
	if $EGREP '^[ 	]*#[ 	]*define[ 	]+__GLIBC__[ 	]+([1-9][0-9]|[3-9])' \
	    $glibc_header_dir/features.h > /dev/null; then
	    libc_provides_ssp=yes
	elif $EGREP '^[ 	]*#[ 	]*define[ 	]+__GLIBC__[ 	]+2' \
	    $glibc_header_dir/features.h > /dev/null \
	    && $EGREP '^[ 	]*#[ 	]*define[ 	]+__GLIBC_MINOR__[ 	]+([1-9][0-9]|[4-9])' \
	    $glibc_header_dir/features.h > /dev/null; then
	    libc_provides_ssp=yes
	fi
    fi
fi

# check for compiler type
cat > $TMPC << EOF
#include <stdio.h>
#if defined __INTEL_COMPILER
#define COMPILER_NAME "icc"
#elif defined __GNUC__
#define COMPILER_NAME "gcc"
#elif defined __xlC__
#define COMPILER_NAME "xlc"
#elif defined __sgi
#define COMPILER_NAME "mipspro"
#elif defined __SUNPRO_C || defined __SUNPRO_CC
#define COMPILER_NAME "sunstudio"
#else
#define COMPILER_NAME "unknown"
#endif
int main(void)
{
    printf("%s\n", COMPILER_NAME);
    return 0;
}
EOF
compiler="unknown"
if $cc $TMPC -o $TMPE >/dev/null 2>&1; then
    compiler=`$TMPE`
fi
rm -f $TMPC $TMPE

# check for compiler flag
check_cc_option() {
    echo "int i;" > $TMPC
    if $cc $* -c $TMPC -o $TMPO > /dev/null 2>&1; then
	rm -f $TMPC $TMPO
	return 0
    fi
    rm -f $TMPC
    return 1
}

# check for c99 support
if test "$compiler" = "gcc"; then
    if check_cc_option -std=c99; then
	cc="$cc -std=c99"
    fi
elif test "$compiler" = "icc"; then
    if check_cc_option -std=c99; then
	cc="$cc -std=c99"
    elif check_cc_option -c99; then
	cc="$cc -c99"
    fi
elif test "$compiler" = "xlc"; then
    if check_cc_option -qlanglvl=extc99; then
	cc="$cc -qlanglvl=extc99"
    fi
elif test "$compiler" = "mipspro"; then
    if check_cc_option -c99; then
	cc="$cc -c99"
    fi
elif test "$compiler" = "sunstudio"; then
    if check_cc_option -xc99; then
	cc="$cc -xc99"
    fi
fi

# check for CFLAGS
if test "$compiler" = "gcc"; then
    if test -z "$CFLAGS"; then
	CFLAGS="-O2 -g"
	if check_cc_option -mtune=generic $CFLAGS; then
	    CFLAGS="$CFLAGS -mtune=generic"
	fi
    fi
    if test "$build_biarch" = "yes" -a -z "$CFLAGS_32"; then
	CFLAGS_32="-m32 -O2 -g"
	if check_cc_option -mtune=generic $CFLAGS_32; then
	    CFLAGS_32="$CFLAGS_32 -mtune=generic"
	fi
    fi
    if check_cc_option -Wall; then
	CFLAGS="$CFLAGS -Wall"
    fi
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`
    GLIB_VERSION=`$pkgconfig --modversion glib-2.0`
else
    echo "GLIB 2.0 environment not found"
    exit 1
fi
cat > $TMPC << EOF
#include <glib.h>
int main(void) {
    (void) g_main_pending();
    return 0;
}
EOF
if ! $cc $CFLAGS $GLIB_CFLAGS $GLIB_LDFLAGS $TMPC -o $TMPE > /dev/null 2>&1; then
    echo "GLIB 2.0 environment not usable"
    rm -f $TMPC
    exit 1
fi
rm -f $TMPC $TMPE

# check for GTK+ 2.0 compile CFLAGS
if test "$build_viewer" = "yes" -o "$build_player" = "yes"; then
    if $pkgconfig --exists gtk+-2.0; then
	GTK_CFLAGS=`$pkgconfig --cflags gtk+-2.0`
	GTK_LDFLAGS=`$pkgconfig --libs gtk+-2.0`
	GTK_VERSION=`$pkgconfig --modversion gtk+-2.0`
    else
	echo "GTK+ 2.0 environment not found"
	exit 1
    fi
    cat > $TMPC << EOF
#include <gtk/gtk.h>
int main(void) {
    gtk_main_quit();
    return 0;
}
EOF
    if ! $cc $CFLAGS $GTK_CFLAGS $GTK_LDFLAGS $TMPC -o $TMPE > /dev/null 2>&1; then
	echo "GTK+ 2.0 environment not usable"
	rm -f $TMPC
	exit 1
    fi
    rm -f $TMPC $TMPE
fi

# check for cURL compile CFLAGS
if test "$build_player" = "yes"; then
    if $pkgconfig --exists libcurl; then
	CURL_CFLAGS=`$pkgconfig --cflags libcurl`
	CURL_LDFLAGS=`$pkgconfig --libs libcurl`
    else
	curlconfig=`which curl-config`
	if test -n "$curlconfig"; then
	    CURL_CFLAGS=`$curlconfig --cflags`
	    CURL_LDFLAGS=`$curlconfig --libs`
	else
	    echo "cURL environment not found"
	    exit 1
	fi
    fi
    cat > $TMPC << EOF
#include <curl/curl.h>
int main(void) {
    curl_global_init(CURL_GLOBAL_NOTHING);
    return 0;
}
EOF
    if ! $cc $CFLAGS $CURL_CFLAGS $CURL_LDFLAGS $TMPC -o $TMPE > /dev/null 2>&1; then
	echo "cURL environment not usable"
	rm -f $TMPC
	exit 1
    fi
    rm -f $TMPC $TMPE
fi

# check for X11 base dir
if test -z "$x_base_dirs"; then
    x_base_dirs="
	/usr
	/usr/X11R6
	/usr/local/X11R6
	$prefix
    "
fi
for dir in $x_base_dirs; do
    x_include_dir="$dir/include"
    if test -f $x_include_dir/X11/Intrinsic.h; then
	x_lib_dir="$dir/$lib64"
	if test -f $x_lib_dir/libXt.so; then
	    x_base_dir=$dir
	    break
	fi
    fi
done
if test -z "$x_base_dir"; then
    echo "X11/Xt 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

bigendian="no"
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

# floating point endian test
cat > $TMPC << EOF
/* This will not work unless sizeof(double) == 8.  */
extern char sizeof_double_must_be_8 [sizeof(double) == 8 ? 1 : -1];

/* This structure must have no internal padding.  */
struct possibility {
  char prefix[8];
  double candidate;
  char postfix[8];
};

#define C(cand) { "\nformat:", cand, ":tamrof\n" }
struct possibility table [] =
{
  C( 3.25724264705901305206e+01), /* @@IEEEFP - IEEE 754 */
  C( 3.53802595280598432000e+18), /* D__float - VAX */
  C( 5.32201830133125317057e-19), /* D.PDP-10 - PDP-10 - the dot is 0x13a */
  C( 1.77977764695171661377e+10), /* IBMHEXFP - s/390 format, ascii */
  C(-5.22995989424860458374e+10)  /* IBMHEXFP - s/390 format, EBCDIC */
};
EOF

if $cc -o $TMPO -c $TMPC 2>/dev/null; then
    od -c $TMPO |
    sed 's/^[0-7]*[ 	]*/ /
	 s/\*/./g
	 s/ \\n/*/g
	 s/ [0-9][0-9][0-9]/./g
	 s/  \\[^ ]/./g' |
    tr -d '
 ' | tr -s '*' '
' | fold | sed '$a\
' > $TMPE

    if grep 'format:.@IEEEF.:tamrof' $TMPE >/dev/null 2>&1; then
	float_format='IEEE (big-endian)'
    elif grep 'format:.I@@PFE.:tamrof' $TMPE >/dev/null 2>&1; then
	float_format='IEEE (big-endian)'
    elif grep 'format:.FEEEI@.:tamrof' $TMPE >/dev/null 2>&1; then
	float_format='IEEE (little-endian)'
    elif grep 'format:.EFP@@I.:tamrof' $TMPE >/dev/null 2>&1; then
	float_format='IEEE (little-endian)'
    elif grep 'format:.__floa.:tamrof' $TMPE >/dev/null 2>&1; then
	float_format='VAX D-float'
    elif grep 'format:..PDP-1.:tamrof' $TMPE >/dev/null 2>&1; then
	float_format='PDP-10'
    elif grep 'format:.BMHEXF.:tamrof' $TMPE >/dev/null 2>&1; then
	float_format='IBM 370 hex'
    else
	echo "unknown floating point format"
	exit 1
    fi

    fbigendian=
    case $float_format in
    'IEEE (big-endian)')
	if test "$bigendian" = "no"; then
	    fbigendian=1
	fi
	;;
    'IEEE (little-endian)')
	if test "$bigendian" = "yes"; then
	    fbigendian=0
	fi
	;;
    *)
	echo "unsupported floating point format $float_format"
	exit 1
	;;
    esac
else
    echo "floating point endian test failed"
fi

rm -f $TMPO $TMPC $TMPE

# detect memory allocation hooks
malloc_hooks_list=`echo "$malloc_hooks" | sed -e 's/,/ /g'`
malloc_hooks=""
malloc_hook_default=""
for mh in $malloc_hooks_list; do
    is_ok="no"
    case $mh in
    libc)
	is_ok="yes"
	;;
    glib)
	if test "x$build_generic" = "xyes"; then
	    echo "WARNING: disabling glib memory hooks with --enable-generic"
	elif ! $pkgconfig --atleast-version=2.10 glib-2.0; then
	    echo "WARNING: disabling glib memory hooks that require glib >= 2.10 (system is $GLIB_VERSION)"
	else
	    is_ok="yes"
	fi
	;;
    esac
    if test "x$is_ok" = "xyes"; then
	if test -n "$malloc_hooks"; then
	    malloc_hooks="$malloc_hooks $mh"
	else
	    malloc_hooks="$mh"
	    malloc_hook_default="$mh"
	fi
    fi
done
if test -z "$malloc_hooks"; then
    malloc_hook_default="libc"
    malloc_hooks="$malloc_hook_default"
fi

# 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 "  --pkglibdir=ROOT            install private files in ROOT [$pkglibdir]"
echo "  --viewer-paths=PATH         allowed viewer lookup PATH [$viewer_paths]"
echo "  --target-os=OS              build plugin support for target OS [$target_os]"
echo "  --target-cpu=CPU            build plugin support for target CPU [$target_cpu]"
echo "  --enable-viewer             build viewer [$build_viewer]"
echo "  --enable-player             build player [$build_player]"
echo ""
echo "Advanced options (experts only):"
echo "  --source-path=PATH          path of source code [$source_path]"
echo "  --enable-strip              strip resulting binaries and libraries [$strip]"
echo "  --enable-generic            build with generic APIs [$build_generic]"
echo "  --enable-biarch             build both 32-bit and 64-bit components at once [$build_biarch]"
echo "  --enable-thread-check       enable main thread checks (DEBUG) [$enable_thread_check]"
echo "  --enable-malloc-check       enable memory allocation checks (DEBUG) [$enable_malloc_check]"
echo "  --with-lib32=NAME           use NAME as the 32-bit library dir name [$lib32]"
echo "  --with-lib64=NAME           use NAME as the 64-bit library dir name [$lib64]"
echo "  --with-x11-prefix=PREFIX    use PREFIX as the X11 base dir [autodetect]"
echo "  --with-cc=CC                use C compiler CC [$cc]"
echo "  --with-cxx=CXX              use C++ compiler CXX [$cxx]"
echo "  --with-malloc=MALLOC        use MALLOC for memory allocation hooks [$malloc_hooks]"
echo "  --rpc-init-timeout=VALUE    wait VALUE seconds for the plugin to connect [$rpc_init_timeout]"
echo ""
echo "NOTE: The object files are built at the place where configure is launched"
exit 1
fi

echo "Source path               $source_path"
echo "Install prefix            $prefix"
echo "nspluginwrapper root dir  $pkglibdir"
echo "Viewer paths              $viewer_paths"
echo "Build viewer              $build_viewer"
echo "Build standalone player   $build_player"
echo "Build 32-/64-bit at once  $build_biarch"
echo "Build with generic APIs   $build_generic"
echo "32-bit library dir name   $lib32"
echo "64-bit library dir name   $lib64"
echo "C compiler                $cc"
echo "C++ compiler              $cxx"
echo "Strip binaries            $strip"
echo "Host OS                   $host_os"
echo "Host CPU                  $host_cpu"
echo "Host big endian           $bigendian"
echo "Target OS                 $target_os"
echo "Target CPU                $target_cpu"
echo "RPC init timeout          $rpc_init_timeout secs"
echo "Memory allocation hooks   $malloc_hooks"
echo "Use thread checks (DEBUG) $enable_thread_check"
echo "Use malloc checks (DEBUG) $enable_malloc_check"

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 "COMPILER=$compiler" >> $config_mak
echo "CC=$cc" >> $config_mak
echo "CXX=$cxx" >> $config_mak
echo "CFLAGS=$CFLAGS" >> $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
echo "CURL_CFLAGS=$CURL_CFLAGS" >> $config_mak
echo "CURL_LDFLAGS=$CURL_LDFLAGS" >> $config_mak
if test "$build_biarch" = "yes"; then
echo "LDFLAGS_32=-m32" >> $config_mak
echo "CFLAGS_32=$CFLAGS_32" >> $config_mak
else
echo 'CFLAGS_32=$(CFLAGS)' >> $config_mak
fi
if test "$host_os" = "linux"; then
    echo "OS=linux" >> $config_mak
    echo "#define HOST_LINUX 1" >> $config_h
    echo "#define HOST_OS \"linux\"" >> $config_h
elif test "$host_os" = "dragonfly"; then
    echo "OS=dragonfly" >> $config_mak
    echo "#define HOST_DRAGONFLY 1" >> $config_h
    echo "#define HOST_OS \"dragonfly\"" >> $config_h
elif test "$host_os" = "freebsd"; then
    echo "OS=freebsd" >> $config_mak
    echo "#define HOST_FREEBSD 1" >> $config_h
    echo "#define HOST_OS \"freebsd\"" >> $config_h
elif test "$host_os" = "netbsd"; then
    echo "OS=netbsd" >> $config_mak
    echo "#define HOST_NETBSD 1" >> $config_h
    echo "#define HOST_OS \"netbsd\"" >> $config_h
elif test "$host_os" = "solaris"; then
    echo "OS=solaris" >> $config_mak
    echo "#define HOST_SOLARIS 1" >> $config_h
    echo "#define HOST_OS \"solaris\"" >> $config_h
else
    echo "Unsupported OS"
    exit 1
fi
if test "$host_cpu" = "i386" ; then
    echo "ARCH=i386" >> $config_mak
    echo "#define HOST_I386 1" >> $config_h
    echo "#define HOST_ARCH \"i386\"" >> $config_h
elif test "$host_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 "$host_cpu" = "ppc" ; then
    echo "ARCH=ppc" >> $config_mak
    echo "#define HOST_PPC 1" >> $config_h
    echo "#define HOST_ARCH \"ppc\"" >> $config_h
elif test "$host_cpu" = "ppc64" ; then
    echo "ARCH=ppc64" >> $config_mak
    echo "#define HOST_PPC64 1" >> $config_h
    echo "#define HOST_ARCH \"ppc64\"" >> $config_h
elif test "$host_cpu" = "sparc" ; then
    echo "ARCH=sparc" >> $config_mak
    echo "#define HOST_SPARC 1" >> $config_h
    echo "#define HOST_ARCH \"sparc\"" >> $config_h
elif test "$host_cpu" = "sparc64" ; then
    echo "ARCH=sparc64" >> $config_mak
    echo "#define HOST_SPARC64 1" >> $config_h
    echo "#define HOST_ARCH \"sparc64\"" >> $config_h
elif test "$host_cpu" = "ia64" ; then
    echo "ARCH=ia64" >> $config_mak
    echo "#define HOST_IA64 1" >> $config_h
    echo "#define HOST_ARCH \"ia64\"" >> $config_h
elif test "$host_cpu" = "arm" ; then
    echo "ARCH=arm" >> $config_mak
    echo "#define HOST_ARM 1" >> $config_h
    echo "#define HOST_ARCH \"arm\"" >> $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 -n "$fbigendian" ; then
    echo "#define FLOAT_WORDS_BIGENDIAN $fbigendian" >> $config_h
fi

echo "SRC_PATH=$source_path" >> $config_mak
echo "build_viewer=$build_viewer" >> $config_mak
echo "build_player=$build_player" >> $config_mak
echo "build_biarch=$build_biarch" >> $config_mak
echo "lib32=$lib32" >> $config_mak
echo "lib64=$lib64" >> $config_mak
echo "prefix=$prefix" >> $config_mak
echo "bindir=$prefix/bin" >> $config_mak
libdir="$prefix/$lib64"
echo "libdir=$libdir" >> $config_mak
echo "#define LIB \"$lib64\"" >> $config_h
echo "#define LIBDIR \"$libdir\"" >> $config_h
echo "x11prefix=$x_base_dir" >> $config_mak
echo "ALLOW_STRIP=$strip" >> $config_mak

echo "VERSION=$VERSION" >> $config_mak
echo "SVNDATE=$SVNDATE" >> $config_mak
echo "SNAPSHOT=$SNAPSHOT" >> $config_mak
echo "#define NPW_SNAPSHOT $SNAPSHOT" >> $config_h
if test $SNAPSHOT -ge 2; then
    echo "#define NPW_VERSION \"$VERSION-Pre ($SVNDATE)\"" >> $config_h
else
    echo "#define NPW_VERSION \"$VERSION\"" >> $config_h
fi

echo "pkglibdir=$pkglibdir" >> $config_mak
echo "#define NPW_LIBDIR \"$pkglibdir\"" >> $config_h

# check if we want to install files in ARCH/OS specific locations
if echo ":$viewer_paths" | $EGREP -q ":$pkglibdir/%(ARCH|OS)%"; then
    npw_common_libdir="$pkglibdir/noarch"
    npw_host_libdir="$pkglibdir/$host_cpu/$host_os"
    npw_target_libdir="$pkglibdir/$target_cpu/$target_os"
    npw_target_libdir_var="$pkglibdir/\\\$\$TARGET_ARCH/\\\$\$TARGET_OS"
else
    npw_common_libdir="$pkglibdir"
    npw_host_libdir="$npw_common_libdir"
    npw_target_libdir="$npw_common_libdir"
    npw_target_libdir_var="$npw_common_libdir"
fi
echo "npcommondir=$npw_common_libdir" >> $config_mak
echo "nphostdir=$npw_host_libdir" >> $config_mak
echo "nptargetdir=$npw_target_libdir" >> $config_mak
echo "nptargetdir_var=$npw_target_libdir_var" >> $config_mak
echo "#define NPW_HOST_LIBDIR \"$npw_host_libdir\"" >> $config_h
echo "#define NPW_TARGET_LIBDIR \"$npw_target_libdir\"" >> $config_h
echo "#define NPW_VIEWER_PATHS \"$viewer_paths\"" >> $config_h

echo "#define RPC_INIT_TIMEOUT $rpc_init_timeout" >> $config_h

for mh in $malloc_hooks; do
    echo "#define USE_MALLOC_`echo $mh | tr '[a-z]' '[A-Z]'` 1" >> $config_h
done
echo "#define DEFAULT_MALLOC_LIB $malloc_hook_default" >> $config_h

if test "$has_visibility_attribute" = "yes"; then
    echo "#define attribute_hidden __attribute__((visibility(\"hidden\")))" >> $config_h
    echo "#define attribute_protected __attribute__((visibility(\"protected\")))" >> $config_h
else
    echo "#define attribute_hidden" >> $config_h
    echo "#define attribute_protected" >> $config_h
fi
if test "$has_initfini_array" = "yes"; then
    echo "#define HAVE_INITFINI_ARRAY 1" >> $config_h
else
    echo "#undef HAVE_INITFINI_ARRAY" >> $config_h
fi
if test "$has_sockaddr_un_sun_len" = "yes"; then
    echo "#define HAVE_SOCKADDR_UN_SUN_LEN 1" >> $config_h
else
    echo "#undef HAVE_SOCKADDR_UN_SUN_LEN" >> $config_h
fi
if test "$libc_provides_ssp" = "yes"; then
    echo "#define TARGET_LIBC_PROVIDES_SSP 1" >> $config_h
else
    echo "#undef TARGET_LIBC_PROVIDES_SSP" >> $config_h
fi
if test "$enable_thread_check" = "yes"; then
    echo "#define ENABLE_THREAD_CHECK 1" >> $config_h
else
    echo "#undef ENABLE_THREAD_CHECK" >> $config_h
fi
if test "$enable_malloc_check" = "yes"; then
    echo "#define ENABLE_MALLOC_CHECK 1" >> $config_h
else
    echo "#undef ENABLE_MALLOC_CHECK" >> $config_h
fi

# check for functions in <glib.h>
for func in g_hash_table_remove_all g_hash_table_find; do
    cat > $TMPC << EOF
extern void $func(void);
int main(void) { $func(); return 0; }
EOF
    if $cc $TMPC -o $TMPE $GLIB_CFLAGS $GLIB_LDFLAGS > /dev/null 2>&1; then
        func_def=`echo "$func" | tr '[:lower:]./-' '[:upper:]___'`
        echo "#define HAVE_$func_def 1" >> $config_h
    fi
    rm -f $TMPC $TMPE
done

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 "$build_generic" = "yes"; then
    echo "#define BUILD_GENERIC 1" >> $config_h
else
    echo "#undef BUILD_GENERIC" >> $config_h
fi

if test "$target_os" = "linux"; then
    echo "TARGET_OS=linux" >> $config_mak
    echo "#define TARGET_OS \"linux\"" >> $config_h
    echo "#define TARGET_LINUX 1" >> $config_h
elif test "$target_os" = "solaris"; then
    echo "TARGET_OS=solaris" >> $config_mak
    echo "#define TARGET_OS \"solaris\"" >> $config_h
    echo "#define TARGET_SOLARIS 1" >> $config_h
else
    echo "Unsupported target OS"
    exit 1
fi

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" = "x86_64" ; then
    echo "TARGET_ARCH=x86_64" >> $config_mak
    echo "#define TARGET_ARCH \"x86_64\"" >> $config_h
    echo "#define TARGET_X86_64 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
fi
