#! /bin/sh

# $Id: configure 186 2004-07-13 13:28:58Z gerd $

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

set_defaults () {
    with_ocamlrpcgen=1
    with_http_tunnel=1
    with_auth_dh=0
    with_auth_local=0
    with_xti=0
    version="0.4.1"
    exec_suffix=""
    bindir=`dirname $ocamlc`
}

ocamlc=`get_path ocamlc`
set_defaults

#######################################################################
# Option parsing

help_ocamlrpcgen="Compile/omit the generator for the RPC language mapping"
help_http_tunnel="Compile/omit the HTTP tunnel (requires netclient >= 0.90)"
help_auth_dh="Compile/omit support for Diffie-Hellman authentication (Secure RPC)"
help_auth_local="Compile/omit authenticated Unix domain sockets (experimental)"

options="ocamlrpcgen http_tunnel auth_dh auth_local"
# Note: xti is selected by auth-dh-style

print_options () {
        echo "    -bindir $bindir"
	for opt in $options; 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
	if [ -n "$auth_dh_style" ]; then
	    echo "    -auth-dh-style $authdh_style"
	fi
}


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

_EOF_
	echo "-bindir <dir>:"
	echo "        Install binaries into this directory"

	for opt in $options; do
		e="help=\$help_$opt"
		eval "$e"
		uopt=`echo $opt | sed -e 's/_/-/g'`
		echo "-with-$uopt:" >&2
		echo "-without-$uopt:" >&2
		echo "        $help" >&2
	done
	echo "-auth-dh-style <style>:"
	echo "        Set style of keyserv connector (see README.authdh)"

	cat <<_EOF_ >&2

Defaults are:

_EOF_
	set_defaults
	print_options >&2
	exit 1
}


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


while [ "$#" -gt 0 ]; do
	case "$1" in
		-with-*)
			opt=`echo "$1" | sed -e 's/-with-//' -e 's/-/_/g'`
			check_opt "$opt"
			eval "with_$opt=1"
			shift
			;;
		-without-*)
			opt=`echo "$1" | sed -e 's/-without-//' -e 's/-/_/g'`
			check_opt "$opt"
			eval "with_$opt=0"
			shift
			;;
		-bindir)
		        bindir="$2"
			shift
			shift
			;;
		-auth-dh-style)
		        adh_style="$2"
			shift
			shift
			;;
		-version*)
			echo "$version"
			exit 0
			;;
		*)
			usage
	esac
done

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

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

######################################################################
# Check equeue

printf "%s" "Checking for equeue... "
if ocamlfind query equeue >/dev/null 2>/dev/null; then
	echo "found"
else
	echo "not found"
	echo "Sorry, installation is not possible without equeue!"
	echo "Please download netstring from www.ocaml-programming.de"
	exit 1
fi

######################################################################
# Check netstring

if [ $with_http_tunnel -gt 0 ]; then
    printf "%s" "Checking for netstring... "
    if ocamlfind query netstring >/dev/null 2>/dev/null; then
	echo "found"
    else
	echo "not found"
	echo "HTTP tunnel will not be compiled"
	with_http_tunnel=0
    fi
fi

######################################################################
# Check netclient

if [ $with_http_tunnel -gt 0 ]; then
    printf "%s" "Checking for netclient... "
    v=`ocamlfind query -format '%v' netclient 2>/dev/null`
    case "X$v" in
	X0.9*)
	    echo "found" ;;
	X[1-9]*)
	    echo "found" ;;
	X0.*)
	    echo "found, but wrong version (netclient >= 0.90 is required)" 
	    echo "HTTP tunnel will not be compiled"
	    with_http_tunnel=0 ;;
        X)
	    echo "not found"
	    echo "HTTP tunnel will not be compiled"
	    with_http_tunnel=0 ;;
	*)
	    echo "got questionable response from ocamlfind"
	    echo "HTTP tunnel will not be compiled"
	    with_http_tunnel=0 ;;
    esac
fi

######################################################################
# Check cryptgps

if [ $with_auth_dh -gt 0 ]; then
    printf "%s" "Checking for cryptgps... "
    if ocamlfind query cryptgps >/dev/null 2>/dev/null; then
	echo "found"
	case "$adh_style" in
	    keyenvoy)
		echo "Connecting to keyserv with keyenvoy helper application"
		authdh_conn=keyenvoy
		;;
	    unixdomain)
		echo "Connecting to keyserv with Unix domain socket"
		authdh_conn=unixdomain
		;;
	    tirpc)
		echo "Connecting to keyserv with TI-RPC"
		with_xti=1
		authdh_reqs=",rpc-xti"
		authdh_conn="Rpc_xti_client.keyserv"
		;;
	    *)
		echo "Sorry, missing or unsupported auth-dh-style."
		echo "Consult README.authdh for more information."
		exit 1
	        ;;
	esac
	authdh_style="$adh_style"
    else
	echo "not found"
	echo "Diffie-Hellman authentication will not be compiled"
	with_auth_dh=0
    fi
fi

######################################################################
# Check cygwin

printf "%s" "Checking for cygwin... "
u=`uname`
case "$u" in
	CYGWIN*)
		echo "found"
		exec_suffix=".exe"
		;;
	*)
		echo "not found"
		;;
esac

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

echo
echo "Effective options:"
print_options
echo

pkglist="rpc"

if [ "$with_ocamlrpcgen" -gt 0 ]; then pkglist="$pkglist generator"; fi
if [ "$with_http_tunnel" -gt 0 ]; then pkglist="$pkglist rpc-over-http"; fi
if [ "$with_xti" -gt 0 ]; then pkglist="$pkglist rpc-xti"; fi
if [ "$with_auth_dh" -gt 0 ]; then pkglist="$pkglist rpc-auth-dh"; fi
if [ "$with_auth_local" -gt 0 ]; then pkglist="$pkglist rpc-auth-local"; fi

######################################################################
# Write META.in

for pkg in $pkglist; do
	if [ -f src/$pkg/META.in ]; then
	    echo "Writing src/$pkg/META"
	    sed -e "s/@VERSION@/$version/g" \
                -e "s/@AUTHDHREQS@/${authdh_reqs}/g" \
		src/$pkg/META.in >src/$pkg/META
	fi
done

######################################################################
# Write rpc_key_service.ml

if [ $with_auth_dh -gt 0 ]; then
    echo "Updating rpc_key_service.ml"
    sed -e "s/@CONNECTOR@/$authdh_conn/" \
	src/rpc-auth-dh/rpc_key_service.mlp \
	>src/rpc-auth-dh/rpc_key_service.ml
fi

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

echo "Writing Makefile.conf"
cat <<_EOF_ >Makefile.conf
VERSION = $version
PKGLIST = $pkglist
EXEC_SUFFIX = $exec_suffix
BINDIR = $bindir
_EOF_

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

echo
echo "You can now compile RPC by invoking"
echo "   make all"
echo "for the bytecode compiler, and optionally by invoking"
echo "   make opt"
echo "for the native-code compiler (if supported on your architecture)."
echo "Finally, a"
echo "   make install"
echo "will install the package."
