#!/bin/sh
#
#
# chkconfig: 345 04 05
# description: This startup script sets locale from /proc/cmdline

WITHOUT_RC_COMPAT=1
KBDDIR=/etc/alterator/sysconfig/kbd
KBD_CONFIG=/etc/sysconfig/keyboard
XKB_CONFIG=/etc/X11/xinit/Xkbmap
X11_KEYBOARD_CONF="/etc/X11/xorg.conf.d/00-keyboard.conf"
CONFIGDIR=/etc/sysconfig/
DCONF_HOOK=/etc/profile.d/00dconf-kbd.sh

vconsole_conf="/etc/vconsole.conf"
i18n_conf="/etc/sysconfig/i18n"
langmap_conf="/etc/sysconfig/langmap"
rpm_conf="/etc/rpm/macros"

# Source function library.
. /etc/init.d/functions
. shell-config

RETVAL=0

dconf_add()
{
	if [ -d /etc/dconf/db ]; then
		echo "$*" >> "$DCONF_HOOK"
		chmod +x "$DCONF_HOOK"
	fi
}

start()
{
	local lang kbmap layout options
	lang="`sed -n 's/^.*\<lang=\([a-z_A-Z]\+\)\>.*$/\1/p' /proc/cmdline`"
	[ -n "$lang" ] || return 0

	# Keyboard layout setup
	kbmap=`[ -d "$KBDDIR/$lang" ] &&
		find "$KBDDIR/$lang" -name '*.xkb' |
		head -n 1 |
		sed 's,^.*/\(.*\)\.xkb$,\1,'`

	# ctrl+shift is the only one available for be/uk right now but still
	case "$lang" in
	ru_RU) kbmap='alt_sh_toggle';;
	be_BY|tt_RU|uk_UA) kbmap='ctrl_shift_toggle';;
	esac

	if [ -n "$kbmap" ]; then
		path="$KBDDIR/$lang/$kbmap"
		# update x11 keyboard mapping
		if [ -n "$DISPLAY" ]; then
			setxkbmap -option ""
			setxkbmap `cat "$path".xkb`
		fi

		layout=`shell_config_get "$path".xkb "-layout" "[[:space:]]" | awk '$1=$1' 2>/dev/null`
		options=`shell_config_get "$path".xkb "-option" "[[:space:]]" | awk '$1=$1' 2>/dev/null`
		# update console keyboard mapping
		install -Dpm644 "$path.kbd" "$KBD_CONFIG" && /sbin/setsyskeytable >/dev/null
		# systemd config console
		if [ -f "$vconsole_conf" ]; then
			sed -i '/^KEYMAP/d' "$vconsole_conf"
			sed "s/KEYTABLE=/KEYMAP=/" "$path.kbd" >> "$vconsole_conf"
		fi
	else
		# create empty keyboard config
		install -Dpm644 /dev/null "$KBD_CONFIG"
		# clean systemd config console
		[ -f "$vconsole_conf" ] && sed -i '/^KEYMAP/d' "$vconsole_conf"
	fi
	# cleanup old unusable $XKB_CONFIG
	install -Dpm644 /dev/null "$XKB_CONFIG"
	[ -n "$layout" ] || layout=us
	# create default x11 keyboard config
	cat > "$X11_KEYBOARD_CONF" << EOF
Section "InputClass"
Identifier "system-keyboard"
MatchIsKeyboard "on"
	Option "XkbLayout" "$layout"
	Option "XkbOptions" "$options"
EndSection
EOF

	# Default language setup
	mkdir -p -- "${i18n_conf%/*}"
	mkdir -p -- "${langmap_conf%/*}"
	mkdir -p -- "${rpm_conf%/*}"

	locale="$lang.UTF-8"

	LOCALECTL=/usr/bin/localectl
	[ ! -x "$LOCALECTL" ] ||
		"$LOCALECTL" set-locale LANG="$locale"

	printf 'LANG=%s\n' "$locale" > "$i18n_conf"

	# FIXME: this is rather related to alterator module and irrelevant here
	if [ -z "${lang##*;*}" ]; then
		printf 'SUPPORTED=%s\n' "$locale" >> "$i18n_conf"
		printf '%s\n' "$locale" > "$langmap_conf"
	else
		printf 'SUPPORTED=%s\n' "$locale" >> "$i18n_conf"
	fi

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

	# Locale specific hacks
	case "$lang" in
	tt_RU)
		echo 'LANGUAGE=tt:ru' >>"$i18n_conf"
		subst 's/^SUPPORTED=.*$/SUPPORTED=tt_RU.UTF-8:ru_RU.UTF-8/' "$i18n_conf"
		printf '%s\n' "tt_RU.UTF-8:ru_RU.UTF-8" > "$langmap_conf"
		;;
	ru_UA)
		echo 'LANGUAGE=ru:uk' >>"$i18n_conf"
		subst 's/^SUPPORTED=.*$/SUPPORTED=ru_UA.UTF-8:ru_RU.UTF-8:uk_UA.UTF-8/' "$i18n_conf"
		printf '%s\n' "ru_UA.UTF-8:ru_RU.UTF-8:uk_UA.UTF-8" > "$langmap_conf"
		;;
	esac

	# Set language for indexhtml
	UPDATE=/usr/sbin/indexhtml-update
	if [ -x "$UPDATE" ]; then
		LANG="$locale" "$UPDATE"
	fi
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	start|stop|reload|restart|condstop|condrestart|condreload|status)
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
		;;
esac

exit $RETVAL
