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

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
			;;
		--ma*)
			mandir=$optarg
			;;
		--di*)
			nls=0
			;;
		*)
			echo "usage: configure [--datadir=DATADIR] [--mandir=MANDIR]"
			exit 1
			;;
	esac
done

if [ x$datadir = x ]; then
	if [ -d /usr/lib/kbd ]; then
		datadir=/usr/lib/kbd
	elif [ -d /usr/share/consolefonts ]; then
		datadir=/usr/share
	elif [ -d /usr/share ]; then
		datadir=/usr/share/kbd
	else
		datadir=/usr/lib/kbd
	fi
fi

case "$datadir" in
	/*)
		echo "Configuring for DATADIR=$datadir"
		;;
	*)
		echo "DATADIR must have a leading slash"
		exit 1
		;;
esac

if [ x$mandir = x ]; then
	if [ -d /usr/share/man ]; then
		mandir=/usr/share/man
	else
		mandir=/usr/man
	fi
fi

case "$mandir" in
	/*)
		echo "Configuring for MANDIR=$mandir"
		;;
	*)
		echo "MANDIR must have a leading slash"
		exit 1
		;;
esac

for i in Makefile src/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,
" $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
