#!/bin/sh
#
# xc configure script (c) 2008 Denis Barthou, 
# from tcc configure script, (c) 2003 Fabrice Bellard
#
# set temporary file name

version=0.1.1

# default parameters
prefix=""
execprefix=""
bindir=""
libdir=""
includedir=""
mandir=""
cross_prefix=""
cc="gcc"
host_cc="gcc"
ar="ar"
make="make"
cpu="x86"
tccdir=""
bigendian="no"
LIBSUF=".a"
EXESUF=""
pl=pl
plinclude=/usr/local/include
# OS specific
targetos=`uname -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

for opt do
  case "$opt" in
  --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --exec-prefix=*) execprefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --bindir=*) bindir=`echo $opt | cut -d '=' -f 2`
  ;;
  --libdir=*) libdir=`echo $opt | cut -d '=' -f 2`
  ;;
  --includedir=*) includedir=`echo $opt | cut -d '=' -f 2`
  ;;
  --mandir=*) mandir=`echo $opt | cut -d '=' -f 2`
  ;;
  --cc=*) cc=`echo $opt | cut -d '=' -f 2`
  ;;
  --make=*) make=`echo $opt | cut -d '=' -f 2`
  ;;
  --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
  ;;
  --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
  ;;
  --extra-libs=*) extralibs=${opt#--extra-libs=}
  ;;
  --pl-includedir=*) plinclude=${opt#--pl-includedir=}
  ;;
  esac
done

# Checking for CFLAGS
if test -z "$CFLAGS"; then
    CFLAGS="-O2"
fi

bigendian="no"

# check gcc version
cat > /tmp/.tmp0 <<EOF
int main(void) {
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)
return 0;
#else
#error gcc < 3.2
#endif
}
EOF

if ! ( $pl --version > /dev/null 2> /dev/null ) ; then
    echo "SWI Prolog             no version detected"
    echo "** SWI Prolog is not installed."
    echo "** Install it and put pl in your execution path."
    exit
fi

plname=`$pl --version|awk '{print $1}'`
if ! [ x"$plname" = x"SWI-Prolog" ] ; then
    echo "SWI Prolog             no version detected"
    echo "** SWI Prolog is not installed."
    echo "** Install it and put pl in your execution path."
    exit
fi

echo "SWI Prolog            installed"

${pl} --dump-runtime-variables > /tmp/.tmp1
. /tmp/.tmp1
pllib=${PLBASE}/lib/${PLARCH}
plinclude=${PLBASE}/include
if ! [ -e ${pllib}/libpl.so ] ; then
    echo "pllib is shared        no"
    echo "** PL should be compiled with flag --enable-shared."
    echo "** Recompile pl and reconfigure xc."
    exit
fi
echo "pllib is shared       yes"

gcc_major="2"
if $cc -o $TMPO $TMPC 2> /dev/null ; then
   gcc_major="3"
fi

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 "  --exec-prefix=EPREFIX    install architecture-dependent files in EPREFIX"
echo "                           [same as prefix]"
echo "  --bindir=DIR             user executables in DIR [EPREFIX/bin]"
echo "  --libdir=DIR             object code libraries in DIR [EPREFIX/lib]"
echo "  --includedir=DIR         C header files in DIR [PREFIX/include]"
echo "  --mandir=DIR             man documentation in DIR [PREFIX/man]"
echo ""
echo "Advanced options (experts only):"
echo "  --cc=CC                  use C compiler CC [$cc]"
echo "  --make=MAKE              use specified make [$make]"
echo ""
exit 1
fi

if test -z "$prefix" ; then
    prefix="/usr/local"
fi
if test x"$execprefix" = x""; then
    execprefix="${prefix}"
fi
if test x"$tccdir" = x""; then
tccdir="${execprefix}/lib/xc"
fi
if test x"$bindir" = x""; then
    bindir="${execprefix}/bin"
fi
if test x"$docdir" = x""; then
    docdir="$prefix/share/doc/tcc"
fi

if test x"$libdir" = x""; then
libdir="${execprefix}/lib"
fi
if test x"$mandir" = x""; then
mandir="${prefix}/man"
fi

echo "Binary  directory     $bindir"
echo "Library directory     $libdir"
echo "Include directory     $includedir"
echo "PL include directory  $plinclude"
echo "PL lib directory      $pllib"

echo "Creating config.mak and config.h"

echo "# Automatically generated by configure - do not modify" > config.mak
echo "/* Automatically generated by configure - do not modify */" > config.h

${pl} --dump-runtime-variables |sed -e 's/;//' -e 's/\"//g' >> config.mak
echo "prefix=$prefix" >> config.mak
echo "bindir=$bindir" >> config.mak
echo "tccdir=$tccdir" >> config.mak
echo "libdir=$libdir" >> config.mak
echo "includedir=$includedir" >> config.mak
echo "mandir=$mandir" >> config.mak
echo "docdir=$docdir" >> config.mak
echo "MAKE=$make" >> config.mak
echo "CC=$cc" >> config.mak
echo "GCC_MAJOR=$gcc_major" >> config.mak
echo "#define CONFIG_TCCDIR \"$tccdir\"" >> config.h
echo "#define GCC_MAJOR $gcc_major" >> config.h
echo "CFLAGS=$CFLAGS -I$plinclude" >> config.mak
#echo "LDFLAGS=$LDFLAGS -L$pllib" >> config.mak
echo "LIBSUF=$LIBSUF" >> config.mak
echo "EXESUF=$EXESUF" >> config.mak
echo "ARCH=i386" >> config.mak
echo "PL=$pl" >> config.mak
echo "#define HOST_I386 1" >> config.h
if test "$bigendian" = "yes" ; then
  echo "WORDS_BIGENDIAN=yes" >> config.mak
  echo "#define WORDS_BIGENDIAN 1" >> config.h
fi
echo "VERSION=$version" >>config.mak
echo "#define XC_VERSION \"$version\"" >> config.h
