#!/bin/sh

# merge of all bakends (sysfont syskbd syslang) of alterator-sysconfig

alterator_api_version=1
po_domain="alterator-sysconfig"

. alterator-sh-functions
. shell-config

ALTCONFDIR=/etc/alterator/sysconfig/
KBDDIR=$ALTCONFDIR/kbd
CONFIGDIR=/etc/sysconfig/
KBD_CONFIG=/etc/sysconfig/keyboard
XKB_CONFIG=/etc/X11/xinit/Xkbmap
LOCALECTL=/usr/bin/localectl
export DISPLAY=${DISPLAY:-:0}

i18n_conf="/etc/sysconfig/i18n"
langmap_conf="/etc/sysconfig/langmap"
rpm_conf="/etc/rpm/macros"
localectl_conf="/etc/locale.conf"

langlist="/etc/alterator/sysconfig/lang/langlist"
po_charset="UTF-8"

# sysconfig-kbd stuff

write_layout(){
  case "$1" in
    alt_sh_toggle) echo "`_ "Alt+Shift key"`";;
    caps_toggle) echo "`_ "CapsLock key"`";;
    ctrl_shift_toggle) echo "`_ "Control+Shift keys"`";;
    ctrl_toggle) echo "`_ "Control key"`";;
    toggle) echo "`_ "Alt key"`";;
    ctrl_shift_toggle_ru_ua) echo "`_ "Control+Shift keys (UK,RU,EN)"`";;
    default) echo "`_ "Default"`";;
    nodeadkeys) echo "`_ "Without dead keys"`";;
    *) echo "$1";;
  esac
}

write_kbd(){
  while read name;do
    name="${name##*/}"
    name="${name%%.xkb}"
    write_enum_item "$name" "$(write_layout "$name")"
  done
}

default_kbd(){
    shell_config_get "$KBDDIR/kbdlist" ${in_language%%;*} '[[:space:]]\+'
}

# sysconfig-language stuff

write_locale(){
  local l="$1";shift
  [ "$l" != "POSIX" ] && echo "$l.$po_charset" || echo "$l"
}

write_locale_conf(){
  local lang="$1";shift
  if [ -e "$localectl_conf" ]; then
    # Replace old value of LANG variable with new or append it
    sed -i "/^LANG=/{h;s/=.*/=$lang/};\${x;/^$/{s//LANG=$lang/;H};x}" "$localectl_conf"
  else
    printf 'LANG=%s\n' "$lang">"$localectl_conf"
  fi
}

### initial actions
shell_config_set "$CONFIGDIR/consolefont" SYSFONT UniCyr_8x16

### message loop

on_message(){
  case $in__objects in
    kbd)
      case "$in_action" in
        read)
          write_string_param layout "$(default_kbd)"
        ;;
        list)
          local language="${in_language%%;*}"
          [ -d "$KBDDIR/$language" ] &&
            find "$KBDDIR/$language" -name '*.xkb'|write_kbd
        ;;
        write)
          local language="${in_language%%;*}"
          if [ -n "$in_layout" ]; then
            local path="$KBDDIR/$language/$in_layout"
	    # update x11 keyboard mapping
	    install -Dpm644 "$path.xkb" "$XKB_CONFIG" &&
		/usr/bin/setxkbmap -option "" &&
		/usr/bin/setxkbmap `cat "$XKB_CONFIG"`
	    # update console keyboard mapping
	    install -Dpm644 "$path.kbd" "$KBD_CONFIG" &&
		/sbin/setsyskeytable >&2
	  else
	    rm -f "$KBD_CONFIG" "$XKB_CONFIG"
	  fi
        ;;
      esac
    ;;
    language)
      case "$in_action" in
        list)
          local lang_file="$langlist.${in_language%%;*}"
          [ -s "$lang_file" ] || lang_file="$langlist.all"
          write_enum <"$lang_file"
        ;;
        write)
          mkdir -p -- "${i18n_conf%/*}"
          mkdir -p -- "${langmap_conf%/*}"
          mkdir -p -- "${rpm_conf%/*}"

          local firstlang="${in_lang%%;*}"

          printf 'LANG=%s\n' "$(write_locale "$firstlang")">"$i18n_conf"

          local lang=$(write_locale "$firstlang")

          if [ -x "$LOCALECTL" ]; then
            $LOCALECTL set-locale LANG=$lang
            if [ $? -ne 0 ]; then
              write_locale_conf "$lang"
            fi
          else
            write_locale_conf "$lang"
          fi

          if echo "$in_language" | fgrep -qs ';'; then
            printf 'SUPPORTED=%s\n' "$(write_language "$in_language")">>"$i18n_conf"
            printf '%s\n' "$(write_language "$in_language")">"$langmap_conf"
          else
            printf 'SUPPORTED=%s\n' "$(write_locale "$firstlang")">>"$i18n_conf"
          fi

          # Hack for tt locale
          if [ "${firstlang%.*}" = 'tt_RU' ]; then
			  echo 'LANGUAGE=tt:ru' >>"$i18n_conf"
			  subst 's/^SUPPORTED=.*$/SUPPORTED=tt_RU.UTF-8:ru_RU.UTF-8/' "$i18n_conf"
		  fi

          sed 's,^%_install_langs[[:space:]].*,%_install_langs all,' -i "$rpm_conf"
        ;;
      esac
    ;;
  esac
}

message_loop
