#! /bin/sh

srcdir="`dirname "$0" | xargs readlink -f`"
CONF_FILE=compiletime-settings
if [ ! -f configure.ac ]; then
    [ -f $CONF_FILE ] || cp "$srcdir"/$CONF_FILE .
    [ -f $CONF_FILE.devel ] || cp "$srcdir"/$CONF_FILE.devel .
fi

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

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

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

SUFFIX=""
while [ "$#" != "0" ]; do
  case "$1" in
    --enable-plugin*)
      EXTRA_PLUGINS="$EXTRA_PLUGINS,${1#*=}"
      ;;
    --*)
      SUFFIX="$SUFFIX $1"
      ;;
    *)
      CONF="$CONF `echo $1 | tr '=' ' '`"
      ;;
    esac
    shift
done

set $CONF

#echo "$*"

STRING=""
PLUGIN=`cat $srcdir/plugin_list | sed 's/$/,/g' | tr -d '\n' | sed 's/,$//'`

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
      ;;
    asan)
      if [ "$2" = "on" ]; then STRING="$STRING --enable-asan"; fi
      ;;
    lsan)
      if [ "$2" = "on" ]; then STRING="$STRING --enable-lsan"; fi
      ;;
    tsan)
      if [ "$2" = "on" ]; then STRING="$STRING --enable-tsan"; fi
      ;;
    ubsan)
      if [ "$2" = "on" ]; then STRING="$STRING --enable-ubsan"; fi
      ;;
    optimize)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-optimization"; fi
      ;;
    system-wa)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-system-wa"; fi
      ;;
    cpuemu)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-cpuemu"; fi
      ;;
    dlplugins)
      if [ "$2" = "off" ]; then STRING="$STRING --disable-dlplugins"; fi
      ;;
    x11fontdir)
      STRING="$STRING --with-$1=$2"
      ;;
    fdtarball)
      STRING="$STRING --with-$1=$srcdir/$2"
      ;;
    fdpp-build-path)
      FDPP_STR="$FDPP_STR --with-$1=$2"
      ;;
    fdpp-include-path)
      FDPP_STR="$FDPP_STR --with-$1=$2"
      ;;
    fdpp)
      if [ "$2" = "off" ]; then
        STRING="$STRING --disable-fdpp";
        PLUGIN=`echo "$PLUGIN" | sed -e s/fdpp//`;
      fi
      ;;
    docdir|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=${1#plugin_}
      if [ "$2" = "on" ]; then PLUGIN="$PLUGIN,$plugin"; fi
      if [ "$2" = "off" ]; then
          PLUGIN=`echo "$PLUGIN" | sed -e s/$plugin// -e s/,,/,/`;
      fi
      ;;
    *)
      ;;
  esac

  shift; shift;
done

[ -f "$srcdir"/install-sh ] || "$srcdir"/autogen.sh || exit $?
if [ -z "$MAKE" ]; then
  if [ `uname -s` = "FreeBSD" ]; then
    MAKE=gmake
  else
    MAKE=make
  fi
fi
if [ `uname -s` = "FreeBSD" ]; then
  export CPPFLAGS="${CPPFLAGS} -I/usr/local/include"
  export LDFLAGS="${LDFLAGS} -L/usr/local/lib"
fi
${MAKE} -C "$srcdir" configure

STRING="$STRING --enable-plugins=$PLUGIN,$EXTRA_PLUGINS"
if [ -n "$FDPP_STR" ]; then
  FDPP_STR=`echo $FDPP_STR | sed 's/^ *//'`
  STRING="$STRING --with-plugin-options=fdpp,\"$FDPP_STR\""
fi

echo exec "$srcdir"/configure $STRING $SUFFIX
eval exec "$srcdir"/configure $STRING $SUFFIX
