#! /bin/sh

# kbconf 1.0.0
#   "Danube" Serbian XKB keymap configuration tool.
#   April 23th, 2003.
#
# Authors:
#   Danilo Segan (Данило Шеган) <dsegan@gmx.net>
#   Chusslove Illich (Часлав Илић) <chaslav@sezampro.yu>
#
# NOTE: Type "kbconf -h" for help.

# ----------------------------------------------------------------------------
# Set some variables.

# Source directory is path to kbconf script.
SRC=`dirname $0`

# Set command line string.
CMDLINE="$*"

# List of all possible command line options.
ALLOPTIONS="\
--help \
-h \
--GTK2 \
--Alt-us \
"

# Makefile substitution script filename.
KBS=kbconf.status

# Configure time-stamp filename.
KBT=kbconf.stamp

# ----------------------------------------------------------------------------
# Some functions.

# Clean up temporary files generated during configure.
kc_cleanup () {
    rm -f $KBS Makefile.tmp
    touch $KBT
}

# Display error description string and exit.
kc_error () {
    echo
    kc_cleanup
    rm -f Makefile
    echo "kbconf: error: " $*
    exit 1
}

# Check if given option ($1) is set in command line.
kc_option_set () {
    SET=false
    for i in $CMDLINE; do
        if [ x$1 == x$i ]; then
            SET=true
            break
        fi
    done

    if [ $SET == true ]; then
        true
    else
        false
    fi
}

kc_subst () {
    VAR=@$1@
    SUBST=`echo $2 | sed 's/\\//\\\\\\//g'`
    echo "-e 's/$VAR/$SUBST/g;' \\" >>$KBS
}

# ----------------------------------------------------------------------------
# Assure that there are no unknown options in command line.

OK=true
for i in $CMDLINE; do
    OK=false
    for j in $ALLOPTIONS; do
        if [ x$i == x$j ]; then
            OK=true
            break
        fi
    done

    if [ $OK == false ]; then
        break
    fi
done
if [ $OK == false ]; then
    echo "Unknown option: $i"
    exit 1
fi

# ----------------------------------------------------------------------------
# Display help if -h or --help option set.

if kc_option_set "-h" || kc_option_set "--help"; then
    cat <<_EOF

"Danube" Serbian XKB keymap configuration tool.

Usage:
    kbconf [OPTIONS]

Options:
    --help, -h
        Display this help.
    --GTK2
        Needed for correct key behaviour in GTK2 apps. However, with this
        setting, keys may not behave correctly in some other, non-GTK2 apps.
    --Alt-us
        Make Alt-key combinations always use US layout.

Report bugs to <chaslav@sezampro.yu>

_EOF
    exit 0
fi

# ----------------------------------------------------------------------------
# Find needed paths, files, etc.

# Find XKB configuration directory.
echo -n "Looking for XKB configuration directory... "
if [ -d /etc/X11/xkb ]; then
    XKBCONF=/etc/X11/xkb
else
    Xpath=`which X`
    XROOT=`dirname $Xpath | sed 's+/bin++'`
    XKBCONF=$XROOT/lib/X11/xkb
    if [ ! -d $XKBCONF ]; then
        kc_error "Cannot find XKB configuration directory."
    fi
fi
echo "ok ($XKBCONF/)"

# Find XKB configuration directory.
echo -n "Looking for XKB symbols directory... "

if [ -d $XKBCONF/symbols/pc ]; then
    EXISTSPC=yes
elif [ -d $XKBCONF/symbols ]; then
    EXISTSPC=no
else
    kc_error "Cannot find XKB symbols directory."
fi
XKBSYMBOLS=$XKBCONF/symbols
echo "ok ($XKBSYMBOLS/)"

# Assure XKB type file "complete" exists.
echo -n "Looking for XKB type file \"complete\"... "
XKBTYPES=$XKBCONF/types
if ! [ -f $XKBTYPES/complete ]; then
    kc_error "Cannot find XKB type file \"complete\"."
fi
echo "ok ($XKBCONF/types/complete)"

# ----------------------------------------------------------------------------
# Start $KBS script.
echo '#! bin/sh' >$KBS
echo 'less '$SRC'/$1.in | sed \' >>$KBS

# ----------------------------------------------------------------------------
# Set output variables according to options (add sed substitutions to $KBS
# script).

echo -n "Processing options... "

# Source directory.
kc_subst srcdir $SRC

# Build directory is current directory.
kc_subst builddir .

# XKB configuration directory.
kc_subst xkbconf $XKBCONF

# XKB symbols directory.
kc_subst xkbsymbols $XKBSYMBOLS
kc_subst exists_pc $EXISTSPC

# XKB types directory.
kc_subst xkbtypes $XKBTYPES

# Option: --GTK2
if kc_option_set "--GTK2"; then
    kc_subst gtk2 yes
else
    kc_subst gtk2 no
fi

# Option: --Alt-us
if kc_option_set "--Alt-us"; then
    kc_subst alt_us yes
else
    kc_subst alt_us no
fi

# ----------------------------------------------------------------------------
# End $KBS script.
echo '>$1.tmp' >>$KBS

echo "ok"

# ----------------------------------------------------------------------------
# Substitute output varables in Makefile.in.

echo -n "Creating makefile... "

# Substitute output variables (creates *.tmp from *.in files).
/bin/sh kbconf.status Makefile
/bin/sh kbconf.status makesymbolfile

# Add configure time-stamp to every target.
less Makefile.tmp | sed "s/^.*:/& $KBT/" >Makefile

# Prepare script.
mv -f makesymbolfile.tmp makesymbolfile
chmod u=rwx makesymbolfile

# Assure that all output variables were substituted in Makefile.
if grep -q -e "@.*@" Makefile; then
    kc_error "Some output variables were not substituted in Makefile."
fi

echo "ok"

# ----------------------------------------------------------------------------
# Clean up.

kc_cleanup

# ----------------------------------------------------------------------------
