reverted rev 82
[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: Sam Dez 16 18:20:42 CET 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 'austria (unicode version)' off \
82  at-iso 'austrian (iso version)' off \
83  au 'austrial (unicode version)' off \
84  au-iso 'australian (iso version)' off \
85  be 'belgian (unicode version)' off \
86  be-iso 'belgian (iso version)' off \
87  bg 'bulgarian (unicode version)' off \
88  bg-iso 'bulgarian (iso version)' off \
89  br 'brazilian (unicode version)' off \
90  br-iso 'brazilian (iso version)' off \
91  ch 'swiss (unicode version)' off \
92  ch-iso 'swiss (iso version)' off \
93  cf 'french canadian' off \
94  cn 'chinese (unicode version)' off \
95  cn-iso 'chinese (iso version)' off \
96  cs 'czech (unicode version)' off \
97  cs-iso 'czech (iso version)' off \
98  cz 'czech (unicode version)' off \
99  cz-iso 'czech (iso version)' off \
100  de 'german (unicode version)' off \
101  de-iso 'german (iso version)' off \
102  dk 'dansk (unicode version)' off \
103  dk-iso 'dansk (iso version)' off \
104  da 'dansk (unicode version)' off \
105  da-iso 'dansk (iso version)' off \
106  el 'greek (unicode version)' off \
107  el-iso 'greek (iso version)' off \
108  en 'english [us] (unicode version, grml default)' on \
109  en-iso 'english [us] (iso version)' off \
110  es 'spanish (unicode version)' off \
111  es-iso 'spanish (iso version)' off \
112  fi 'finnish (unicode version)' off \
113  fi-iso 'finnish (iso version)' off \
114  fr 'frensh (unicode version)' off \
115  fr-iso 'frensh (iso version)' off \
116  ga 'irish gaeilge (unicode version)' off \
117  ga-iso 'irish gaeilge (iso version)' off \
118  he 'hebrew (unicode version)' off \
119  he-iso 'hebrew (iso version)' off \
120  il 'hebrew (unicode version)' off \
121  il-iso 'hebrew (iso version)' off \
122  ie 'irish (unicode version)' off \
123  ie-iso 'irish (iso version)' off \
124  it 'italian (unicode version)' off \
125  it-iso 'italian (iso version)' off \
126  ja 'japanese (unicode version)' off \
127  ja-iso 'japanese (iso version)' off \
128  nl 'dutch (unicode version)' off \
129  nl-iso 'dutch (iso version)' off \
130  pl 'polish (unicode version)' off \
131  pl-iso 'polisch (iso version)' off \
132  pt 'portuguese (unicode version)' off \
133  pt-iso 'portuguese (iso version)' off \
134  ru 'russian (unicode version)' off \
135  ru-iso 'russian (iso version)' off \
136  sk 'slovak (unicode version)' off \
137  sk-iso 'slovak (iso version)' off \
138  sl 'slovenian (unicode version)' off \
139  sl-iso 'slovenian (iso version)' off \
140  tr 'turkish (unicode version)' off \
141  tr-iso 'turkish (iso version)' off \
142  tw 'chinese (traditional) (unicode version)' off \
143  tw-iso 'chinese (traditional) (iso version)' off \
144  uk 'british (unicode version)' off \
145  uk-iso 'british (iso version)' off \
146  us 'american (unicode version)' off \
147  us-iso 'american (iso version)' off \
148 )
149
150   retval=$?
151   case $retval in
152       (0)   # everything ok
153             ;;
154       (1)   echo "Cancel pressed." ; exit 1 ;;
155       (255) echo "ESC pressed."    ; exit 1 ;;
156   esac
157
158 else # non-interactive
159   LANGUAGE="$1"
160 fi
161
162 if ! grep -q "${LANGUAGE})" $LANGFUNC ; then
163    ewarn "Language ${LANGUAGE} not supported, using default." ; eend 0
164 fi
165
166 # read in the file where all the $LANGUAGE stuff is defined
167   source $LANGFUNC
168
169 # make sure the file exists
170 if ! [ -r $CONFFILE ] ; then
171 cat > $CONFFILE <<EOF
172 # File generated by $PN on $(date)
173 LANGUAGE=$LANGUAGE
174 LANG=$LANG
175 LC_MESSAGES=$LANG
176 TZ=$TZ
177 # other environment variables you might want to set:
178 # LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY
179 # LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE
180 # LC_MEASUREMENT LC_IDENTIFICATION
181 # Notice: set LC_ALL to overwrite all LC_* variables
182 EOF
183 fi
184
185 setvalue 'LANGUAGE='    $LANGUAGE
186 setvalue 'LANG='        $LANG
187 setvalue 'LC_MESSAGES=' $LANG
188 setvalue 'TZ='          $TZ
189
190 retval=$?
191 case $retval in
192     (0)
193           if [ -z "$NONINTERACTIVE" ] ; then
194              $DIALOG --stdout --msgbox "Writing language settings ($LANGUAGE) to $CONFFILE was successful." 0 0
195           else
196              einfo "Writing language settings ($LANGUAGE) to $CONFFILE was successful."
197              esyslog user.notice "$PN" "Writing language settings ($LANGUAGE) to $CONFFILE was successful." ; eend 0
198           fi
199           ;;
200     *)
201           if [ -z "$NONINTERACTIVE" ] ; then
202              $DIALOG --stdout --msgbox "Error writing settings for $LANGUAGE to $CONFFILE." 0 0
203           else
204              eerror "Error writing settings for $LANGUAGE to $CONFFILE." ; eend 1
205              esyslog user.notice "$PN" "Error writing settings for $LANGUAGE to $CONFFILE."
206           fi
207           ;;
208 esac
209
210 eoutdent
211
212 ## END OF FILE #################################################################