#!/bin/sh

CONFIG=.config

# Defaults
KDIR=/usr/src/linux
CRYPTODIR=/usr/src/cryptoapi
PREFIX=/usr/local

# Read config file
if [ -r $CONFIG ]; then
    . $CONFIG
fi

echo "Before you can compile and install ipsec_tunnel, you must enter the"
echo "paths to the directories containing the source code of the Linux"
echo "kernel and the CryptoAPI, as well as the directory prefix where the"
echo "programs and manual pages will be installed.  To accept the default"
echo "choice in brackets, just press enter."
echo

while [ 1 ]; do
    echo -n "Linux kernel source directory [$KDIR]: "
    read dir
    if [ -z "$dir" ]; then
	dir=$KDIR
    fi
    if [ -d "$dir/include" ]; then
	break
    fi
    echo
    echo "That directory is not a Linux kernel source directory."
    echo
done
KDIR=$dir

if [ -f "$KDIR/include/linux/crypto.h" ]; then
    echo "CryptoAPI patched Linux kernel directory detected."
    CRYPTODIR="$KDIR"
else
    while [ 1 ]; do
	echo -n "CryptoAPI source/include directory [$CRYPTODIR]: "
	read dir
	if [ -z "$dir" ]; then
	    dir=$CRYPTODIR
	fi
	if [ -f "$dir/linux/crypto.h" ]; then
	    CRYPTODIR="$dir"
	    break
	fi
	if [ -f "$dir/kernel/include/linux/crypto.h" ]; then
	    CRYPTODIR="$dir/kernel/include"
	    break
	fi
	echo
	echo "That directory is not a CryptoAPI source directory."
	echo
    done
fi

while [ 1 ]; do
    echo -n "Installation prefix [$PREFIX]: "
    read dir
    if [ -z "$dir" ]; then
	dir=$PREFIX
    fi
    if [ -d "$dir" ]; then
	PREFIX="$dir"
	break
    fi
    echo
    echo "That directory does not exist."
    echo
done

if [ -z "$EXEC_PREFIX" ]; then
    EXEC_PREFIX=$PREFIX
fi

while [ 1 ]; do
    echo -n "Installation prefix for executables [$EXEC_PREFIX]: "
    read dir
    if [ -z "$dir" ]; then
	dir=$EXEC_PREFIX
    fi
    if [ -d "$dir" ]; then
	EXEC_PREFIX="$dir"
	break
    fi
    echo
    echo "That directory does not exist."
    echo
done

echo "# Automatically generated by configure." > $CONFIG
echo "KDIR=$KDIR" >> $CONFIG
echo "CRYPTODIR=$CRYPTODIR" >> $CONFIG
echo "PREFIX=$PREFIX" >> $CONFIG
echo "EXEC_PREFIX=$EXEC_PREFIX" >> $CONFIG

echo
echo "Configuration is now complete."
echo
echo "Run \"make\" to compile and then \"make install\" to install."
