initial checkin
[grml-scripts.git] / usr_sbin / grml-setlang
1 #!/bin/sh
2 # Filename:      grml-setlang
3 # Purpose:       set language system-wide on grml system
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Mit Okt 04 11:54:45 CEST 2006 [mika]
8 ################################################################################
9
10 PN="$(basename $0)"
11 DIALOG=dialog
12 CMDLINE=/proc/cmdline
13 LANGFUNC=/etc/grml/language-functions
14
15 # notice: Debian's locales.postinst has been modified to write
16 # locale variables into /etc/default/locale instead of
17 # /etc/environment; the latter file is a PAM configuration file,
18 # so modifying it was a policy violation.
19 CONFFILE=/etc/default/locale
20
21 . /etc/grml/script-functions
22 . /etc/grml/lsb-functions
23
24 check4root || exit 100
25
26 eindent # as we are running inside grml boot sequence as well make sure we integrate fine
27
28 # allow writing $CONFFILE non-interactive via "grml-setlang $LANGUAGE"
29 if [ -n "$1" ] ; then
30    NONINTERACTIVE=1
31 else
32    NONINTERACTIVE=''
33 fi
34
35 if ! [ -r "$LANGFUNC" ] ; then
36    echo "$LANGFUNC could not be read. Make sure you have package grml-autoconfig installed." >&2
37    exit 1
38 fi
39
40 setvalue(){
41   [ -n "$2" ] || return 1
42   # already present in conffile?
43   if grep -q ${1} $CONFFILE ; then
44      sed -i "s#^${1}.*#${1}${2}#"   $CONFFILE
45   else
46      echo "$1${2}" >> $CONFFILE
47   fi
48 }
49
50 # grml-small does not provide any further locales
51 if grep -q small /etc/grml_version 2>/dev/null ; then
52    if [ -z "$NONINTERACTIVE" ] ; then
53       $DIALOG --stdout --msgbox "Notice: grml-small does not provide a full language setup.
54
55 You have to make sure the appropriate packages are installed." 0 0
56       exit 1
57    else
58       esyslog user.notice "$PN" 'grml-small does not provide a full language setup.'
59    fi
60 fi
61
62 [ -r /etc/environment ]    && . /etc/environment
63 [ -r /etc/default/locale ] && . /etc/default/locale
64 [ -n "$LANGUAGE" ] && DEFAULT_LANGUAGE="$LANGUAGE"
65
66 if [ -z "$DEFAULT_LANGUAGE" ] ; then
67    DEFAULT_LANGUAGE=en
68 fi
69
70 if [ -z "$NONINTERACTIVE" ] ; then
71    LANGUAGE=$($DIALOG --stdout --title "$PN" --default-item $DEFAULT_LANGUAGE --radiolist \
72 "Which language do you want to use?
73
74 This will affect \$LANG, \$LANGUAGE, \$LC_MESSAGES and \$TZ.
75
76 Notice: if you want to adjust /etc/locale.gen (defines
77 which locales should be generated by locale-gen)
78 please run 'dpkg-reconfigure locale' manually.
79
80 Configuration will be written to $CONFFILE" 0 0 0 \
81  at austrian off \
82  at-utf8 'austria (unicode version)' off \
83  au australian off \
84  au-utf8 'austrial (unicode version)' off \
85  be belgian off \
86  be-utf8 'belgian (unicode version)' off \
87  bg bulgarian off \
88  bg-utf8 'bulgarian (unicode version)' off \
89  br brazilian off \
90  br-utf8 'brazilian (unicode version)' off \
91  ch swiss off \
92  ch-utf8 'swiss (unicode version)' off \
93  cf 'french canadian' off \
94  cn chinese off \
95  cn-utf8 'chinese (unicode version)' off \
96  cs czech off \
97  cs-utf8 'czech (unicode version)' off \
98  cz czech off \
99  cz-utf8 'czech (unicode version)' off \
100  de german off \
101  de-utf8 'german (unicode version)' off \
102  dk dansk off \
103  dk-utf8 'dansk (unicode version)' off \
104  da dansk off \
105  da-utf8 'dansk (unicode version)' off \
106  el greek off \
107  el-utf8 'greek (unicode version)' off \
108  en 'english [us] (default)' on \
109  es spanish off \
110  es-utf8 'spanish (unicode version)' off \
111  fi finnish off \
112  fi-utf8 'finnish (unicode version)' off \
113  fr frensh off \
114  fr-utf8 'frensh (unicode version)' off \
115  ga 'irish gaeilge' off \
116  ga-utf8 'irish gaeilge (unicode version)' off \
117  he hebrew off \
118  he-utf8 'hebrew (unicode version)' off \
119  il hebrew off \
120  il-utf8 'hebrew (unicode version)' off \
121  ie irish off \
122  ie-utf8 'irish (unicode version)' off \
123  it italian off \
124  it-utf8 'italian (unicode version)' off \
125  ja japanese off \
126  ja-utf8 'japanese (unicode version)' off \
127  nl dutch off \
128  nl-utf8 'dutch (unicode version)' off \
129  pl off polish \
130  pl-utf8 'polish (unicode version)' off \
131  pt portuguese off \
132  pt-utf8 'portuguese (unicode version)' off \
133  ru russian off \
134  ru-utf8 'russian (unicode version)' off \
135  sk slovak off \
136  sk-utf8 'slovak (unicode version)' off \
137  sl slovenian off \
138  sl-utf8 'slovenian (unicode version)' off \
139  tr turkish off \
140  tr-utf8 'turkish (unicode version)' off \
141  tw 'chinese (traditional)' off \
142  tw-utf8 'chinese (traditional) (unicode version)' off \
143  uk british off \
144  uk-utf8 'british (unicode version)' off \
145  us-utf8 'american (unicode version)' off \
146 )
147
148   retval=$?
149   case $retval in
150       (0)   # everything ok
151             ;;
152       (1)   echo "Cancel pressed." ; exit 1 ;;
153       (255) echo "ESC pressed."    ; exit 1 ;;
154   esac
155
156 else # non-interactive
157   LANGUAGE="$1"
158 fi
159
160 if ! grep -q "${LANGUAGE})" $LANGFUNC ; then
161    ewarn "Language ${LANGUAGE} not supported, using default." ; eend 0
162 fi
163
164 # read in the file where all the $LANGUAGE stuff is defined
165   source $LANGFUNC
166
167 # make sure the file exists
168 if ! [ -r $CONFFILE ] ; then
169 cat > $CONFFILE <<EOF
170 # File generated by $PN on $(date)
171 LANGUAGE=$LANGUAGE
172 LANG=$LANG
173 LC_MESSAGES=$LANG
174 TZ=$TZ
175 # other environment variables you might want to set:
176 # LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY
177 # LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE
178 # LC_MEASUREMENT LC_IDENTIFICATION
179 # Notice: set LC_ALL to overwrite all LC_* variables
180 EOF
181 fi
182
183 setvalue 'LANGUAGE='    $LANGUAGE
184 setvalue 'LANG='        $LANG
185 setvalue 'LC_MESSAGES=' $LANG
186 setvalue 'TZ='          $TZ
187
188 retval=$?
189 case $retval in
190     (0)
191           if [ -z "$NONINTERACTIVE" ] ; then
192              $DIALOG --stdout --msgbox "Writing language settings ($LANGUAGE) to $CONFFILE was successful." 0 0
193           else
194              einfo "Writing language settings ($LANGUAGE) to $CONFFILE was successful."
195              esyslog user.notice "$PN" "Writing language settings ($LANGUAGE) to $CONFFILE was successful." ; eend 0
196           fi
197           ;;
198     *)
199           if [ -z "$NONINTERACTIVE" ] ; then
200              $DIALOG --stdout --msgbox "Error writing settings for $LANGUAGE to $CONFFILE." 0 0
201           else
202              eerror "Error writing settings for $LANGUAGE to $CONFFILE." ; eend 1
203              esyslog user.notice "$PN" "Error writing settings for $LANGUAGE to $CONFFILE."
204           fi
205           ;;
206 esac
207
208 eoutdent
209
210 ## END OF FILE #################################################################