#!/bin/sh

version="1.3.5"
prefix="/usr/local"
packageprefix=""
compiler="gcc"
have_mandir="no"
have_bindir="no"

for opt in "$@" ; do
	case $opt in
	--prefix=*)
		prefix=`echo $opt | sed -n 's/--prefix=\(.*\)/\1/p'`
		;;
	--package-prefix=*)
		packageprefix=`echo $opt | sed -n 's|--package-prefix=\(.*\)|\1|p'`
		;;
	--bindir=*)
		bindir=`echo $opt | sed -n 's|--bindir=\(.*\)|\1|p'`
		have_bindir="yes"
		;;
	--mandir=*)
		mandir=`echo $opt | sed -n 's|--mandir=\(.*\)|\1|p'`
		have_mandir="yes"
		;;
	--compiler=*)
		compiler=`echo $opt | sed -n 's|--compiler=\(.*\)|\1|p'`
		;;
	--help)
		echo ""
		echo "valid options are:"
		echo "--prefix dir           install to prefix 'dir'"
 		echo "--package-prefix dest  pretend to install to the prefix,"
		echo "                       but copy files to 'dest/prefix' on make install"
		echo "--bindir dir           install program to 'dir' (PREFIX/bin/)"
		echo "--mandir dir           install man page to 'dir' (PREFIX/share/man/)"
		echo "--compiler program     compile with 'program' (gcc)"
		exit
		;;
        *)
		echo "unknown configure argument:" "$opt" "(ignoring)"
		;;
	esac
done

if test -n "$packageprefix" ; then
    packageprefix="$packageprefix/"
fi

if test "$have_bindir" = "no" ; then
    bindir="$prefix/bin"
fi

if test "$have_mandir" = "no" ; then
    mandir="$prefix/share/man"
fi

packagebindir="$packageprefix$bindir"
packagemandir="$packageprefix$mandir"

sed -e "s|{BINDIR}|$packagebindir|g" \
    -e "s|{MANDIR}|$packagemandir|g" \
    < Makefile.in > Makefile

sed -e "s|{CC}|$compiler|" \
    -e "s|{CFLAGS}|$CFLAGS|" \
    < src/Makefile.in > src/Makefile

echo "" > "src/config.h"
echo "#ifndef CKSFV_CONFIG_H" >> "src/config.h"
echo "#define CKSFV_CONFIG_H" >> "src/config.h"

echo "#define VERSION \"$version\"" >> src/config.h

cat > tmpfile.c <<EOF
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {return 0;}
EOF
gcc -Wall -O2 -o tmpfile tmpfile.c >/dev/null 2>/dev/null
if test "$?" != "0" ; then
    has_stdint="no"
else
    has_stdint="yes"
fi

cat > tmpfile <<EOF
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {return 0;}
EOF
gcc -Wall -O2 -o tmpfile tmpfile.c >/dev/null 2>/dev/null
if test "$?" != "0"; then
    has_inttypes="no"
else
    has_inttypes="yes"
fi

if test "$has_stdint" = "yes" ; then
    echo "#include <stdint.h>" >> src/config.h
elif test "$has_inttypes" = "yes"; then
    echo "#include <inttypes.h>" >> src/config.h
else
    # no stdint.h or inttypes.h. obsolete environment. but we may hope..
    echo "#include <unistd.h>" >> src/config.h
    has_stdint="no (compilation will probably fail)"
fi

echo "#endif" >> "src/config.h"

echo ""
echo "  prefix directory:          $prefix"
echo "  package prefix:            $packageprefix"
echo "  binary directory:          $bindir"
echo "  man directory:             $mandir"
echo "  compiler:                  $compiler"
echo "  has stdint.h:              $has_stdint"
echo "  has inttypes.h:            $has_inttypes"
echo ""
echo "configure succesful."
