#!/bin/sh

# utt configure file
# creates config.mak which is used by other makefiles

# variables for dynamically selecting whether to compile utf8 versions
CUR_DIR=$(pwd)
SRC_DIR="${CUR_DIR}/src"

# list of components to compile
CMPLIST="compdic cor dgc dgp fla gph grp gue kon kor kot lem mar rm12 rs12 sen sen-nl ser tags tok.c tok.l tok.pl tre unfla"
COMP=


# sets all variables to a defined value
set_all(){
	value=$1
	shift
	for var in $*; do
		eval $var=$value
	done
}

# enables a feature
enable(){
	set_all yes $*
}

# disables a feature
disable(){
	set_all no $*
}

# checks whether a feature is enabled
enabled(){
	eval test "x\$$1" = "xyes"
}

# checks whether a feature is disabled
disabled(){
	eval test "x\$$1" = "xno"
}

show_help(){
	echo "Usage configure [options]"
	echo "Options: [defaults in brackets after descriptions]"
	echo "All \"enable\" options have \"disable\" counterparts"
	echo
	echo "   --help                    print this message"
	echo "   --prefix=PREFIX           install in PREFIX [$PREFIX]"
	echo "   --bindir=BINDIR           install binaries in BINDIR [$bindir]"
	echo "   --confdir=CONFDIR         install configuration in CONFDIR [$confdir]"
	echo "   --sharedir=SHAREDIR       install shared in SHAREDIR [$sharedir]"
	echo "   --docdir=DOCDIR           install doc in DOCDIR [$docdir]"
	echo "   --langdir=LANGDIR         install lang in LANGDIR [$langdir]"
	echo "   --libdir=LIBDIR           install libraries in LIBDIR [$libdir]"
	echo
	echo "   --language=LANGUAGE           select language"
	echo
	echo "   --enable-static           build static versions [no]"
	echo "   --enable-doc              build documentation [yes]"
	echo "   --enable-utf8             build UTF-8 versions of applications [no]"
	echo
	echo "   --cc=CC                   use C compiler CC [$cc_default]"
	echo "   --cxx=CXX                 use C++ compiler CXX [$cxx_default]"
	echo "   --gengetopt=GGO           use gengetopt GGO [$gengetopt_default]"
	echo "   --flex=FLEX               use flex FLEX [$flex_default]"
	echo "   --makeinfo=MAKEINFO       use makeinfo MAKEINFO [$makeinfo_default]"
	echo "   --texi2dvi=TEXI2DVI       use texi2dvi TEXI2DVI [$texi2dvi_default]"
	echo "   --texi2pdf=TEXI2PDF       use texi2pdf TEXI2PDF [$texi2pdf_default]"
	echo "   --dvips=DVIPS             use dvips DVIPS [$dvips_default]"
	exit 1
}

die_unknown(){
	echo "Unknown option \"$1\"."
	echo "See $0 --help for availible options."
	exit 1
}

#
# default values
PREFIX="/usr/local"

bindir="${PREFIX}/bin"
confdir="/etc/utt"
sharedir="${PREFIX}/share"
docdir="${PREFIX}/share/doc/utt"
langdir="${PREFIX}/share/utt"
libdir="${PREFIX}/lib/utt"

languages="pl_PL.ISO-8859-2"
language="pl_PL.ISO-8859-2"

cc_default="gcc"
cxx_default="g++"
gengetopt_default="gengetopt"
flex_default="flex"
makeinfo_default="makeinfo"
texi2dvi_default="texi2dvi"
texi2pdf_default="texi2pdf"
dvips_default="dvips"

# default switch values
DEFAULT="
"

DEFAULT_NO="static
utf8
"

DEFAULT_YES="doc
"

CMDLINE_SELECT="$DEFAULT
  $DEFAULT_NO
  $DEFAULT_YES
"

enable $DEFAULT_YES
disable $DEFAULT_NO

for opt do
	optval="${opt#*=}"
	case "$opt" in
	--help)
		show_help
	;;
	--prefix=*)
		PREFIX="$optval"
    bindir="$optval/bin"
    sharedir="$optval/share"
    docdir="$optval/share/doc/utt"
    langdir="$optval/share/utt"
    libdir="$optval/lib/utt"
	;;
	--bindir=*)
		bindir="$optval"
	;;
	--confdir=*)
		confdir="$optval"
	;;
	--sharedir=*)
		sharedir="$optval"
	;;
	--docdir=*)
		docdir="$optval"
	;;
	--langdir=*)
		langdir="$optval"
	;;
	--libdir=*)
		libdir="$optval"
	;;
	--language=*)
		language="$optval"
	;;
	--cc=*)
		cc="$optval"
	;;
	--cxx=*)
		cxx="$optval"
	;;
	--gengetopt=*)
		gengetopt="$optval"
	;;
	--flex=*)
	        flex="$optval"
	;;
	--makeinfo=*)
		makeinfo="$optval"
	;;
	--texi2dvi=*)
		texi2dvi="$optval"
	;;
	--texi2pdf=*)
		texi2pdf="$optval"
	;;
	--dvips=*)
		dvips="$optval"
	;;
	--enable-?*|--disable-?*)
		eval `echo "$opt" | sed 's/--/action=/;s/-/ option=/;s/-/_/g'`
    echo "$CMDLINE_SELECT" | grep -q "^ *$option\$" || die_unknown $opt
		$action $option
	;;
	*)
		die_unknown $opt
	;;
	esac
done

if ! test -z $cc; then
	cc_default="${cc}"
fi
cc="${cc_default}"

if ! test -z $cxx; then
	cxx_default="${cxx}"
fi

cxx="${cxx_default}"

if ! test -z $gengetopt; then
	gengetopt_default="${gengetopt}"
fi

gengetopt="${gengetopt_default}"

if ! test -z $flex; then
  flex_default="$flex"
fi

flex="${flex_default}"

enabled doc && {
	if ! test -z $makeinfo; then
		makeinfo_default="${makeinfo}"
	fi
	if ! test -z $texi2dvi; then
		texi2dvi_default="${texi2dvi}"
	fi
	if ! test -z $texi2pdf; then
		texi2pdf_default="${texi2pdf}"
	fi
	if ! test -z $dvips; then
		dvips_default="${dvips}"
	fi
	
	makeinfo="${makeinfo_default}"
	texi2dvi="${texi2dvi_default}"
	texi2pdf="${texi2pdf_default}"
	dvips="${dvips_default}"
}

# generating config.mak

echo "# Automatically generated by configure" > config.mak
echo "PREFIX=$PREFIX" >> config.mak
echo "BIN_DIR=$bindir" >> config.mak
echo "CONF_DIR=$confdir" >> config.mak
echo "SHARE_DIR=$sharedir" >> config.mak
echo "DOC_DIR=$docdir" >> config.mak
echo "LANG_DIR=$langdir" >> config.mak
echo "LIB_DIR=$libdir" >> config.mak
echo "LANGUAGES=$languages" >> config.mak
echo "LANGUAGE=$language" >> config.mak
echo "CC=$cc" >> config.mak
echo "CXX=$cxx" >> config.mak
echo "GENGETOPT=$gengetopt" >> config.mak
echo "FLEX=$flex" >> config.mak
enabled doc && {
	echo "MAKEINFO=$makeinfo" >> config.mak
	echo "TEXI2DVI=$texi2dvi" >> config.mak
	echo "TEXI2PDF=$texi2pdf" >> config.mak
	echo "DVIPS=$dvips" >> config.mak
}
echo "BUILD_UTF8=$utf8" >> config.mak
echo "BUILD_STATIC=$static" >> config.mak
echo "BUILD_DOC=$doc" >> config.mak

enabled utf8 && {
  for cmp in $CMPLIST; do
    if [ -d "${SRC_DIR}/${cmp}_utf8" ]; then
      COMPONENTS="${COMPONENTS} ${cmp}_utf8"
    else
      COMPONENTS="${COMPONENTS} ${cmp}"
    fi
  done
}

disabled utf8 && {
  for cmp in $CMPLIST; do
    COMPONENTS="${COMPONENTS} ${cmp}"
  done
}
COMPONENTS=$(echo $COMPONENTS | cut -c1-)
echo "COMPONENTS=$COMPONENTS" >> config.mak

# echoing the configuration to console
echo "prefix:     $PREFIX"
echo "bindir:     $bindir"
echo "confdir:    $confdir"
echo "sharedir:   $sharedir"
echo "docdir:     $docdir"
echo "langdir:    $langdir"
echo "libdir:     $libdir"
echo "languages:  $languages"
echo "language:   $language"
enabled doc && {
echo "makeinfo:   $makeinfo"
echo "texi2dvi:   $texi2dvi"
echo "texi2pdf:   $texi2pdf"
echo "dvips:      $dvips"
}
echo "cc:         $cc"
echo "cxx:        $cxx"
echo "gengetopt:  $gengetopt"
echo "flex:       $flex"
echo "utf8:       $utf8"
echo "static:     $static"
echo "doc:        $doc"
