#!/bin/sh
# 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.
# Latest change: Mit Okt 04 11:54:45 CEST 2006 [mika]
################################################################################

PN="$(basename $0)"
DIALOG=dialog
CMDLINE=/proc/cmdline
LANGFUNC=/etc/grml/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

. /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 "$LANGFUNC could not be read. Make sure you have package grml-autoconfig installed." >&2
   exit 1
fi

setvalue(){
  [ -n "$2" ] || return 1
  # already present in conffile?
  if grep -q ${1} $CONFFILE ; then
     sed -i "s#^${1}.*#${1}${2}#"   $CONFFILE
  else
     echo "$1${2}" >> $CONFFILE
  fi
}

# grml-small does not provide any further locales
if grep -q small /etc/grml_version 2>/dev/null ; then
   if [ -z "$NONINTERACTIVE" ] ; then
      $DIALOG --stdout --msgbox "Notice: grml-small does not provide a full language setup.

You have to make sure the appropriate packages are installed." 0 0
      exit 1
   else
      esyslog user.notice "$PN" 'grml-small does not provide a full language setup.'
   fi
fi

[ -r /etc/environment ]    && . /etc/environment
[ -r /etc/default/locale ] && . /etc/default/locale
[ -n "$LANGUAGE" ] && DEFAULT_LANGUAGE="$LANGUAGE"

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

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

This will affect \$LANG, \$LANGUAGE, \$LC_MESSAGES and \$TZ.

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

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

if ! grep -q "${LANGUAGE})" $LANGFUNC ; then
   ewarn "Language ${LANGUAGE} not supported, using default." ; eend 0
fi

# read in the file where all the $LANGUAGE stuff is defined
  source $LANGFUNC

# make sure the file exists
if ! [ -r $CONFFILE ] ; then
cat > $CONFFILE <<EOF
# File generated by $PN on $(date)
LANGUAGE=$LANGUAGE
LANG=$LANG
LC_MESSAGES=$LANG
TZ=$TZ
# other environment variables you might want to set:
# LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY
# LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE
# LC_MEASUREMENT LC_IDENTIFICATION
# Notice: set LC_ALL to overwrite all LC_* variables
EOF
fi

setvalue 'LANGUAGE='    $LANGUAGE
setvalue 'LANG='        $LANG
setvalue 'LC_MESSAGES=' $LANG
setvalue 'TZ='          $TZ

retval=$?
case $retval in
    (0)
          if [ -z "$NONINTERACTIVE" ] ; then
             $DIALOG --stdout --msgbox "Writing language settings ($LANGUAGE) to $CONFFILE was successful." 0 0
          else
             einfo "Writing language settings ($LANGUAGE) to $CONFFILE was successful."
             esyslog user.notice "$PN" "Writing language settings ($LANGUAGE) to $CONFFILE was successful." ; eend 0
          fi
          ;;
    *)
          if [ -z "$NONINTERACTIVE" ] ; then
             $DIALOG --stdout --msgbox "Error writing settings for $LANGUAGE to $CONFFILE." 0 0
          else
             eerror "Error writing settings for $LANGUAGE to $CONFFILE." ; eend 1
             esyslog user.notice "$PN" "Error writing settings for $LANGUAGE to $CONFFILE."
          fi
          ;;
esac

eoutdent

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