#!/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
CONFIGDIR=/etc/sysconfig/
DCONF_HOOK=/etc/profile.d/00dconf-kbd.sh

i18n_conf="/etc/sysconfig/i18n"
langmap_conf="/etc/sysconfig/langmap"
rpm_conf="/etc/rpm/macros"

# Source function library.
. /etc/init.d/functions

RETVAL=0

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

start()
{
	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
	be_BY|ru_RU|tt_RU|uk_UA) kbmap='ctrl_shift_toggle';;
	esac

	if [ -n "$kbmap" ]; then
		path="$KBDDIR/$lang/$kbmap"
		# update x11 keyboard mapping
		install -Dpm644 "$path.xkb" "$XKB_CONFIG"
		if [ -n "$DISPLAY" ]; then
			setxkbmap -option ""
			setxkbmap `cat "$XKB_CONFIG"`
		fi
		# update console keyboard mapping
		install -Dpm644 "$path.kbd" "$KBD_CONFIG" && /sbin/setsyskeytable >/dev/null

		# handle gnome3 in a special way :-/
		GSHELL="/usr/bin/gnome-shell"
		CINNAMON="/usr/bin/startcinnamon"
		if [ -x "$GSHELL" -o -x "$CINNAMON" ]; then
			layouts=
			comma=
			_IFS="$IFS"; IFS=','

			# NB: alterator-sysconfig has tabs instead of spaces in those files
			for l in `sed -n 's/^.*-layout[[:blank:]]\+\([a-z,]\+\).*$/\1/p' "$XKB_CONFIG"`; do
				[ -z "$layouts" ] || comma=", "
				layouts="$layouts$comma('xkb', '$l')"
			done

			IFS="$_IFS"; unset _IFS

			dconf_add "#!/bin/sh"
			dconf_add "[ \"\$EUID\" -gt 0 ] || return 0"
			dconf_add "dconf write /org/gnome/desktop/input-sources/sources \"[$layouts]\" 2>/dev/null"
			dconf_add "dconf write /org/gnome/desktop/input-sources/xkb-options \"['grp:$kbmap']\" 2>/dev/null"
		fi
	else
		rm -f "$KBD_CONFIG" "$XKB_CONFIG"
	fi

	# 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
