#!/bin/sh

# xreconfig, version 1.01
# For RedHat 6.0
# (c) 1999, Jan ONDREJ (SAL) <ondrejj@salstar.shadow.sk>
# GPL

if [ `id -u` -ne 0 ]; then
  echo "Prosim spustite tento program ako root."
  exit 0
fi

TMP=/tmp/xreconfig.$$

function acex() {
  rm -f $TMP
  clear
  exit $1
}

. /etc/sysconfig/i18n
. /etc/sysconfig/keyboard

function inputkbd() {
  if [ -x /usr/bin/dialog ]; then
    # Use dialog
    dialog --menu \
    "                 Vyberte si klávesnicu.\n
              TYP       PRIM. SEK. ROZLOZENIE" \
    21 60 12 \
    "us_sk_qwerty" " US    SK   QWERTY " \
    "us_sk_qwertz" " US    SK   QWERTZ " \
    "us_sk_prog  " " US    SK   PROG   " \
    "sk_us_qwerty" " SK    US   QWERTY " \
    "sk_us_qwertz" " SK    US   QWERTZ " \
    "sk_us_prog  " " SK    US   PROG   " \
    "us_cz_qwerty" " US    CZ   QWERTY " \
    "us_cz_qwertz" " US    CZ   QWERTZ " \
    "us_cz_prog  " " US    CZ   PROG   " \
    "cz_us_qwerty" " CZ    US   QWERTY " \
    "cz_us_qwertz" " CZ    US   QWERTZ " \
    "cz_us_prog  " " CZ    US   PROG   " \
    2> $TMP || acex 1
    export kbd=`cat $TMP | tr -d " "`
    rm -f $TMP
    clear
    echo $kbd
  else
  # without dialog
  cat << END
############################################################################
##               Nastavenie typu klavesnice pre XWindows                  ##
############################################################################

############################################################################
|			Prim.klav.	Sek.klav.	Rozlozenie         |
| us_sk_qwerty		US		SK		QWERTY             |
| us_sk_qwertz		US		SK		QWERTZ             |
| us_sk_prog		US		SK		programatorska     |
| sk_us_qwerty		SK		US		QWERTY             |
| sk_us_qwertz		SK		US		QWERTZ             |
| sk_us_prog		SK		US		programatorska     |
| us_cz_qwerty		US		CZ		QWERTY             |
| us_cz_qwertz		US		CZ		QWERTZ             |
| us_cz_prog		US		CZ		programatorska     |
| cz_us_qwerty		CZ		US		QWERTY             |
| cz_us_qwertz		CZ		US		QWERTZ             |
| cz_us_prog		CZ		US		programatorska     |
############################################################################

Prosim, vyberte si jeden z typov klavesnic a napiste cely jeho nazov.
Napr.: us_sk_qwerty
END

  read kbd
   export kbd
  if [ -z "$kbd" ]; then
    exit 0
  fi
  fi
}

function setfontpath() {
   export FSC=/etc/X11/fs/config

   mv /etc/X11/fs/config /etc/X11/fs/config.old

cat << END > $FSC
#
# Default font server configuration file for Red Hat Linux 6.0
#

# allow a max of 4 clients to connect to this font server
client-limit = 4

# when a font server reaches its limit, start up a new one
clone-self = off

# alternate font servers for clients to use
#alternate-servers = foo:7101,bar:7102

# where to look for fonts
# Some of these are commented out, i.e. the TrueType and Type1
# directories in /usr/share, because they aren't forced to be
# installed alongside X.
#
catalogue = 
END

export FFP=/usr/share/fonts/ISO8859-
export XFP=/usr/X11R6/lib/X11/fonts
SEP=0
function secho() {
  if [ "$SEP" = "0" ]; then
    SEP=1
  else
    echo ","
  fi
  echo -n "$1"
}

for i in 2/misc 2/75dpi 2/100dpi 2/Type1 9/misc 9/75dpi 9/100dpi; do
  if [ -d $FFP$i ]; then
    secho "        $FFP$i" >> $FSC
  fi
done

if [ -d $XFP/misc ]; then
  secho "        $XFP/misc:unscalled" >> $FSC
fi

for i in 75dpi 100dpi misc Type1 Speedo cyrillic; do
  if [ -d $XFP/$i ]; then
    secho "        $XFP/$i" >> $FSC
  fi
done

cat << END >> $FSC


# in 12 points, decipoints
default-point-size = 120

# 100 x 100 and 75 x 75
default-resolutions = 75,75,100,100

# how to log errors
use-syslog = on
END
}

function setxkbd() {
  cp -f /etc/X11/XF86Config /etc/X11/XF86Config.temp
  gawk '
    BEGIN {
      section=""
      kbd=ENVIRON["kbd"]
    }
    /^#.* ---comment_by_XKB-cs---$/ {
      if (kbd=="remove") {
        l=length($0)
        i=index($0," ---comment_by_XKB-cs---")
        print substr($0,2,i-2)
        next
      }
    }
    /^ *#/ { print; next }
    /^Section .*Keyboard/ {
      section="Keyboard";
      print; next
    }
    /^ *XkbLayout/ {
      if ((section=="Keyboard") && (kbd!="remove")) {
          print "#" $0 " ---comment_by_XKB-cs---"
          next
        fi
      }
    }
    /^ *XkbKeymap/ {
      if ((section=="Keyboard") && (kbd!="remove"))
        print "   XkbKeymap       \"xfree86(" kbd ")\""
      section=""
      next
    }
    /^EndSection/ {
      if ((section=="Keyboard") && (kbd!="remove"))
        print "   XkbKeymap       \"xfree86(" kbd ")\""
      section=""
      print; next
    }
    // { print }
  ' /etc/X11/XF86Config.temp > /etc/X11/XF86Config
  rm -f /etc/X11/XF86Config.temp
}

# MAIN
if [ "$1" = "auto" ]; then
  case `basename $KEYTABLE | sed 's/\.map//g'` in
    sk-qwerty)
	kbd=us_sk_qwerty
	;;
    sk-qwertz)
	kbd=us_sk_qwertz
	;;
    sk-prog-qwerty)
	kbd=us_sk_prog
	;;
    sk-prog-qwertz)
	kbd=us_sk_prog
	;;
    *)
	kbd=remove
	;;
  esac
  export kbd
  setxkbd
else
  if [ "$1" = "" ]; then
    inputkbd
  else
    export kbd="$1"
  fi
  setxkbd
fi

# call function setfontpath
setfontpath
