#!/bin/sh
curdir=`dirname $0`

OS=`uname -s`
case "$OS" in
    Linux)
        CPU=`uname -m`
        case "$CPU" in
            i386|i486|i586|i686) 
                CPU="i386"
                ;;
            x86_64|amd64)
                CPU="x86_64"
                # if the kernel release contains 'mixed64', we have a 64-bit kernel
                # with a 32-bit environment. We should generate i386 binaries then
                kernel_mixed=`uname -r | grep mixed64`
                if [ -n "$kernel_mixed" ] ; then
                    CPU="i386"
                fi
                # another specific case
                kernel_uXen=`uname -r | grep "gg.*-xenU"`
                if [ -n "$kernel_uXen" ] ; then
                    CPU="i386"
                fi
                ;;
        esac
        ;;
    Darwin)
        CPU=`uname -p`
        if [ "$CPU" = "i386" ] ; then
            OS=darwin-x86
        else
            OS=darwin-ppc
        fi
        ;;
    CYGWIN*)
        echo "Do not compile this library with Cygwin, use MSYS instead !!"
        exit 2
        ;;
    *_NT-*)
        OS=windows
        ;;
esac


PREFIX=
for opt do
  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  case "$opt" in
  --prefix=*) PREFIX=$optarg
  ;;
  esac
done

if [ -z "$PREFIX" ] ; then
    if [ -z "$ANDROID_PRODUCT_OUT" ] ; then
        echo "Please use the --prefix=<path> option to indicate where you want"
        echo "to install the final library binaries."
        exit 3
    fi
    TOP=`dirname $ANDROID_PRODUCT_OUT`
    TOP=`dirname $TOP`
    TOP=`dirname $TOP`
    TOP=`dirname $TOP`
    if [ ! -f $TOP/config/envsetup.make ] ; then
        echo "Your ANDROID_PRODUCT_OUT is defined but doesn't point to an Android source tree."
        echo "Please make sure that you have your Android build setup properly."
        exit 3
    fi
    PREBUILT=$TOP/prebuilt/$OS
    # the following test is mainly used for prebuilt/Linux-x86_64
    if [ -d "$PREBUILT-$CPU" ] ; then
        PREBUILT=$PREBUILT-$CPU
    fi
    PREFIX=$PREBUILT/sdl
fi

# list of enabled SDL modules, we absolutely require these
ENABLED=""

# list of disabled SDL modules, since we're generating a static library
DISABLED="shared rpath diskaudio video-directfb video-svga video-fbcon video-aalib cdrom joystick timer"

if [ "$OS" = darwin-x86 -o "$OS" = darwin-ppc ] ; then
  DISABLED="$DISABLED video-x11"
else
  DISABLED="$DISABLED file"
fi

if [ "$OS" = windows ] ; then
    # on Windows, disable C library support, otherwise a library compiled with MSys (mingw32)
    # cannot be properly linked on Cygwin
    DISABLED="$DISABLED video-directx libc"
fi

if [ "$OS" = Linux ] ; then
    DISABLED="$DISABLED nas arts pulseaudio"
    ENABLED="$ENABLED alsa esd video-x11"
fi

ALL_DISABLED=""
for  dd in $DISABLED; do
    ALL_DISABLED="$ALL_DISABLED --disable-$dd"
done

echo "$curdir/configure --prefix=$PREFIX $ALL_DISABLED"
$curdir/configure --prefix=$PREFIX $ALL_DISABLED
