merged grml-quickconfig nonblocking branch
[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 Sep 05 18:33:23 CEST 2007 [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   # is the new Debian style /etc/default/locale present?
46   elif grep -q "^# ${1}$" "$CONFFILE" ; then
47      sed -i "s#^\# ${1}#${1}${2}#" $CONFFILE
48   else
49      echo "$1${2}" >> $CONFFILE
50   fi
51 }
52
53 # grml-small does not provide any further locales
54 if grep -q small /etc/grml_version 2>/dev/null ; then
55    if [ -z "$NONINTERACTIVE" ] ; then
56       LANG=C $DIALOG --stdout --msgbox "Notice: grml-small does not provide a full language setup.
57
58 You have to make sure the appropriate packages are installed." 0 0
59       exit 1
60    else
61       esyslog user.notice "$PN" 'grml-small does not provide a full language setup.'
62    fi
63 fi
64
65 [ -r /etc/environment ]    && . /etc/environment
66 [ -r /etc/default/locale ] && . /etc/default/locale
67 [ -n "$LANGUAGE" ] && DEFAULT_LANGUAGE="$LANGUAGE"
68
69 if [ -z "$DEFAULT_LANGUAGE" ] ; then
70    DEFAULT_LANGUAGE=en
71 fi
72
73 if [ -z "$NONINTERACTIVE" ] ; then
74    LANGUAGE=$(LANG=C $DIALOG --stdout --title "$PN" --default-item $DEFAULT_LANGUAGE --radiolist \
75 "Which language do you want to use?
76
77 This will affect \$LANG, \$LANGUAGE and \$LC_MESSAGES.
78
79 Notice: if you want to adjust /etc/locale.gen (defines
80 which locales should be generated by locale-gen)
81 please run 'dpkg-reconfigure locales' manually.
82
83 Configuration will be written to $CONFFILE" 0 0 0 \
84  at 'austria (unicode version)' off \
85  at-iso 'austrian (iso version)' off \
86  au 'austrial (unicode version)' off \
87  au-iso 'australian (iso version)' off \
88  be 'belgian (unicode version)' off \
89  be-iso 'belgian (iso version)' off \
90  bg 'bulgarian (unicode version)' off \
91  bg-iso 'bulgarian (iso version)' off \
92  br 'brazilian (unicode version)' off \
93  br-iso 'brazilian (iso version)' off \
94  ch 'swiss (unicode version)' off \
95  ch-iso 'swiss (iso version)' off \
96  cf 'french canadian' off \
97  cn 'chinese (unicode version)' off \
98  cn-iso 'chinese (iso version)' off \
99  cs 'czech (unicode version)' off \
100  cs-iso 'czech (iso version)' off \
101  cz 'czech (unicode version)' off \
102  cz-iso 'czech (iso version)' off \
103  de 'german (unicode version)' off \
104  de-iso 'german (iso version)' off \
105  dk 'dansk (unicode version)' off \
106  dk-iso 'dansk (iso version)' off \
107  da 'dansk (unicode version)' off \
108  da-iso 'dansk (iso version)' off \
109  el 'greek (unicode version)' off \
110  el-iso 'greek (iso version)' off \
111  en 'english [us] (unicode version, grml default)' on \
112  en-iso 'english [us] (iso version)' off \
113  es 'spanish (unicode version)' off \
114  es-iso 'spanish (iso version)' off \
115  fi 'finnish (unicode version)' off \
116  fi-iso 'finnish (iso version)' off \
117  fr 'frensh (unicode version)' off \
118  fr-iso 'frensh (iso version)' off \
119  ga 'irish gaeilge (unicode version)' off \
120  ga-iso 'irish gaeilge (iso version)' off \
121  he 'hebrew (unicode version)' off \
122  he-iso 'hebrew (iso version)' off \
123  il 'hebrew (unicode version)' off \
124  il-iso 'hebrew (iso version)' off \
125  ie 'irish (unicode version)' off \
126  ie-iso 'irish (iso version)' off \
127  it 'italian (unicode version)' off \
128  it-iso 'italian (iso version)' off \
129  ja 'japanese (unicode version)' off \
130  ja-iso 'japanese (iso version)' off \
131  nl 'dutch (unicode version)' off \
132  nl-iso 'dutch (iso version)' off \
133  pl 'polish (unicode version)' off \
134  pl-iso 'polisch (iso version)' off \
135  pt 'portuguese (unicode version)' off \
136  pt-iso 'portuguese (iso version)' off \
137  ru 'russian (unicode version)' off \
138  ru-iso 'russian (iso version)' off \
139  sk 'slovak (unicode version)' off \
140  sk-iso 'slovak (iso version)' off \
141  sl 'slovenian (unicode version)' off \
142  sl-iso 'slovenian (iso version)' off \
143  tr 'turkish (unicode version)' off \
144  tr-iso 'turkish (iso version)' off \
145  tw 'chinese (traditional) (unicode version)' off \
146  tw-iso 'chinese (traditional) (iso version)' off \
147  uk 'british (unicode version)' off \
148  uk-iso 'british (iso version)' off \
149  us 'american (unicode version)' off \
150  us-iso 'american (iso version)' off \
151 )
152
153   retval=$?
154   case $retval in
155       (0)   # everything ok
156             ;;
157       (1)   echo "Cancel pressed." ; exit 1 ;;
158       (255) echo "ESC pressed."    ; exit 1 ;;
159   esac
160
161 else # non-interactive
162   LANGUAGE="$1"
163 fi
164
165 if ! grep -qe "${LANGUAGE})" -qe "${LANGUAGE}|" $LANGFUNC ; then
166    ewarn "Language ${LANGUAGE} not supported, using default." ; eend 0
167 fi
168
169 # read in the file where all the $LANGUAGE stuff is defined
170   source $LANGFUNC
171
172 # make sure the file exists
173 if ! [ -r $CONFFILE ] ; then
174 cat > $CONFFILE <<EOF
175 # File generated by $PN on $(date)
176 LANGUAGE=$LANGUAGE
177 LANG=$LANG
178 LC_MESSAGES=$LANG
179 # TZ=$TZ
180 # other environment variables you might want to set:
181 # LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY
182 # LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE
183 # LC_MEASUREMENT LC_IDENTIFICATION
184 # Notice: set LC_ALL to overwrite all LC_* variables
185 EOF
186 fi
187
188 setvalue 'LANGUAGE='    $LANGUAGE
189 setvalue 'LANG='        $LANG
190 setvalue 'LC_MESSAGES=' $LANG
191 # setvalue 'TZ='          $TZ
192
193 retval=$?
194 case $retval in
195     (0)
196           if [ -z "$NONINTERACTIVE" ] ; then
197              LANG=C $DIALOG --stdout --msgbox "Writing language settings ($LANGUAGE) to $CONFFILE was successful." 0 0
198           else
199              einfo "Writing language settings ($LANGUAGE) to $CONFFILE was successful."
200              esyslog user.notice "$PN" "Writing language settings ($LANGUAGE) to $CONFFILE was successful." ; eend 0
201           fi
202           ;;
203     *)
204           if [ -z "$NONINTERACTIVE" ] ; then
205              LANG=C $DIALOG --stdout --msgbox "Error writing settings for $LANGUAGE to $CONFFILE." 0 0
206           else
207              eerror "Error writing settings for $LANGUAGE to $CONFFILE." ; eend 1
208              esyslog user.notice "$PN" "Error writing settings for $LANGUAGE to $CONFFILE."
209           fi
210           ;;
211 esac
212
213 eoutdent
214
215 ## END OF FILE #################################################################