]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/pc-sysinstall/backend/functions-localize.sh
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / pc-sysinstall / backend / functions-localize.sh
1 #!/bin/sh
2 #-
3 # Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26 # $FreeBSD$
27
28 # Functions which runs commands on the system
29
30 . ${BACKEND}/functions.sh
31 . ${BACKEND}/functions-parse.sh
32
33
34 # Function which localizes a FreeBSD install
35 localize_freebsd()
36 {
37   sed -i.bak "s/lang=en_US/lang=${LOCALE}/g" ${FSMNT}/etc/login.conf
38   rm ${FSMNT}/etc/login.conf.bak
39 };
40
41 localize_x_desktops() {
42
43   # Check for and customize KDE lang
44   ##########################################################################
45
46   # Check if we can localize KDE via skel
47   if [ -e "${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals" ] ; then
48     sed -i '' "s/Country=us/Country=${COUNTRY}/g" ${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals
49     sed -i '' "s/Country=us/Country=${COUNTRY}/g" ${FSMNT}/root/.kde4/share/config/kdeglobals
50     sed -i '' "s/Language=en_US/Language=${SETLANG}:${LOCALE}/g" ${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals
51   fi
52
53   # Check if we have a KDE root config
54   if [ -e "${FSMNT}/root/.kde4/share/config/kdeglobals" ] ; then
55     sed -i '' "s/Language=en_US/Language=${SETLANG}:${LOCALE}/g" ${FSMNT}/root/.kde4/share/config/kdeglobals
56   fi
57
58   # Check for KDM
59   if [ -e "${FSMNT}/usr/local/kde4/share/config/kdm/kdmrc" ] ; then
60     sed -i '' "s/Language=en_US/Language=${LOCALE}.UTF-8/g" ${FSMNT}/usr/local/kde4/share/config/kdm/kdmrc
61   fi
62
63   # Check for and customize GNOME / GDM lang
64   ##########################################################################
65
66   # See if GDM is enabled and customize its lang
67   cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q "gdm_enable=\"YES\"" 2>/dev/null
68   if [ "$?" = "0" ] ; then
69     echo "gdm_lang=\"${LOCALE}.UTF-8\"" >> ${FSMNT}/etc/rc.conf
70   fi
71
72 };
73
74 # Function which localizes a PC-BSD install
75 localize_pcbsd()
76 {
77   # Check if we have a localized splash screen and copy it
78   if [ -e "${FSMNT}/usr/local/share/pcbsd/splash-screens/loading-screen-${SETLANG}.pcx" ]
79   then
80     cp ${FSMNT}/usr/local/share/pcbsd/splash-screens/loading-screen-${SETLANG}.pcx ${FSMNT}/boot/loading-screen.pcx    
81   fi
82
83 };
84
85 localize_x_keyboard()
86 {
87   KEYMOD="$1"
88   KEYLAY="$2"
89   KEYVAR="$3"
90   COUNTRY="$4"
91   OPTION="grp:alt_shift_toggle"
92   SETXKBMAP=""
93
94   if [ "${COUNTRY}" = "NONE" -o "${COUNTRY}" = "us" -o "${COUNTRY}" = "C" ] ; then
95     #In this case we don't need any additional language
96     COUNTRY=""
97     OPTION=""
98   else
99     COUNTRY=",${COUNTRY}"
100   fi
101
102   if [ "${KEYMOD}" != "NONE" ]
103   then
104     SETXKBMAP="-model ${KEYMOD}"
105     KXMODEL="${KEYMOD}"
106   else
107     KXMODEL="pc104"
108   fi
109
110   if [ "${KEYLAY}" != "NONE" ]
111   then
112     localize_key_layout "$KEYLAY"
113     SETXKBMAP="${SETXKBMAP} -layout ${KEYLAY}"
114     KXLAYOUT="${KEYLAY}"
115   else
116     KXLAYOUT="us"
117   fi
118
119   if [ "${KEYVAR}" != "NONE" ]
120   then
121     SETXKBMAP="${SETXKBMAP} -variant ${KEYVAR}"
122     KXVAR="(${KEYVAR})"
123   else
124     KXVAR=""
125   fi
126
127   # Setup .xprofile with our setxkbmap call now
128   if [ ! -z "${SETXKBMAP}" ]
129   then
130     if [ ! -e "${FSMNT}/usr/share/skel/.xprofile" ]
131     then
132       echo "#!/bin/sh" >${FSMNT}/usr/share/skel/.xprofile
133     fi
134
135     # Save the keyboard layout for user / root X logins
136     echo "setxkbmap ${SETXKBMAP}" >>${FSMNT}/usr/share/skel/.xprofile
137     chmod 755 ${FSMNT}/usr/share/skel/.xprofile
138     cp ${FSMNT}/usr/share/skel/.xprofile ${FSMNT}/root/.xprofile
139
140     # Save it for KDM
141     if [ -e "${FSMNT}/usr/local/kde4/share/config/kdm/Xsetup" ] ; then
142       echo "setxkbmap ${SETXKBMAP}" >>${FSMNT}/usr/local/kde4/share/config/kdm/Xsetup
143     fi
144   fi
145  
146   # Create the kxkbrc configuration using these options
147   if [ -d "${FSMNT}/usr/share/skel/.kde4/share/config" ] ; then
148     echo "[Layout]
149 DisplayNames=${KXLAYOUT}${COUNTRY}
150 IndicatorOnly=false
151 LayoutList=${KXLAYOUT}${KXVAR}${COUNTRY}
152 Model=${KXMODEL}
153 Options=${OPTION}
154 ResetOldOptions=true
155 ShowFlag=true
156 ShowSingle=false
157 SwitchMode=WinClass
158 Use=true " >${FSMNT}/usr/share/skel/.kde4/share/config/kxkbrc
159   fi
160
161 };
162
163 localize_key_layout()
164 {
165
166   KEYLAYOUT="$1"
167
168   # Set the keylayout in rc.conf
169   case ${KEYLAYOUT} in
170     am) KEYLAYOUT_CONSOLE="hy.armscii-8" ;;
171     ca) KEYLAYOUT_CONSOLE="fr_CA.acc.iso" ;;
172     ch) KEYLAYOUT_CONSOLE="swissgerman.iso" ;;
173     cz) KEYLAYOUT_CONSOLE="cz.iso2" ;;
174     de) KEYLAYOUT_CONSOLE="german.iso" ;;
175     dk) KEYLAYOUT_CONSOLE="danish.iso" ;;
176     ee) KEYLAYOUT_CONSOLE="estonian.iso" ;;
177     es) KEYLAYOUT_CONSOLE="spanish.iso" ;;
178     fi) KEYLAYOUT_CONSOLE="finnish.iso" ;;
179     is) KEYLAYOUT_CONSOLE="icelandic.iso" ;;
180     jp) KEYLAYOUT_CONSOLE="jp.106" ;;
181     nl) KEYLAYOUT_CONSOLE="dutch.iso.acc" ;;
182     no) KEYLAYOUT_CONSOLE="norwegian.iso" ;;
183     pl) KEYLAYOUT_CONSOLE="pl_PL.ISO8859-2" ;;
184     ru) KEYLAYOUT_CONSOLE="ru.koi8-r" ;;
185     sk) KEYLAYOUT_CONSOLE="sk.iso2" ;;
186     se) KEYLAYOUT_CONSOLE="swedish.iso" ;;
187     tr) KEYLAYOUT_CONSOLE="tr.iso9.q" ;;
188     gb) KEYLAYOUT_CONSOLE="uk.iso" ;;
189      *)  if [ ! -z "${KEYLAYOUT}" ]
190          then
191            KEYLAYOUT_CONSOLE="${KEYLAYOUT}.iso"
192          fi
193         ;;
194   esac
195
196   if [ -n "${KEYLAYOUT_CONSOLE}" ]
197   then
198     echo "keymap=\"${KEYLAYOUT_CONSOLE}\"" >>${FSMNT}/etc/rc.conf
199   fi
200
201 };
202
203 #  Function which prunes other l10n files from the KDE install
204 localize_prune_langs()
205 {
206   get_value_from_cfg localizeLang
207   KEEPLANG="$VAL"
208   if [ -z "$KEEPLANG" ] ; then
209     KEEPLANG="en"
210   fi
211   export KEEPLANG 
212
213   echo_log "Pruning other l10n files, keeping ${KEEPLANG}"
214
215   # Create the script to do uninstalls
216   echo '#!/bin/sh
217
218   for i in `pkg_info -xEI kde-l10n`
219   do
220     echo "$i" | grep "${KEEPLANG}-kde"
221     if [ $? -ne 0 ] ; then
222       pkg_delete ${i}
223     fi
224   done
225   ' > ${FSMNT}/.pruneLangs.sh
226
227   chmod 755 ${FSMNT}/.pruneLangs.sh
228   chroot ${FSMNT} /.pruneLangs.sh >/dev/null 2>/dev/null
229   rm ${FSMNT}/.pruneLangs.sh
230
231 };
232
233 # Function which sets COUNTRY SETLANG and LOCALE based upon $1
234 localize_get_codes()
235
236   TARGETLANG="${1}"
237   # Setup the presets for the specific lang
238   case $TARGETLANG in
239     af)
240       COUNTRY="C"
241       SETLANG="af"
242       LOCALE="af_ZA"
243       ;;
244     ar)
245           COUNTRY="C"
246       SETLANG="ar"
247       LOCALE="en_US"
248       ;;
249     az)
250           COUNTRY="C"
251       SETLANG="az"
252       LOCALE="en_US"
253       ;;
254     ca)
255           COUNTRY="es"
256       SETLANG="es:ca"
257       LOCALE="ca_ES"
258       ;;
259     be)
260           COUNTRY="be"
261       SETLANG="be"
262       LOCALE="be_BY"
263       ;;
264     bn)
265           COUNTRY="bn"
266       SETLANG="bn"
267       LOCALE="en_US"
268       ;;
269     bg)
270           COUNTRY="bg"
271       SETLANG="bg"
272       LOCALE="bg_BG"
273       ;;
274     cs)
275           COUNTRY="cz"
276       SETLANG="cs"
277       LOCALE="cs_CZ"
278       ;;
279     da)
280           COUNTRY="dk"
281       SETLANG="da"
282       LOCALE="da_DK"
283       ;;
284     de)
285           COUNTRY="de"
286       SETLANG="de"
287       LOCALE="de_DE"
288       ;;
289     en_GB)
290           COUNTRY="gb"
291       SETLANG="en_GB:cy"
292       LOCALE="en_GB"
293       ;;
294     el)
295           COUNTRY="gr"
296       SETLANG="el:gr"
297       LOCALE="el_GR"
298       ;;
299     es)
300           COUNTRY="es"
301       SETLANG="es"
302       LOCALE="es_ES"
303       ;;
304     es_LA)
305           COUNTRY="us"
306       SETLANG="es:en_US"
307       LOCALE="es_ES"
308       ;;
309     et)
310           COUNTRY="ee"
311       SETLANG="et"
312       LOCALE="et_EE"
313       ;;
314     fr)
315           COUNTRY="fr"
316       SETLANG="fr"
317       LOCALE="fr_FR"
318       ;;
319     he)
320           COUNTRY="il"
321       SETLANG="he:ar"
322       LOCALE="he_IL"
323       ;;
324     hr)
325           COUNTRY="hr"
326       SETLANG="hr"
327       LOCALE="hr_HR"
328       ;;
329     hu)
330           COUNTRY="hu"
331       SETLANG="hu"
332       LOCALE="hu_HU"
333       ;;
334     it)
335           COUNTRY="it"
336       SETLANG="it"
337       LOCALE="it_IT"
338       ;;
339     ja)
340           COUNTRY="jp"
341       SETLANG="ja"
342       LOCALE="ja_JP"
343       ;;
344     ko)
345           COUNTRY="kr"
346       SETLANG="ko"
347       LOCALE="ko_KR"
348       ;;
349     nl)
350           COUNTRY="nl"
351       SETLANG="nl"
352       LOCALE="nl_NL"
353       ;;
354     nn)
355           COUNTRY="no"
356       SETLANG="nn"
357       LOCALE="en_US"
358       ;;
359     pa)
360           COUNTRY="pa"
361       SETLANG="pa"
362       LOCALE="en_US"
363       ;;
364     pl)
365           COUNTRY="pl"
366       SETLANG="pl"
367       LOCALE="pl_PL"
368       ;;
369     pt)
370           COUNTRY="pt"
371       SETLANG="pt"
372       LOCALE="pt_PT"
373       ;;
374     pt_BR)
375           COUNTRY="br"
376       SETLANG="pt_BR"
377       LOCALE="pt_BR"
378       ;;
379     ru)
380           COUNTRY="ru"
381       SETLANG="ru"
382       LOCALE="ru_RU"
383       ;;
384     sl)
385           COUNTRY="si"
386       SETLANG="sl"
387       LOCALE="sl_SI"
388       ;;
389     sk)
390           COUNTRY="sk"
391       SETLANG="sk"
392       LOCALE="sk_SK"
393       ;;
394     sv)
395           COUNTRY="se"
396       SETLANG="sv"
397       LOCALE="sv_SE"
398       ;;
399     uk)
400           COUNTRY="ua"
401       SETLANG="uk"
402       LOCALE="uk_UA"
403       ;;
404     vi)
405           COUNTRY="vn"
406       SETLANG="vi"
407       LOCALE="en_US"
408       ;;
409     zh_CN)
410           COUNTRY="cn"
411       SETLANG="zh_CN"
412       LOCALE="zh_CN"
413       ;;
414     zh_TW)
415           COUNTRY="tw"
416       SETLANG="zh_TW"
417       LOCALE="zh_TW"
418       ;;
419     *)
420           COUNTRY="C"
421       SETLANG="${TARGETLANG}"
422       LOCALE="en_US"
423       ;;
424   esac
425
426   export COUNTRY SETLANG LOCALE
427
428 };
429
430 # Function which sets the timezone on the system
431 set_timezone()
432 {
433   TZONE="$1"
434   cp ${FSMNT}/usr/share/zoneinfo/${TZONE} ${FSMNT}/etc/localtime
435 };
436
437 # Function which enables / disables NTP
438 set_ntp()
439 {
440   ENABLED="$1"
441   if [ "$ENABLED" = "yes" -o "${ENABLED}" = "YES" ]
442   then
443     cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'ntpd_enable="YES"' 2>/dev/null
444     if [ $? -ne 0 ]
445     then
446       echo 'ntpd_enable="YES"' >>${FSMNT}/etc/rc.conf
447       echo 'ntpd_sync_on_start="YES"' >>${FSMNT}/etc/rc.conf
448     fi
449   else
450     cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'ntpd_enable="YES"' 2>/dev/null
451     if [ $? -ne 0 ]
452     then
453       sed -i.bak 's|ntpd_enable="YES"||g' ${FSMNT}/etc/rc.conf
454     fi
455   fi
456 };
457
458 # Starts checking for localization directives
459 run_localize()
460 {
461   KEYLAYOUT="NONE"
462   KEYMOD="NONE"
463   KEYVAR="NONE"
464
465   while read line
466   do
467     # Check if we need to do any localization
468     echo $line | grep -q "^localizeLang=" 2>/dev/null
469     if [ $? -eq 0 ]
470     then
471
472       # Set our country / lang / locale variables
473       get_value_from_string "$line"
474       localize_get_codes ${VAL}
475
476       get_value_from_string "$line"
477       # If we are doing PC-BSD install, localize it as well as FreeBSD base
478       if [ "${INSTALLTYPE}" != "FreeBSD" ]
479       then
480         localize_pcbsd "$VAL"
481       fi
482
483       # Localize FreeBSD
484       localize_freebsd "$VAL"
485
486       # Localize any X pkgs
487       localize_x_desktops "$VAL"
488     fi
489
490     # Check if we need to do any keylayouts
491     echo $line | grep -q "^localizeKeyLayout=" 2>/dev/null
492     if [ $? -eq 0 ] ; then
493       get_value_from_string "$line"
494       KEYLAYOUT="$VAL"
495     fi
496
497     # Check if we need to do any key models
498     echo $line | grep -q "^localizeKeyModel=" 2>/dev/null
499     if [ $? -eq 0 ] ; then
500       get_value_from_string "$line"
501       KEYMOD="$VAL"
502     fi
503
504     # Check if we need to do any key variant
505     echo $line | grep -q "^localizeKeyVariant=" 2>/dev/null
506     if [ $? -eq 0 ] ; then
507       get_value_from_string "$line"
508       KEYVAR="$VAL"
509     fi
510
511
512     # Check if we need to set a timezone
513     echo $line | grep -q "^timeZone=" 2>/dev/null
514     if [ $? -eq 0 ] ; then
515       get_value_from_string "$line"
516       set_timezone "$VAL"
517     fi
518
519     # Check if we need to set a timezone
520     echo $line | grep -q "^enableNTP=" 2>/dev/null
521     if [ $? -eq 0 ] ; then
522       get_value_from_string "$line"
523       set_ntp "$VAL"
524     fi
525   done <${CFGF}
526
527   if [ "${INSTALLTYPE}" != "FreeBSD" ] ; then
528     # Do our X keyboard localization
529     localize_x_keyboard "${KEYMOD}" "${KEYLAYOUT}" "${KEYVAR}" "${COUNTRY}"
530   fi
531
532   # Check if we want to prunt any other KDE lang files to save some disk space
533   get_value_from_cfg localizePrune
534   if [ "${VAL}" = "yes" -o "${VAL}" = "YES" ] ; then
535     localize_prune_langs
536   fi
537
538   # Update the login.conf db, even if we didn't localize, its a good idea to make sure its up2date
539   run_chroot_cmd "/usr/bin/cap_mkdb /etc/login.conf" >/dev/null 2>/dev/null
540
541 };