#!/bin/sh
#
# Call:
#  ./configure [--datadir=DATADIR] [--mandir=MANDIR] [--disable-nls]
#
# Where shall we store our stuff?
#  Old default: /usr/lib/kbd
#  New default: /usr/share/kbd
#

prefix=
datadir=
mandir=
nls=1

for arg in $*; do
	case "$arg" in
		-*=*) optarg=`echo "$arg" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
		   *) optarg= ;;
	esac

	case "$arg" in
		--da*)
			datadir=$optarg
			;;
		--di*)
			nls=0
			;;
		--ma*)
			mandir=$optarg
			;;
	        --pr*)
		        prefix=$optarg
			;;
		*)
			echo "usage: configure [--prefix=PREFIXDIR] [--datadir=DATADIR] [--mandir=MANDIR]"
			exit 1
			;;
	esac
done

if [ x$prefix = x ]; then
	# the default case - don't bother the user with messages
	prefix=/usr
else
    case "$prefix" in
	/*)
	    echo "Configuring for PREFIX=$prefix"
	    ;;
	*)
	    echo "PREFIX must have a leading slash"
	    exit 1
	    ;;
    esac
fi

if [ x$datadir = x ]; then
	if [ -d $prefix/lib/kbd ]; then
		datadir=$prefix/lib/kbd
	elif [ -d $prefix/share/consolefonts ]; then
		datadir=$prefix/share
	elif [ -d $prefix/share ]; then
		datadir=$prefix/share/kbd
	else
		datadir=$prefix/lib/kbd
	fi
else
    case "$datadir" in
	/*)
		datadir="$datadir"
		echo "Configuring for DATADIR=$datadir"
		;;
	*)
		echo "DATADIR must have a leading slash"
		exit 1
		;;
    esac
fi

if [ x$mandir = x ]; then
	if [ -d $prefix/share/man ]; then
		mandir=$prefix/share/man
	else
		mandir=$prefix/man
	fi
else
    case "$mandir" in
	/*)
		mandir="$mandir"
		echo "Configuring for MANDIR=$mandir"
		;;
	*)
		echo "MANDIR must have a leading slash"
		exit 1
		;;
    esac
fi

for i in Makefile src/Makefile po/Makefile man/man1/dumpkeys.1 man/man1/loadkeys.1 man/man8/setfont.8 man/man8/loadunimap.8 man/man8/mapscrn.8; do
	sed -e "
s,@datadir@,$datadir,
s,@mandir@,$mandir,
s,@prefix@,$prefix,
" $i.in > $i
done

rm -f defines.h make_include

# Next, figure out some things about the environment
# Things taken from util-linux configure

if test "$RANDOM" = "$RANDOM"; then
  # Plain old Bourne shell.
  echo checking for gcc
  test -z "$CC" -a -n "`gcc 2>&1`" && CC="gcc -O"
else
  # ksh, bash or zsh.  ksh and zsh write "command not found" to stderr.
  echo checking for gcc
  test -z "$CC" && type gcc && CC="gcc -O"
fi

CC=${CC-cc}
CFLAGS=${CFLAGS-"-O"}
compile="$CC $CFLAGS $DEFS conftest.c -o conftest $LIBS >/dev/null 2>&1"
static_compile="$CC -static $DEFS conftest.c -o conftest $LIBS >/dev/null 2>&1"

rm -f conftest.c conftest

#
# 0. Figure out architecture (one of i386, alpha, sparc, arm, m68k, mips)
#
ARCH=`uname -m | sed s/i.86/i386/`
echo "ARCH=$ARCH" >> make_include

#
# 1. Do we have <locale.h>?
#
echo "
#include <locale.h>
main(){ exit(0); }
" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then
        echo "#define HAVE_locale_h" >> defines.h
        echo "You have <locale.h>"
else
        echo "You don't have <locale.h>"
fi
rm -f conftest conftest.c

#
# 2. For lib/nls.h: do we have <libintl.h> and gettext() ?
#
echo '
#include <libintl.h>
main(int a, char **v){
  if (a == -1)  /* false */
        gettext("There is no gettext man page\n");
  exit(0);
}
' > conftest.c
eval $compile
if [ $nls = 1 ]; then
	if test -s conftest && ./conftest 2>/dev/null; then
        	echo '#define HAVE_libintl_h' >> defines.h
	        echo "You have <libintl.h> and gettext()"
        	echo '#define ENABLE_NLS' >> defines.h
	        echo "Assuming that you want to enable NLS support."
        	echo "(Otherwise, edit defines.h and delete the line with ENABLE_NLS)"
	        ENABLE_NLS=yes
	else
        	echo "You don't have <libintl.h> and gettext()"
	        ENABLE_NLS=no
	fi
else
	echo "NLS disabled"
	ENABLE_NLS=no
fi
rm -f conftest conftest.c


#
# 3. Does xgettext exist and take the option --foreign-user?
#
if (test $ENABLE_NLS = yes && type xgettext > /dev/null 2>&1); then
	msg=`xgettext --foreign-user 2>&1 | grep unrecognized`
	if test -n "$msg"; then
		echo "FOREIGN = " >> make_include
	else
		echo "FOREIGN = --foreign-user" >> make_include
	fi
	echo "HAVE_XGETTEXT=yes" >> make_include
else
	echo "HAVE_XGETTEXT=no" >> make_include
fi

echo
echo "Done. You can run make now."
