#!/usr/bin/env bash

# The contents of this file are subject to the MonetDB Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is the MonetDB Database System.
#
# The Initial Developer of the Original Code is CWI.
# Portions created by CWI are Copyright (C) 1997-2005 CWI.
# All Rights Reserved.

#
# ! this file should be kept identical in                                                         !
# ! MonetDB, template, sql, pathfinder,  xml, pruning, playpen, times100, misq, gis, acoi, monet5 !
#

# helps bootstrapping MonetDB, when checked out from CVS
# requires GNU autoconf, automake and libtool
# this is not meant to go into the distributions


if [ -x src/utils/autogen/autogen.py ] ; then
	AUTOGEN=src/utils/autogen/autogen.py 
	CONFDIR=
 else
	if [ -z "$MONETDB_PREFIX" -a -n "$MONETDIST" ] ; then
		MONETDB_PREFIX=$MONETDIST
	fi
	if [ "$MONETDB_PREFIX" = "" ]; then
		type -p monetdb-config > /dev/null 2>&1
		ERROR=$?
		if [ $ERROR -eq 0 ]; then
			MONETDB_PREFIX="`monetdb-config --prefix`"
		else
			echo "!ERROR: requires MONETDB_PREFIX to be set or monetdb-config in your PATH"
			exit 1
		fi
	fi

	#Some variables autogen needs to be able to expand
	#to get the dependencies right.
	export MONETDB_INCLUDEDIR="`$MONETDB_PREFIX/bin/monetdb-config --pkgincludedir`"
	export MONETDB_INCS="`$MONETDB_PREFIX/bin/monetdb-config --includes`"

	AUTOGEN=$MONETDB_PREFIX/lib/autogen/autogen.py
	CONFDIR="-I `$MONETDB_PREFIX/bin/monetdb-config --pkgdatadir`/conf"
fi

if [ "`uname`" = Darwin  -a  "`type -p glibtool 2>/dev/null`"  -a  "`type -p glibtoolize 2>/dev/null`" ] ; then
	LIBTOOL=glibtool
	LIBTOOLIZE=glibtoolize
	echo "using `type -p glibtool 2>/dev/null` and `type -p glibtoolize 2>/dev/null`."
  else
	LIBTOOL=libtool
	LIBTOOLIZE=libtoolize
fi


find . -name .incs.\* \-exec rm {} \;
rm -rf autom4*.cache


# temporarly create fake-configure.in to give autoconf-wrappers a hint, which version we need:
cat << "EOF" > configure.in
AC_PREREQ(2.57)
AC_INIT(src/utils/Mx/Mx.h)
EOF


REQ_AM_VERSION="1.5"
NEW_AM_VERSION="1.7"
if [ ! "`type -p automake`" ]; then
	echo "no automake found."
	echo "We require (at least) automake $REQ_AM_VERSION."
	echo "Please get it at eg. http://www.gnu.org/software/automake/."
	exit 1
fi
AM_VERSION=`automake --version 2>/dev/null | sed -n -e '/^automake/{s/.* //g;s/-.*//g;p;q;}'`
_AM_VERSION=`echo $AM_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
_REQ_AM_VERSION=`echo $REQ_AM_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
_NEW_AM_VERSION=`echo $NEW_AM_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`

if [ "$_AM_VERSION" -ge "$_REQ_AM_VERSION" ]; then
	echo "automake $AM_VERSION is $REQ_AM_VERSION or newer. Good."
elif [ "$_AM_VERSION" -lt "$_REQ_AM_VERSION" ]; then
	echo "automake $AM_VERSION is older than $REQ_AM_VERSION."
	echo "We require (at least) automake $REQ_AM_VERSION."
	echo "Please get a newer version (cf. http://www.gnu.org/software/automake/)."
	exit 1
fi


REQ_AC_VERSION="2.57"
if [ ! "`type -p autoconf`" ]; then
	echo "no autoconf found."
	echo "We require (at least) autoconf $REQ_AC_VERSION."
	echo "Please get it at eg. http://www.gnu.org/software/autoconf/."
	exit 1
fi
AC_VERSION=`autoconf --version 2>/dev/null | sed -n -e '/^[Aa]utoconf/{s/.* //g;s/-.*//g;p;q;}'`
_AC_VERSION=`echo $AC_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
_REQ_AC_VERSION=`echo $REQ_AC_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`

if [ "$_AC_VERSION" -ge "$_REQ_AC_VERSION" ]; then
	echo "autoconf $AC_VERSION is $REQ_AC_VERSION or newer. Good."
elif [ "$_AC_VERSION" -lt "$_REQ_AC_VERSION" ]; then
	echo "autoconf $AC_VERSION is older than $REQ_AC_VERSION."
	echo "We require (at least) autoconf $REQ_AC_VERSION."
	echo "Please get a newer version (cf. http://www.gnu.org/software/autoconf/)."
	exit 1
fi


REQ_LT_VERSION="1.4"
if [ ! "`type -p $LIBTOOL`" ]; then
	echo "no libtool found."
	echo "We require (at least) libtool $REQ_LT_VERSION."
	echo "Please get it at eg. http://www.gnu.org/software/libtool/."
	exit 1
fi
LT_VERSION=`$LIBTOOL --version 2>/dev/null | sed -n '/^ltmain\.sh/{s/([^)]*)//g;s/ltmain.sh//g;s/ //g;p;q;}'`
_LT_VERSION=`echo $LT_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
_REQ_LT_VERSION=`echo $REQ_LT_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`

if [ "$_LT_VERSION" -ge "$_REQ_LT_VERSION" ]; then
	echo "libtool $LT_VERSION is $REQ_LT_VERSION or newer. Good."
elif [ "$_LT_VERSION" -lt "$_REQ_LT_VERSION" ]; then
	echo "libtool $LT_VERSION is older than $REQ_LT_VERSION."
	echo "We require (at least) libtool $REQ_LT_VERSION."
	echo "Please get a newer version (cf. http://www.gnu.org/software/libtool/)."
	exit 1
fi


REQ_PY_VERSION="2.0.0"
if [ ! "`type -p python`" ]; then
	echo "no python found."
	echo "We require (at least) Python $REQ_PY_VERSION."
	echo "Please get a newer version (cf. http://www.python.org/)."
	exit 1
fi
PY_VERSION=`python -V 2>&1 | sed -n -e '/^[Pp]ython/{s/.* //g;s/-.*//g;p;q;}'`
_PY_VERSION=`echo $PY_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
_REQ_PY_VERSION=`echo $REQ_PY_VERSION | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`

if [ "$_PY_VERSION" -ge "$_REQ_PY_VERSION" ]; then
	echo "Python $PY_VERSION is $REQ_PY_VERSION or newer. Good."
elif [ "$_PY_VERSION" -lt "$_REQ_PY_VERSION" ]; then
	echo "Python $PY_VERSION is older than $REQ_PY_VERSION."
	echo "We require (at least) Python $REQ_PY_VERSION."
	echo "Please get a newer version (cf. http://www.python.org/)."
	exit 1
fi


$AUTOGEN `pwd` $_AM_VERSION

rm -f acconfig.h
cp configure.ag configure.in

echo "AC_OUTPUT(" >> configure.in
cat acout.in  >> configure.in
echo ")" >> configure.in


$LIBTOOLIZE -c -f

# default deplibs_check_method (pass all libs)
# needed since deplibs is switched off in libtool 1.3c (we cannot wait for 1.4)
test -f conf/ltconfig && perl -i -p -e  's/&& deplibs_check_method=unknown/&& deplibs_check_method=pass_all/g' conf/ltconfig


# in case automake was installed after libtool:
# give aclocal a hint, where to find libtool.m4
LT_M4_DIR=""
d="`type -p $LIBTOOL | sed 's|/bin/'"$LIBTOOL"'$||'`/share/aclocal"
if [ -d "$d" ] ; then
	LT_M4_DIR="-I $d"
fi

# make sure, aclocal finds pkg.m4 even if pkg-config is not installed in /usr/;
# requires that location of pkg-config is in PATH
PKG_M4_DIR=""
d="`type -p pkg-config | sed 's|/bin/pkg-config$||'`/share/aclocal"
if [ -d "$d" ] ; then
	PKG_M4_DIR="-I $d"
fi

aclocal -I . -I conf $LT_M4_DIR $PKG_M4_DIR $CONFDIR

# patch aclocal.m4 to call the linker with "-IPA" on IRIX (medusa)
# (just adding -IPA to LDFLAGS somehow doesn't help ...)
perl -i -p -e 's|(LD-ld. )|$1-IPA |g' aclocal.m4

# patch aclocal.m4 for cygpath on CYGWIN
grep -q '"$CYGPATH_W"' aclocal.m4 && cat << "EOF" | patch aclocal.m4
--- aclocal.m4.ORG      2004-09-07 18:32:18.593750000 +0200
+++ aclocal.m4  2004-09-07 18:34:05.453125000 +0200
@@ -6386,11 +6386,14 @@
 if test -z "$CYGPATH_W"; then
   if (cygpath --version) >/dev/null 2>/dev/null; then
     CYGPATH_W='cygpath -w'
+    CYGPATH_WP='cygpath -wp'
   else
     CYGPATH_W=echo
+    CYGPATH_WP=echo
   fi
 fi
 AC_SUBST([CYGPATH_W])
+AC_SUBST([CYGPATH_WP])
 
 # Define the identity of the package.
 dnl Distinguish between old-style and new-style calls.
EOF

# older versions of automake/aclocal don't know about the
# Intel compiler on Linux (icc/ecc), yet...
if [ "$_AM_VERSION" -lt "$_NEW_AM_VERSION" ]; then

echo "automake/aclocal $AM_VERSION is older than $NEW_AM_VERSION."
echo "Patching aclocal.m4 for Intel compiler on Linux (icc/ecc)."

# patch aclocal.m4: 
# set some correct switches for Intel compiler on Linux (icc/ecc)
#grep 'lt_cv_prog_cc_can_build_shared' aclocal.m4 >/dev/null && 
cat << "EOF" | patch aclocal.m4
--- aclocal.m4~	Wed Jan 16 20:34:17 2002
+++ aclocal.m4	Wed Jan 16 20:37:55 2002
@@ -2542,8 +2542,17 @@
       ;;
 
-    *)
-      lt_cv_prog_cc_can_build_shared=no
-      ;;
-    esac
+    linux*)
+      lt_cv_prog_cc_pic='-Kpic'
+      case "$CC" in
+        pgcc*)	lt_cv_prog_cc_static='-Bstatic';;
+        *)	lt_cv_prog_cc_static='-static';;
+      esac
+      lt_cv_prog_cc_wl='-Wl,'
+      ;;
+
+    *)
+      lt_cv_prog_cc_can_build_shared=no
+      ;;
+    esac
   fi
 ])
EOF

# patch aclocal.m4: 
# fix checks "if icc/ecc supports -c -o file.{o,lo}"
# and "whether -lc should be explicitly linked in"
cat << "EOF" | patch aclocal.m4
--- aclocal.m4.OK	Thu Jul  4 17:51:11 2002
+++ aclocal.m4	Thu Jul  4 17:58:21 2002
@@ -1184,7 +1184,7 @@
 if { (eval echo configure:__oline__: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>out/conftest.err; } && test -s out/conftest2.$ac_objext; then
   # The compiler can only warn and ignore the option if not recognized
   # So say no if there are warnings
-  if test -s out/conftest.err; then
+  if test -s out/conftest.err && test "`grep -v "^conftest.$ac_ext\$" out/conftest.err`" ; then
     lt_cv_compiler_c_o=no
   else
     lt_cv_compiler_c_o=yes
@@ -2444,7 +2444,7 @@
 
 AC_LIBTOOL_DLOPEN_SELF
 
-if test "$enable_shared" = yes && test "$GCC" = yes; then
+if test "$enable_shared" = yes && ( test "$GCC" = yes || test "$CC" = icc || test "$CC" = ecc ) ; then
   case $archive_cmds in
   *'~'*)
     # FIXME: we may have to deal with multi-command sequences.
@@ -2464,7 +2464,10 @@
       libobjs=conftest.$ac_objext
       deplibs=
       wl=$lt_cv_prog_cc_wl
-      compiler_flags=-v
+      if ( test "$CC" = icc || test "$CC" = ecc )
+        then  compiler_flags=-dryrun
+        else  compiler_flags=-v
+      fi
       linker_flags=-v
       verstring=
       output_objdir=.
EOF

fi # [ "$_AM_VERSION" -lt "$_NEW_AM_VERSION" ]


autoheader
automake -a -c --foreign

#autoupdate
autoconf


exit 0
