#! /bin/sh

if [ -n "$DOSEMU_DEFAULT_CONFIGURE" ] ; then
  exit 0
fi

CONF_FILE=`dirname $0`/compiletime-settings

if [ "$1" != "" ]; then
  if [ "${1#--}" = "$1" -a "${1#*=}" = "$1" ]; then
    CONF_FILE=$1
    if [ "$CONF_FILE" = "-d" ]; then
      CONF_FILE=`dirname $0`/compiletime-settings.devel
    fi
    shift
  fi
fi

if [ ! -f $CONF_FILE ]; then
  echo "$CONF_FILE cannot be opened"
  exit 1
fi

SUFFIX=""
while [ "$1" != "" ]; do
  SUFFIX="$SUFFIX \"$1\""
  shift
done

CONF=`cat $CONF_FILE`
CONF=`echo -n "$CONF"| sed '/^config {/d' | sed '/^}/d' | tr '\n' ' ' `

set $CONF

#echo "$*"

STRING=""
PLUGIN=""

while [ "$1" != "" ]; do
#  echo "$1 $2"

  case "$1" in
    experimental)
      if [ "$2" = "on" ]; then STRING="$STRING --enable-experimental"; fi
      ;;
    debug)
      if [ "$2" = "on" ]; then STRING="$STRING --enable-debug"; fi
      ;;
    net)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-net"; fi
      ;;
    x)
      if [ "$2" = "off" ]; then STRING="$STRING --without-x"; fi
      ;;
    mitshm)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-mitshm"; fi
      ;;
    vidmode)
      if [ "$2" = "off" ]; then STRING="$STRING --without-vidmode"; fi
      ;;
    sbemu)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-sbemu"; fi
      ;;
    cpuemu)
      if [ "$2" = "on" ]; then STRING="$STRING --enable-cpuemu"; fi
      ;;
    aspi)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-aspi"; fi
      ;;
    svgalib)
      if [ "$2" = "on" ]; then STRING="$STRING --with-svgalib"; fi
      ;;
    gpm)
      if [ "$2" = "off" ]; then STRING="$STRING --without-gpm"; fi
      ;;
    dlplugins)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-dlplugins"; fi
      ;;
    alsa)
      if [ "$2" = "off" ]; then STRING="$STRING --without-alsa"; fi
      ;;
    sndfile)
      if [ "$2" = "off" ]; then STRING="$STRING --without-sndfile"; fi
      ;;
    docdir|x11fontdir|syshdimagedir|fdtarball)
      STRING="$STRING --with-$1=$2"
      ;;
    mandir|datadir|libdir|sysconfdir|bindir|prefix)
      STRING="$STRING --$1=`echo $2`"
      ;;
    target_bits)
      if [ "$2" != "auto" ]; then
	  if uname -m | grep -q 64; then
	      otherbits=64
	      if cpp -dM /dev/null | grep -q __x86_64__; then
		  otherbits=32
	      fi
	      if [ "$2" = "$otherbits" ]; then
		  STRING="$STRING --with-target-bits=$otherbits";
	      fi
	  elif [ "$2" = "64" ]; then
	      # cross compilation on pure 32-bit machines
	      STRING="$STRING --with-target-bits=64 --host=x86_64-pc-linux-gnu";
	  fi
      fi
      ;;
    target_cpu)
      if [ "$2" != "auto" ]; then STRING="$STRING --with-target-cpu=$2"; fi
      ;;
    plugin_*)
      plugin=`expr substr $1 8 100`
      if [ "$2" = "on" ]; then PLUGIN="$PLUGIN $plugin yes"; fi
      if [ "$2" = "off" ]; then PLUGIN="$PLUGIN $plugin no"; fi
      ;;
    *)
      ;;
  esac

  shift; shift;
done

echo `dirname $0`/mkpluginhooks enable $PLUGIN
`dirname $0`/mkpluginhooks enable $PLUGIN

eval "set -- $SUFFIX"
export DOSEMU_DEFAULT_CONFIGURE=1
echo exec `dirname $0`/configure $STRING "$@"
exec `dirname $0`/configure $STRING "$@"
