#!/bin/bash
# Filename:      grml-setlang
# Purpose:       set language 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
LANGFUNC=/usr/share/grml-autoconfig/language-functions

# notice: Debian's locales.postinst has been modified to write
# locale variables into /etc/default/locale instead of
# /etc/environment; the latter file is a PAM configuration file,
# so modifying it was a policy violation.
CONFFILE=/etc/default/locale

# shellcheck disable=SC1091
{
. /etc/grml/script-functions
. /etc/grml/lsb-functions
}

check4root || exit 100

eindent # as we are running inside grml boot sequence as well make sure we integrate fine

# allow writing $CONFFILE non-interactive via "grml-setlang $LANGUAGE"
if [ -n "$1" ] ; then
   NONINTERACTIVE=1
else
   NONINTERACTIVE=''
fi

if ! [ -r "$LANGFUNC" ] ; then
   echo "E: $LANGFUNC could not be read." >&2
   exit 1
fi

# Generate locale if necessary
generate_locale() {
  local locale="$1"

  # If locales-all is installed, we don't need to generate anything
  if dpkg -s locales-all >/dev/null 2>&1 ; then
    return 0
  fi

  # Check if locale is already in /etc/locale.gen
  if ! grep -q "^$locale " /etc/locale.gen 2>/dev/null ; then
    # Add the locale to /etc/locale.gen
    echo "$locale UTF-8" >> /etc/locale.gen

    # Remove autogenerated markers if present
    sed -i --follow-symlinks '/^# This file was created by grml-live/d' /etc/locale.gen
    sed -i --follow-symlinks '/^# XXX GENERATED XXX/d' /etc/locale.gen

    # Generate the locale
    locale-gen --keep-existing >/dev/null 2>&1
  fi
}

# shellcheck disable=SC1091
{
[ -r /etc/environment ]    && . /etc/environment
[ -r /etc/default/locale ] && . /etc/default/locale
}
[ -n "$LANGUAGE" ] && DEFAULT_LANGUAGE="$LANGUAGE"

if [ -z "$DEFAULT_LANGUAGE" ] ; then
  # Austrian English
  DEFAULT_LANGUAGE=en
fi

if [ -z "$NONINTERACTIVE" ] ; then
# shellcheck disable=SC1010
{
   LANGUAGE=$(LANG=C $DIALOG --stdout --title "$PN" --default-item "$DEFAULT_LANGUAGE" --radiolist \
"Which language do you want to use?

This will affect \$LANG, \$LANGUAGE, \$LC_COLLATE,
\$LC_TIME and \$TZ.

Notice: if you want to adjust /etc/locale.gen (defines
which locales should be generated by locale-gen)
please run 'dpkg-reconfigure locales' manually.

Configuration will be written to $CONFFILE" 0 0 0 \
 at 'austrian' off \
 au 'austrialian' off \
 be 'belgian' off \
 bg 'bulgarian' off \
 br 'brazilian' off \
 ch 'swiss-german' off \
 cf 'french canadian' off \
 cn 'chinese' off \
 cs 'czech' off \
 cz 'czech' off \
 de 'german' off \
 dk 'dansk' off \
 da 'dansk' off \
 el 'greek' off \
 en 'english [at] (grml default)' on \
 es 'spanish' off \
 fi 'finnish' off \
 fr 'french' off \
 ga 'irish gaeilge' off \
 he 'hebrew' off \
 hu 'hungarian' off \
 il 'hebrew' off \
 ie 'irish' off \
 it 'italian' off \
 ja 'japanese' off \
 nl 'dutch' off \
 pl 'polish' off \
 pt 'portuguese' off \
 ru 'russian' off \
 sk 'slovak' off \
 sl 'slovenian' off \
 tr 'turkish' off \
 tw 'chinese (traditional)' off \
 uk 'british' off \
 us 'american' off \
)
}

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

else # non-interactive
  LANGUAGE="$1"
fi

XKBLAYOUT=""
# read in the file where all the $LANGUAGE stuff is defined
# shellcheck disable=SC1090
. $LANGFUNC

if [ -z "$XKBLAYOUT" ] ; then
  if [ -z "$NONINTERACTIVE" ] ; then
    LANG=C $DIALOG --stdout --msgbox "Language ${LANGUAGE} not supported, aborting." 0 0
    exit 1
  else
    eerror "Language ${LANGUAGE} not supported, aborting." ; eend 1
    exit 1
  fi
fi

# Generate locale if necessary
generate_locale "$LANG"

LC_COLLATE="${LC_COLLATE:-$LANG}"
LC_TIME="${LC_TIME:-$LANG}"
cat > $CONFFILE <<EOF
# File generated by $PN on $(date)
LANG=$LANG
LANGUAGE=$LANGUAGE
LC_COLLATE=$LC_COLLATE
LC_TIME=$LC_TIME
TZ=$TZ
EOF

if [ -z "$NONINTERACTIVE" ] ; then
  LANG=C $DIALOG --stdout --msgbox "Wrote settings for language $LANGUAGE into $CONFFILE." 0 0
else
  einfo "Wrote settings for language $LANGUAGE into $CONFFILE."
  esyslog user.notice "$PN" "Wrote settings for language $LANGUAGE into $CONFFILE." ; eend 0
fi

eoutdent

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