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