#!/bin/sh

PACKAGES=`pwd`
cd ../..
GSDLHOME=`pwd`
cd $PACKAGES

GSDLOS=`uname -s |tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
# check for running bash under cygwin
if test "`echo $GSDLOS | sed 's/cygwin//'`" != "$GSDLOS" ;
then
 GSDLOS=windows
fi
if test "x$crossOS" != "x" ; then
  # Override derived value with value explicitly provided in crossOS
  GSDLOS=$crossOS
fi

prefix=""
bindir=""
HOSTTARGETFLAGS=""

# only some of the packages can take environment variables as options to 
# configure 
# format is VAR=value, e.g "LDFLAGS=-static"
ENVIRONMENT=""

# Java support enabled by default, can switch it off with --disable-java
ENABLE_JAVA=1
# GDBM support enabled by default, can switch it off with --disable-gdbm
USE_GDBM=1
# JDBM support enabled by default, can switch it off with --disable-jdbm
USE_JDBM=1
# SQLite support enabled by default, can switch it off with --disable-sqlite
USE_SQLITE=true
# yaz compilation enabled by default, can switch it off with --disable-yaz
USE_YAZ=true
# z3950 server disabled by default, switch it on with --enable-z3950
USE_Z3950=false
# CORBA support disabled by default, switch it on with --enable-corba
USE_CORBA=false
MICO_DIR=default

# parse any arguments given from toplevel configure
while test $# -ne 0; do
    case "$1" in
	--cache-file=*)
	    cache_filename=`echo $1 | sed s/--cache-file=//`
	    # turn relative path into an absolute path
	    cache_dir=`echo $cache_filename | sed 's@/[^/]*$@@'`
	    cache_dir=`cd $cache_dir 2>/dev/null && pwd`

	    cache_basefile=`echo $cache_filename | sed 's@.*/@@'`
	    cache_file="$cache_dir/$cache_basefile"
	    if test ! -f "$cache_file"; then
		CACHE_FILE=""
	    else
		CACHE_FILE="--cache-file=$cache_file"
	    fi
	    ;;
	--disable-java)
	    ENABLE_JAVA=0
	    USE_JDBM=0
	    ;;
	--disable-gdbm)
	    USE_GDBM=0
	    ;;
	--disable-jdbm)
	    USE_JDBM=0
	    ;;
	--disable-sqlite)
	    USE_SQLITE=false
	    ;;
	--disable-yaz)
	    USE_YAZ=false
	    ;;
	--enable-z3950)
	    USE_Z3950=true
	    ;;
	--enable-corba)
	    USE_CORBA=true
	    ;;
	--with-micodir=*)
	    if test $1 = ""; then
	      $1 = "default"
            fi
	    MICO_DIR=$1
	    ;;       
	--prefix=*)
	    prefix=`echo $1 | sed s/--prefix=//`
	    ;;
	--bindir=*)
	    # where to install binaries to
	    bindir=`echo $1 | sed s/--bindir=//`
	    # turn relative path into an absolute path
	    if test -d "$bindir" ; then
	      bindir=`cd "$bindir" ; pwd `
	      cd "$PACKAGES"
	    elif test -z `echo $bindir | sed '/^\// p;D'` ; then
	      rel_bindir="`echo $bindir | sed s@/.*@@`"
	      cd "$rel_bindir"
	      bindir="`pwd`/`echo $bindir | sed s@^[^/]*/@@`"
	      cd "$PACKAGES"
	    else
	      # bindir doesn't exist, and isn't relative
	      true
	    fi
	    ;;
	--host=* | --target=* | --build=*)
	    HOSTTARGETFLAGS="$HOSTTARGETFLAGS $1"
	    ;;
	*)
	    # ignore all other options for now
	    ;;
    esac
    shift
done

# note! Our GSDL package currently always installs into it's source
# directory, ignoring any --prefix given to configure.
# When this is fixed, uncomment the relevant lines below...

##if test -z "$prefix" ; then
prefix="${GSDLHOME}"
##fi

##if test -z "$bindir" ; then
bindir="${prefix}/bin/$GSDLOS"
##fi

#libdir="${prefix}/lib/$GSDLOS"

# This loads the top configure's cache file, with variables already set.
if test -f "$cache_file" ; then
  . $cache_file
fi

# we assume that gzip and tar are on the search path.
# non-GNU versions of tar don't take the -z option.



# configure expat (needs to be done before XML::Parser)
# now compile expat during the configure stage itself
echo ""
echo "Configuring expat"
echo ""
cd "$PACKAGES/expat"
if test ! -d expat-1.95.8 ;
then
  gzip -dc expat-1.95.8.tar.gz | tar -xf -
fi

cd "$PACKAGES/expat/expat-1.95.8"

if test -e Makefile ; then
  echo make distclean && \
  make distclean
fi

#Configure and compile expat
echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --disable-shared --with-pic --prefix=$PACKAGES/expat --bindir="$bindir" $HOSTTARGETFLAGS $ENVIRONMENT
CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --disable-shared --with-pic --prefix=$PACKAGES/expat --bindir="$bindir" $HOSTTARGETFLAGS $ENVIRONMENT

echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make $ENVIRONMENT
CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make $ENVIRONMENT

echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make install $ENVIRONMENT
CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make install $ENVIRONMENT

echo make distclean && \
make distclean

# configure and compile libiconv during configuration stage
echo ""
echo "Configuring iconv"
echo ""
cd "$PACKAGES/iconv"

# Darwin before El Capitan required a different version of iconv than the one we use for Linux
# From El Capitan onwards, we realised that MacOS came with its own specific version of libiconv
# and so we'll use that system libiconv, in order to get around with Macs wiping out the DYLD_LIBRARY_PATH

if test $GSDLOS != darwin ; then

    if test ! -d libiconv-1.13.1 ; then
	gzip -dc libiconv-1.13.1.tar.gz | tar -xf -
    fi
    cd "$PACKAGES/iconv/libiconv-1.13.1"

    if test -e Makefile ; then	
	echo make distclean && \
	    make distclean
    fi

    # configure
    echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --enable-shared --enable-static --prefix="$PACKAGES/iconv" $HOSTTARGETFLAGS $ENVIRONMENT
    CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --enable-shared --enable-static --prefix="$PACKAGES/iconv" $HOSTTARGETFLAGS $ENVIRONMENT
#else
    ##darwin
    #if test ! -d libiconv-1.11 ; then
    #	gzip -dc gs-libiconv-1.11.tar.gz | tar -xf -
    #fi
    #cd "$PACKAGES/iconv/libiconv-1.11"
    #
    #if test -e Makefile ; then	
    #	echo make distclean && \
    #	    make distclean
    #fi

    ## configure
    #echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --disable-shared --enable-static --prefix="$PACKAGES/iconv" $HOSTTARGETFLAGS $ENVIRONMENT
    #CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --disable-shared --enable-static --prefix="$PACKAGES/iconv" $HOSTTARGETFLAGS $ENVIRONMENT
#fi

  # make
  echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make $ENVIRONMENT
  CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make $ENVIRONMENT

  # make install
  echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make install $ENVIRONMENT
  CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" make install $ENVIRONMENT

  echo make distclean && \
  make distclean

else
    # darwin, no need to compile libiconv: use system libiconv
    echo "******* NOT Configuring iconv *****"
    echo "******* Relying on system libiconv for compiling GS on MacOS *****"
fi


# configure GDBM
echo ""
echo "Configuring GDBM"
if test $USE_GDBM  ; 
then
    echo ""
    cd "$PACKAGES/gdbm"

    echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --disable-shared --prefix=$PACKAGES/gdbm $HOSTTARGETFLAGS $ENVIRONMENT

    cd "$PACKAGES/gdbm/gdbm-1.8.3" && \
	CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --disable-shared --prefix=$PACKAGES/gdbm $HOSTTARGETFLAGS $ENVIRONMENT
else
  echo " GDBM support disabled."
  echo "  add '--enable-gdbm' to the configure command to include GDBM support."
  echo ""
fi

# configure JDBM
echo ""
echo "Configuring JDBM"
if test $USE_JDBM  ; 
then
  echo ""
  cd "$PACKAGES/jdbm"
  if test ! -d jdbm-1.0 ;
  then
    gzip -dc gs-jdbm-1.0.tar.gz | tar -xf -
  fi

  if test "x$JAVAC" = "x" ; then
    JAVAC=javac
  fi

# we now only use -source and -target when making a release, and in that case
#they are set y the release kit.  
#  if test "x$JAVACFLAGS" = "x" ; then
#    JAVACFLAGS="-source 1.6 -target 1.6"
#  fi

  cd jdbm-1.0 && cat Makefile.in | sed -e "s,@JAVAC@,$JAVAC,g" -e "s,@JAVACFLAGS@,$JAVACFLAGS,g" > Makefile
else
  echo "JDBM support disabled."
  echo "  add '--enable-jdbm' to the configure command to include JDBM support."
  echo ""
fi

# configure SQLite
echo ""
echo "Configuring SQLite"
if test x$USE_SQLITE = xtrue ; 
then
  echo ""
  cd "$PACKAGES/sqlite"
  if test ! -d sqlite-autoconf-3070602 ;
  then 
    gzip -dc sqlite-autoconf-3070602.tar.gz | tar -xf -
  fi

  arch=`uname -p`

  if test $arch = powerpc ; then
      CFLAGS="-DSQLITE_ENABLE_LOCKING_STYLE=0 $CFLAGS"
  fi

  echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --prefix="$PACKAGES/sqlite" --bindir="$bindir" $HOSTTARGETFLAGS $ENVIRONMENT

  cd "$PACKAGES/sqlite/sqlite-autoconf-3070602" && \
  CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure $CACHE_FILE --prefix="$PACKAGES/sqlite" --bindir="$bindir" $HOSTTARGETFLAGS $ENVIRONMENT
else
  echo "SQLite support disabled."
  echo "  add '--enable-sqlite' to the configure command to include SQLite support."
  echo ""
fi

# configure search4j
echo ""
echo "Configuring search4j"
echo ""
cd "$PACKAGES/search4j"

#echo CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure --bindir="$GSDLHOME/bin/$GSDLOS" --libdir="$GSDLHOME/lib/$GSDLOS" $CACHE_FILE $HOSTTARGETFLAGS $ENVIRONMENT
echo ./configure --bindir="$GSDLHOME/bin/$GSDLOS" --libdir="$GSDLHOME/lib/$GSDLOS" $CACHE_FILE $HOSTTARGETFLAGS $ENVIRONMENT

cd "$PACKAGES/search4j" && \
 ./configure --bindir="$GSDLHOME/bin/$GSDLOS" --libdir="$GSDLHOME/lib/$GSDLOS" $CACHE_FILE $HOSTTARGETFLAGS $ENVIRONMENT
# CFLAGS="$CFLAGS $ARCH_DEP_FLAGS" CXXFLAGS="$CXXFLAGS $ARCH_DEP_FLAGS" LDFLAGS="$LDFLAGS $ARCH_DEP_FLAGS" ./configure --bindir="$GSDLHOME/bin/$GSDLOS" --libdir="$GSDLHOME/lib/$GSDLOS" $CACHE_FILE $HOSTTARGETFLAGS $ENVIRONMENT
