#!/bin/sh
# Filename:      grml-setkeyboard
# Purpose:       set keyboard layout system-wide on grml system
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

PN="$(basename $0)"
DIALOG=dialog
CMDLINE=/proc/cmdline

. /etc/grml/script-functions

check4root || exit 1

setvalue(){
  [ -n "$2" ] || return 1
  # already present in conffile?
  if grep -q ${1} $CONFFILE ; then
     sed -i "s#^${1}.*#${1}=${2}#"   $CONFFILE
  else
     echo "$1=${2}" >> $CONFFILE
  fi
}

# same for strings
stringinstring(){
  case "$2" in *$1*) return 0;; esac
  return 1
}

# Reread boot command line; echo last parameter's argument or return false.
getbootparam(){
  stringinstring " $1=" "$CMDLINE" || return 1
  result="${CMDLINE##*$1=}"
  result="${result%%[ 	]*}"
  echo "$result"
  return 0
}

[ -r /etc/sysconfig/keyboard ] && . /etc/sysconfig/keyboard

[ -n "$KEYTABLE" ] && DEFAULT_KEYBOARD="$KEYTABLE"
if [ -z "$DEFAULT_KEYBOARD" ] ; then
  DEFAULT_KEYBOARD="$(getbootparam lang 2>/dev/null)"
  if [ -z "$DEFAULT_KEYBOARD" ] ; then
    DEFAULT_KEYBOARD=en
  fi
fi

LANGUAGE=$(LANG=C $DIALOG --stdout --title "$PN" --default-item $DEFAULT_KEYBOARD --radiolist \
"Which keyboard layout do you want to use?

Please notice that this will not really affect your currently running system.
If you want to change keyboard settings temporarly please use grml-lang instead.

Configuration will be written to /etc/sysconfig/keyboard" 0 0 0 \
 at  austrian off \
 be  belgian off \
 bg  bulgarian off \
 cf  "french canadian" off \
 ch  swiss off \
 cn  chinese off \
 cs  czech off \
 cz  czech off \
 da  da off \
 de  german   off \
 dk  dansk off \
 en  "english [us] (default)" on \
 es  spanish off \
 fi  finnish off \
 fr  french off \
 he  hebrew off \
 ie  irish off \
 il  hebrew off \
 it  italian off \
 ja  japanese off \
 nl  dutch off \
 pl  polish off \
 ru  russian off \
 sk  slovak off \
 sl  slovenian off \
 tr  turkish off \
 tw  "chinese (traditional)" off \
 uk  british off \
)

retval=$?
case $retval in
    (0)
          # everything ok
          ;;
    (1)   echo "Cancel pressed." ; exit 1 ;;
    (255) echo "ESC pressed."    ; exit 1 ;;
esac

# read in the file where all the $LANGUAGE stuff is defined
  . /etc/grml/language-functions

cat > /etc/sysconfig/keyboard <<EOF
# File generated by $PN on $(date)
KEYTABLE="$KEYTABLE"
XKEYBOARD="$XKEYBOARD"
KDEKEYBOARD="$KDEKEYBOARD"
KDEKEYBOARDS="$KDEKEYBOARD"
EOF

retval=$?
case $retval in
    (0)
          # everything ok
          LANG=C $DIALOG --stdout --msgbox "Writing keyboard settings ($KEYTABLE) to /etc/sysconfig/keyboard was successful." 0 0
          ;;
    *)
          LANG=C $DIALOG --stdout --msgbox "Error writing settings for $KEYTABLE to /etc/sysconfig/keyboard." 0 0
          ;;
esac

## END OF FILE #################################################################
