#! /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

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=""

# GDBM compilation enabled by default, can switch it off with --disable-gdbm
USE_GDBM=true
# JDBM compilation enabled by default, can switch it off with --disable-jdbm
USE_JDBM=true
# 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
# Apache's httpd (web server) enabled by default
USE_APACHE_HTTPD=false

# 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-gdbm)
	    USE_GDBM=false
	    ;;
	--disable-jdbm)
	    USE_JDBM=false
	    ;;
	--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
	    ;;       
	--enable-apache-httpd)
	    USE_APACHE_HTTPD=true
	    ;;
	--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=*)
	    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

# 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.

if test $USE_CORBA = true ; then
  if test $MICO_DIR = default; then
    # configure the MICO CORBA ORB
    echo ""
    echo "Configuring MICO CORBA"
    echo ""

    cd "$PACKAGES/mico"
    gzip -dc mico-2.3.13-gs.tar.gz | tar -xf -
    cd mico && \
    ./configure $CACHE_FILE    --prefix=$PACKAGES/mico/  --disable-coss --disable-naming --disable-relship --disable-events --disable-streams --disable-property --disable-trader --disable-time 
#--disable-debug 
#--enable-optimize --enable-final --enable-speed-tune
#    sed 's/tools//'  $PACKAGES/mico/mico/Makefile | sed 's/ldconfig//' >$PACKAGES/mico/mico/Makefile.out 

# Remove any references to running 'tools' and 'ldconfig' as these require
# admin permissions when running 'make install'.
# Use fgrep here to knock out the entire line containing $(LDCONFIG), because
# there may be a special tab preceding the line which in a Makefile can have
# special meaning. Don't want to leave it in by itself.
    sed 's/tools//'  $PACKAGES/mico/mico/Makefile | fgrep -iv ldconfig >$PACKAGES/mico/mico/Makefile.out
    mv $PACKAGES/mico/mico/Makefile.out $PACKAGES/mico/mico/Makefile
  else
    #MICO is on the system path
    echo ""
    echo "Taking MICO from system paths"
    echo ""
  fi
else
  echo "corba support disabled - mico compiler not required."
  echo "  add '--enable-corba' to the configure command to include corba support."
  echo ""
fi



# D2M stuff needed for z3950server
echo ""
echo "Configuring D2M"
if test x$USE_Z3950 = xtrue ; 
then
  USE_YAZ=true
  echo ""
  cd "$PACKAGES/d2m"
  ./configure $CACHE_FILE --prefix="$PACKAGES/d2m" $HOSTTARGETFLAGS
else
  echo "Z39.50 support disabled - D2M library not required."
  echo "  add '--enable-z3950' to the configure command to include z39.50 support."
  echo ""
fi
   
# YAZ Z39.50 toolkit
# http://stackoverflow.com/questions/21027388/how-to-make-pkg-check-modules-work-with-static-libraries
echo ""
echo "Configuring YAZ"
if test x$USE_YAZ = xtrue ; 
then
  echo "YAZ library support enabled"
  echo "add --disable-yaz to the configure command to disable this"
  echo ""
  cd "$PACKAGES/yaz" 
  gzip -dc yaz-2.1.56.tar.gz | tar -xf -
  cd yaz-2.1.56 && \
    ac_cv_path_pkgconfigpath="pkg-config --static" ./configure $CACHE_FILE --prefix="$PACKAGES/yaz" --bindir="$bindir" --disable-shared --enable-static $HOSTTARGETFLAGS
else
  echo "YAZ support disabled"
  echo ""
fi


# Apache's HTTPD web server
echo ""
echo "Configuring Apache httpd"
if test x$USE_APACHE_HTTPD = xtrue ; 
then
  echo "Apache httpd support enabled"
  echo "add --disable-apache-httpd to the configure command to disable this"
  echo ""
#  echo "Setting up libiconv (used by Apache httpd)"
#  cd "$PACKAGES/apache-httpd" 
#  if [ ! -e libiconv-1.13.1 ] ; then gzip -dc libiconv-1.13.1.tar.gz | tar -xf - ; fi
#  cd libiconv-1.13.1&& \
#    ./configure $CACHE_FILE --prefix="${prefix}/apache-httpd/${GSDLOS}" --disable-shared $HOSTTARGETFLAGS&&
#    make && make install
#  echo ""
  echo "Configuring Apache httpd"
  cd "$PACKAGES/apache-httpd" 
  if [ ! -e httpd-2.2.11 ] ; then gzip -dc httpd-2.2.11-gs.tar.gz | tar -xf - ; fi
  cd httpd-2.2.11&& \

CFLAGS="$CFLAGS -I${GSDLHOME}/common-src/packages/iconv/include" CXXFLAGS="$CXXFLAGS -I${GSDLHOME}/common-src/packages/iconv/include" LDFLAGS="$LDFLAGS -L${GSDLHOME}/common-src/packages/iconv/lib -L${GSDLHOME}/common-src/packages/sqlite/lib" ./configure $CACHE_FILE --with-included-apr --with-layout=BinaryDistribution --enable-most --enable-rewrite --prefix="${prefix}/apache-httpd/${GSDLOS}" $HOSTTARGETFLAGS --with-iconv

else
  echo "Apache httpd support disabled"
  echo "add --enable-apache-httpd to the configure command to enable this"
  echo ""
fi


