#! /bin/sh

# $Id: configure 1080 2007-01-07 16:00:50Z gerd $
# Adapted from the ocamlnet configure script.

#######################################################################
# Helpers:

# Split $PATH into words:
oldifs="$IFS"
IFS="   :"
spacepath=`echo $PATH`
IFS="$oldifs"


in_path () {
    # Does $1 exist in $PATH?
    for d in $spacepath; do
        if test -x "$d/$1"; then
            return 0
        fi
    done
    return 1
}


get_path () {
    for d in $spacepath; do
        if test -x "$d/$1"; then
            echo "$d/$1"
            return
        fi
    done
}

#######################################################################
# Defaults

#--- Options ---
# value 0: off
# value 1: on
# defaults:

set_defaults () {
    name="ocsigen"
    enable_bytecode=1
    enable_nativecode=0
    enable_debug=0
    enable_ocamlduce=1
    enable_newocamlnet=1
    enable_sqlite=1
    enable_camlzip=1
    enable_dbm=1
    prefix="/usr/local"
    bindir=""
    examplesdir=""
    libdir=""
    mandir=""
    docdir=""
    sysconfdir="/etc"
    staticpagesdir="/var/www"
    uploaddir="/tmp"
    datadir="/var/lib"
    commandpipe="/var/run/ocsigen_command"
    temproot=""
    root=""
    ocsigen_user="www-data"
    ocsigen_group="www-data"
}

set_defaults
my_pwd=`dirname $0`
version=`head -n 1 $my_pwd/VERSION`
full_pwd=`pwd`

########################################################################
## Option parsing
#
#
ehelp_debug="Enable/disable debug output"
ehelp_bytecode="Enable/disable bytecode version of Ocsigen"
ehelp_nativecode="Enable/disable nativecode version of Ocsigen"
ehelp_ocamlduce="Enable/disable Ocamlduce for typing pages (requires ocamlduce to be installed)"
ehelp_newocamlnet="Use new/old versions of Ocamlnet (>= 2.0)"
ehelp_sqlite="Compile ocsipersist with SQLite for persistent storage (default)"
ehelp_dbm="Compile ocsipersist with DBM for persistent storage"
ehelp_camlzip="Compile deflatemod extension (requires camlzip); default is yes"

## Which options exist? eoptions for enable/disable, woptions for with/without:
eoptions="debug bytecode nativecode ocamlduce newocamlnet sqlite dbm camlzip"
woptions=""



# Packages to include anyway:
requires="netstring ssl sqlite3"



check_library () {
    # $1: the name of the library (findlib)
#   # $2: a typical file in $incdirs
#    if [ "$enable_findlib" -gt 0 ]; then
	ocamlfind query "$1" >/dev/null 2>/dev/null
	return
#    else
#	stdlib=`ocamlc -where`
#	for dir in $incdirs; do
#	    case "$dir" in
#		+*)
#		    dir=`echo "$dir" | sed -e "s|^\+|$stdlib/|"` ;;
#	    esac
#	    if [ -f "$dir/$2" ]; then
#		return 0
#	    fi 
#	done
	return 1   # not found
#    fi
}


print_options () {
	for opt in $eoptions; do
		e="o=\$enable_$opt"
		eval "$e"
		uopt=`echo $opt | sed -e 's/_/-/g'`
		if [ $o -gt 0 ]; then
			echo "    --enable-$uopt"
		else
			echo "    --disable-$uopt"
		fi
	done
	for opt in $woptions; do
		e="o=\$with_$opt"
		eval "$e"
		uopt=`echo $opt | sed -e 's/_/-/g'`
		if [ $o -gt 0 ]; then
			echo "    --with-$uopt"
		else
			echo "    --without-$uopt"
		fi
	done
        case "$bindir" in
            "") bindir2="<prefix>/bin";;
            *) bindir2=$bindir;;
        esac
        
        case "$examplesdir" in
            "") examplesdir2="<prefix>/lib";;
            *) examplesdir2=$examplesdir;;
        esac
        
        case "$libdir" in
            "") libdir2="<prefix>/<ocamlfind install directory>";;
            *) libdir2=$libdir;;
        esac
        
        case "$mandir" in
            "") mandir2="<prefix>/share/man/man1";;
            *) mandir2=$mandir;;
        esac
        
        case "$docdir" in
            "") docdir2="<prefix>/share/doc";;
            *) docdir2=$docdir;;
        esac

	echo "    --root $root"
	echo "    --temproot $temproot"
	echo "    --prefix $prefix"
	echo "    --ocsigen-user $ocsigen_user"
	echo "    --ocsigen-group $ocsigen_group"
	echo "    --bindir $bindir2"
	echo "    --libdir $libdir2"
	echo "    --examplesdir $examplesdir2"
	echo "    --mandir $mandir2"
	echo "    --docdir $docdir2"
	echo "    --sysconfdir $sysconfdir"
	echo "    --staticpagesdir $staticpagesdir"
	echo "    --uploaddir $uploaddir"
	echo "    --datadir $datadir"
	echo "    --name $name"
	echo "    --commandpipe $commandpipe"
}


usage () {
	set_defaults
	cat <<_EOF_ >&2
usage: ./configure [ options ]

_EOF_
	for opt in $eoptions; do
		e="help=\$ehelp_$opt"
		eval "$e"
		uopt=`echo $opt | sed -e 's/_/-/g'`
		echo "--enable-$uopt:" >&2
		echo "--disable-$uopt:" >&2
		echo "        $help" >&2
		echo "" >&2
	done
	for opt in $woptions; do
		e="help=\$whelp_$opt"
		eval "$e"
		uopt=`echo $opt | sed -e 's/_/-/g'`
		echo "--with-$uopt:" >&2
		echo "--without-$uopt:" >&2
		echo "        $help" >&2
		echo "" >&2
	done
	cat <<_EOF_ >&2
--root dir
	Root directory to install the package (usually /) (Everything will be in this directory)

--temproot dir
	Temporary root directory to install the package (usually always "" but for package makers)

--prefix dir
	Subdirectory where to install binaries and libs (usually /usr or /usr/local)

--bindir dir
        Install binaries into this directory

--libdir dir
        Install Ocsigen's extensions into this directory

--examplesdir dir
        Install Ocsigen's examples sites into this directory

--mandir dir
	Install man pages in this directory

--docdir dir
	Install documentation in this directory

--sysconfdir dir
	Create configuration directory in this directory

--staticpagesdir dir
	Install default static pages directory in this directory

--uploaddir dir
	By default, files will be uploaded in this directory

--datadir dir
	The directory for data written by the server will be in this directory

--name name
	The name of the server (and directories)

--ocsigen-user name
	The name of the ocsigen user

--ocsigen-group name
	The name of the ocsigen group

--commandpipe name
        The name of the named pipe used to command the server

Defaults are:

_EOF_
	print_options >&2
	exit 1
}


check_eopt () {
	for x in $eoptions; do
		if [ "$x" = "$1" ]; then
			return 0
		fi
	done
	echo "Unknown option: $1" >&2
	exit 1
}


check_wopt () {
	for x in $woptions; do
		if [ "$x" = "$1" ]; then
			return 0
		fi
	done
	echo "Unknown option: $1" >&2
	exit 1
}


echo "Welcome to Ocsigen version $version" >&2

while [ "$#" -gt 0 ]; do
	case "$1" in
		--enable-*)
			opt=`echo "$1" | sed -e 's/--enable-//' -e 's/-/_/g'`
			check_eopt "$opt"
			eval "enable_$opt=2"
			shift
			;;
		--disable-*)
			opt=`echo "$1" | sed -e 's/--disable-//' -e 's/-/_/g'`
			check_eopt "$opt"
			eval "enable_$opt=-1"
			shift
			;;
		--with-*)
			opt=`echo "$1" | sed -e 's/--with-//' -e 's/-/_/g'`
			check_wopt "$opt"
			eval "with_$opt=2"
			shift
			;;
		--without-*)
			opt=`echo "$1" | sed -e 's/--without-//' -e 's/-/_/g'`
			check_wopt "$opt"
			eval "with_$opt=-1"
			shift
			;;
		--root)
			root="$2"
			shift
			shift
			;;
		--temproot)
			temproot="$2"
			shift
			shift
			;;
		--prefix)
			prefix="$2"
			shift
			shift
			;;
                --bindir)
                        bindir="$2"
                        shift
                        shift
                        ;;
                --libdir)
                        libdir="$2"
                        shift
                        shift
                        ;;
                --examplesdir)
                        examplesdir="$2"
                        shift
                        shift
                        ;;
		--mandir)
			mandir="$2"
			shift 
			shift
			;;
		--docdir)
			docdir="$2"
			shift
			shift
			;;
		--staticpagesdir)
			staticpagesdir="$2"
			shift
			shift
			;;
		--sysconfdir)
			sysconfdir="$2"
			shift
			shift
			;;
		--uploaddir)
			uploaddir="$2"
			shift
			shift
			;;
		--datadir)
			datadir="$2"
			shift
			shift
			;;
		--name)
			name="$2"
			shift
			shift
			;;
		--ocsigen-user)
			ocsigen_user="$2"
			shift
			shift
			;;
		--ocsigen-group)
			ocsigen_group="$2"
			shift
			shift
			;;
		--commandpipe)
			commandpipe="$2"
			shift
			shift
			;;
		--version)
			echo "$version"
			exit 0
			;;
		*)
			usage
	esac
done

case "$bindir" in
  "") bindir="$prefix/bin";;
esac

case "$examplesdir" in
  "") examplesdir="$prefix/lib";;
esac

case "$libdir" in
  "") libdir="\`\$(OCAMLFIND) printconf destdir\`";;
esac

case "$mandir" in
  "") mandir="$prefix/share/man/man1";;
esac

case "$docdir" in
  "") docdir="$prefix/share/doc";;
esac




######################################################################
# Check ocamlfind

printf "%s" "Checking for findlib... "
if ocamlfind query stdlib >/dev/null 2>/dev/null; then
    echo "found"
else
    echo "not found"
    echo "Make sure that ocamlfind is in your PATH, or install findlib"
    echo "(from www.ocaml-programming.de)"
    exit 1
fi



######################################################################
# Check SSL

printf "%s" "Checking for ssl... "
if ocamlfind query ssl >/dev/null 2>/dev/null; then
    echo "found"
else
    echo "not found"
    echo "Required library ssl not found!"
    exit 1
fi


######################################################################
# Check SQLITE3

if [ "$enable_sqlite" -gt 0 ]; then
    printf "%s" "Checking for sqlite3... "
    if ocamlfind query sqlite3 >/dev/null 2>/dev/null; then
        echo "found"
    else
        echo "not found"
        echo "Required library sqlite3 not found!"
        exit 1
    fi

    ocamlsqlite3dir=`ocamlfind query sqlite3`
fi


######################################################################
# Check Camlzip

if [ "$enable_camlzip" -gt 0 ]; then
    printf "%s" "Checking for camlzip... "
    if ocamlfind query camlzip >/dev/null 2>/dev/null; then
        echo "found camlzip"
        camlzipdir=`ocamlfind query camlzip`
        camlzipname=camlzip
    else
      if ocamlfind query zip >/dev/null 2>/dev/null; then
          echo "found zip"
          camlzipdir=`ocamlfind query zip`
          camlzipname=zip
      else
          echo "not found"
          echo "Required library camlzip (or libzip-ocaml) not found!"
          exit 1
      fi

    fi

fi

######################################################################
# Check Cryptokit

printf "%s" "Checking for Cryptokit... "
if ocamlfind query cryptokit >/dev/null 2>/dev/null; then
    echo "found"
else
    echo "not found"
    echo "Required library cryptokit not found!"
    exit 1
fi

cryptokitdir=`ocamlfind query cryptokit`



######################################################################
# Check whether OCaml version is > 3.9
version=`ocamlc -version`
ocamlversion=`n1=${version%%.[0-9][0-9].[0-9]}; \
              tail=${version##[0-9].}; \
              n2=${tail%%.[0-9]}; \
              n3=${tail##[0-9][0-9].}; \
              if [ $n1 = "3" ] && [ $n2 -le 9 ]; then \
                echo "OLD"; \
              else \
                echo "NEW"; \
              fi`

#ocamlversion=`n1=${version:0:1}; \
#              n2=${version:2:2}; \
#              n3=${version:5}; \

######################################################################
# Check camlp4o
#printf "%s" "Finding out which camlp4o to use... "
#if which camlp4o.byte >/dev/null 2>/dev/null; then
#    echo "camlp4o.byte"
#    camlp4o="camlp4o.byte"
#else
#    echo "camlp4o"
#    camlp4o="camlp4o" 
#fi

######################################################################

######################################################################
# Summary

echo
echo "Effective options:"
print_options
echo


#Convert 0/1 values to YES/NO
if [ $enable_debug -gt 0 ] ; then
	enable_debug="YES"
else
	enable_debug="NO"
fi
if [ $enable_bytecode -gt 0 ] ; then
	enable_bytecode="YES"
else
	enable_bytecode="NO"
fi
if [ $enable_nativecode -gt 0 ] ; then
	enable_nativecode="YES"
        echo "WARNING: Native code version is experimental and requires a modified ocaml compiler for now (does not work with ocaml 3.10)"
else
	enable_nativecode="NO"
fi
if [ $enable_ocamlduce -gt 0 ] ; then
	enable_ocamlduce="YES"
else
	enable_ocamlduce="NO"
fi
if [ $enable_newocamlnet -gt 0 ] ; then
	enable_newocamlnet="YES"
else
	enable_newocamlnet="NO"
fi
if [ $enable_dbm -gt 0 ] ; then
	enable_dbm="YES"
else
	enable_dbm="NO"
fi
if [ $enable_sqlite -gt 0 ] ; then
	enable_sqlite="YES"
else
	enable_sqlite="NO"
fi
if [ $enable_camlzip -gt 0 ] ; then
	enable_camlzip="YES"
else
	enable_camlzip="NO"
fi




ocamlinclude=`ocamlfind printconf stdlib`

######################################################################
# Write Makefile.conf

echo "Writing Makefile.config"
cat <<_EOF_ > $my_pwd/Makefile.config
# Which ocamlfind/camlp4/ ... version to use? 
# ocamlfind will choose automatically the compiler.
OCAMLFIND=ocamlfind
CAMLP4=camlp4
CAMLLEX=ocamllex
CAMLYACC=ocamlyacc
CAMLCNAME=ocamlc
CAMLOPTNAME=ocamlopt
CAMLCPNAME=ocamlcp -p a # No: profiling is incompatible with the -pp option
CHOWN=chown
CC=gcc

CAMLINCLUDE=-I $ocamlinclude

## The root directory for the package install (usually /)
#ROOT = $root

# Temporary root directory to install the package (usually always "" but for package makers)
TEMPROOT = $temproot

# Do you want the bytecode version ? YES/NO
BYTECODE=$enable_bytecode

# Do you want the native code version (ocsigen.opt) ? YES/NO
NATIVECODE=$enable_nativecode

# Do you want the ocamlduce extension? (YES/NO) 
# (You need ocamlduce to be installed)
OCAMLDUCE=$enable_ocamlduce
# If you have OCamlduce installed, which ocamlducefind do you want?
OCAMLDUCEFIND=ocamlducefind

# The directory for ocsigen server (binary):
BINDIR = $root$bindir

# The directory for ocsigen manpage:
MANDIR = $root$mandir

# Where to install the directory for ocsigen extensions:
MODULEINSTALLDIR = $root$libdir

# Where to install examples:
EXAMPLESINSTALLDIR = $root$examplesdir/\$(OCSIGENNAME)

# User who will run Ocsigen server (not root) (eg, for debian, www-data)
# (This user must exist on your system)
OCSIGENUSER=$ocsigen_user

# group who will run Ocsigen server (not root) (eg, for debian, www-data)
# (This group must exist)
OCSIGENGROUP=$ocsigen_group

# The name of the server (and the directory for the extensions)
OCSIGENNAME=$name

# ocsigen's logs:
LOGDIR = $root/var/log/\$(OCSIGENNAME)

# Config files:
CONFIGDIR = $root$sysconfdir/\$(OCSIGENNAME)

# Where to put static pages:
STATICPAGESDIR =$root$staticpagesdir/\$(OCSIGENNAME)

# Where to put dynamic data:
DATADIR = $root$datadir/\$(OCSIGENNAME)

# Default directory for file upload:
UPLOADDIR = $root$uploaddir

# Where to put Ocsigen documentation:
DOCDIR = $root$docdir/\$(OCSIGENNAME)

# Do you want debugging information? (-g -dtypes)
DEBUG=$enable_debug

# The name of the named pipe used to command the server
COMMANDPIPE=$root$commandpipe

# The directory where ocamlsqlite3 is installed
OCAMLSQLITE3DIR=$ocamlsqlite3dir

# The directory where camlzip is installed
CAMLZIPDIR=$camlzipdir

# The directory where cryptokit is installed
CRYPTOKITINSTALLDIR=$cryptokitdir

# The name of camlzip package:
CAMLZIPNAME=$camlzipname

# Do you want deflatemod? (requires camlzip)
CAMLZIP=$enable_camlzip

# Do you want ocsipersist with sqlite?
OCSIPERSISTSQLITE=$enable_sqlite

# Do you want ocsipersist with dbm?
OCSIPERSISTDBM=$enable_dbm


# The source directory
SRC=$full_pwd



# If you are using ocamlnet 1, write NO here
NEWOCAMLNET=$enable_newocamlnet

# Profiling (always put NO here - but if you want to debug ocsigen):
PROFILING=NO

# Ocaml version
OCAMLVERSION=$ocamlversion

# Do not modify this
LIBDIRS= -I ../lwt -I ../baselib -I ../http -I ../xmlp4 -I ../server -I ../extensions -I ../eliom
LIBDIRS2= -I ../../lwt -I ../../baselib -I ../../http -I ../../xmlp4 -I ../../server -I ../../extensions -I ../../eliom
LIBDIRS3= -I lwt -I baselib -I http -I xmlp4 -I server -I extensions -I eliom

_EOF_

######################################################################
# Finish

echo

case "$ocamlversion" in
    "OLD") echo "Compiling for OCaml < 3.10";;
    "NEW") echo "Compiling for OCaml >= 3.10";;
esac

echo
echo "Please check Makefile.config."
echo
echo "You can now compile Ocsigen by invoking"
echo "   make depend"
echo "   make"
echo
echo "Finally, if you want system-wide install, (become root if needed and) do"
echo "   make install"
echo "to install the package, or"
echo "   make installnodoc"
echo "to install the package without generating the documentation."

