#!/bin/bash

# Adapted from the ocamlnet configure script.

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

#--- Options ---
# value 0: off
# value 1: on
# defaults:
set_defaults () {
    name="gikia-tiny"
    enable_debug=0
    enable_bytecode=1
    enable_nativecode=1
    enable_static=0
    enable_bytelib=0
    enable_sharedlib=0
    root=""
    prefix="/usr/local"
    logdir='\$$HOME/.'"$name"
    datadir='\$$HOME/.'"$name"
    commandpipe='\$$datadir/ocsigen_command'
}

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"
ehelp_nativecode="Enable/disable nativecode version"
ehelp_static="Enable/disable building statically linked binaries and bytecode version with integrated runtime; if enabled, application will run without any extra dependencies"
ehelp_bytelib="Enable/disable bytecode library"
ehelp_sharedlib="Enable/disable nativecode shared library"

## Which options exist? eoptions for enable/disable, woptions for with/without:
eoptions="debug bytecode nativecode static bytelib sharedlib"

# Packages to include anyway:
requires="lwt ocsigen gikia-common"

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

	echo "    --prefix $prefix"
	echo "    --logdir $logdir"
	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
--prefix dir
	Subdirectory where to install binaries and libs (usually /usr or /usr/local)

--logdir dir
        Use this directory for Ocsigen's logs

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

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

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

Defaults are:

_EOF_
	print_options >&2
	exit 1
}

check_library () {
  printf "CHecking for %s... " $1
	if ocamlfind query "$1" >/dev/null 2>/dev/null;
  then echo "found";
  else
    echo "not found"
    echo "Make sure that ocamlfind is in your PATH, or install $1"
    echo "(from $2)"
    exit 1
  fi
}

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 $name 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
			;;
		--prefix)
			prefix="$2"
			shift
			shift
			;;
    --logdir)
      logdir="$2"
      shift
      shift
      ;;
		--datadir)
			datadir="$2"
			shift
			shift
			;;
		--name)
			name="$2"
			shift
			shift
			;;
		--commandpipe)
			commandpipe="$2"
			shift
			shift
			;;
		--version)
			echo "$version"
			exit 0
			;;
		*)
                        echo "Unknown option: $1" >&2
			usage
	esac
done


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

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_library "ocsigen" "ocsigen.org"
check_library "lwt" "ocsigen.org/lwt"
check_library "gikia-common" "komar.bitcheese.net/darcs/gikia-common"

printf "%s" "Checking for ocamlduce... "
if which ocamlduce >/dev/null 2>/dev/null; then
    echo "found"
else
    echo "not found"
    exit 1
fi

echo "Checking for OCaml version"
version=`ocamlc -version|sed 's/^\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/'`
ocamlversion=`n1=${version%%.*}; n2=${version##*.}; \
              if [ $n1 -eq 3 ] && [ $n2 -le 9 ]; then \
                echo "OLD"; \
              else \
                echo "NEW"; \
              fi`
case "$ocamlversion" in
    "OLD") echo; echo "OCaml >= 3.10 is required. Aborting."; exit 1;;
esac


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

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

echo
echo "Effective options:"
print_options
echo


if [ $enable_debug -gt 0 ] ; then
	enable_debug="-g"
else
	enable_debug=""
fi

if [ $enable_bytecode -gt 0 ] ; then
	all+=" byte"
  install+=" installbyte"
fi
if [ $enable_nativecode -gt 0 ] ; then
	all+=" native"
  install+=" installnative"
fi
if [ $enable_static -gt 0 ] ; then
	byteopts+=" -custom"
  nativeopts+=""
fi
if [ $enable_bytelib -gt 0 ] ; then
	all+=" bytelib"
  install+=" installbytelib"
fi
if [ $enable_sharedlib -gt 0 ] ; then
	all+=" sharedlib"
  install+=" installsharedlib"
fi



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

echo "Writing Makefile.config"
cat <<_EOF_ > $my_pwd/Makefile.config
# subdirectory where to install binaries and libs (usually /usr or /usr/local)
PREFIX=$root$prefix

# ocsigen's logs:
LOGDIR='$root$logdir'

# Where to put dynamic data:
DATADIR='$root$datadir'

# Extra options
BYTEOPTS=$byteopts
NATIVEOPTS=$nativeopts

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

## Do you want annot files? (-dtypes)
#ANNOT=$enable_annot

# The name of the project
NAME=$name

all:$all

install:$install

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

_EOF_

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

echo
echo
echo "You can now compile $name by invoking"
echo "   make"
echo "and install it with"
echo "   make install"
echo
