]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.sbin/pc-sysinstall/backend/functions-localize.sh
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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     ch) KEYLAYOUT_CONSOLE="swissgerman.iso" ;;
172     cz) KEYLAYOUT_CONSOLE="cz.iso2" ;;
173     de) KEYLAYOUT_CONSOLE="german.iso" ;;
174     dk) KEYLAYOUT_CONSOLE="danish.iso" ;;
175     ee) KEYLAYOUT_CONSOLE="estonian.iso" ;;
176     es) KEYLAYOUT_CONSOLE="spanish.iso" ;;
177     fi) KEYLAYOUT_CONSOLE="finnish.iso" ;;
178     is) KEYLAYOUT_CONSOLE="icelandic.iso" ;;
179     jp) KEYLAYOUT_CONSOLE="jp.106" ;;
180     nl) KEYLAYOUT_CONSOLE="dutch.iso.acc" ;;
181     no) KEYLAYOUT_CONSOLE="norwegian.iso" ;;
182     pl) KEYLAYOUT_CONSOLE="pl_PL.ISO8859-2" ;;
183     ru) KEYLAYOUT_CONSOLE="ru.koi8-r" ;;
184     sk) KEYLAYOUT_CONSOLE="sk.iso2" ;;
185     se) KEYLAYOUT_CONSOLE="swedish.iso" ;;
186     tr) KEYLAYOUT_CONSOLE="tr.iso9.q" ;;
187     gb) KEYLAYOUT_CONSOLE="uk.iso" ;;
188      *)  if [ ! -z "${KEYLAYOUT}" ]
189          then
190            KEYLAYOUT_CONSOLE="${KEYLAYOUT}.iso"
191          fi
192         ;;
193   esac
194
195   if [ -n "${KEYLAYOUT_CONSOLE}" ]
196   then
197     echo "keymap=\"${KEYLAYOUT_CONSOLE}\"" >>${FSMNT}/etc/rc.conf
198   fi
199
200 };
201
202 #  Function which prunes other l10n files from the KDE install
203 localize_prune_langs()
204 {
205   get_value_from_cfg localizeLang
206   KEEPLANG="$VAL"
207   if [ -z "$KEEPLANG" ] ; then
208     KEEPLANG="en"
209   fi
210   export KEEPLANG 
211
212   echo_log "Pruning other l10n files, keeping ${KEEPLANG}"
213
214   # Create the script to do uninstalls
215   echo '#!/bin/sh
216
217   for i in `pkg_info -xEI kde-l10n`
218   do
219     echo "$i" | grep "${KEEPLANG}-kde"
220     if [ $? -ne 0 ] ; then
221       pkg_delete ${i}
222     fi
223   done
224   ' > ${FSMNT}/.pruneLangs.sh
225
226   chmod 755 ${FSMNT}/.pruneLangs.sh
227   chroot ${FSMNT} /.pruneLangs.sh >/dev/null 2>/dev/null
228   rm ${FSMNT}/.pruneLangs.sh
229
230 };
231
232 # Function which sets COUNTRY SETLANG and LOCALE based upon $1
233 localize_get_codes()
234
235   TARGETLANG="${1}"
236   # Setup the presets for the specific lang
237   case $TARGETLANG in
238     af)
239       COUNTRY="C"
240       SETLANG="af"
241       LOCALE="af_ZA"
242       ;;
243     ar)
244           COUNTRY="C"
245       SETLANG="ar"
246       LOCALE="en_US"
247       ;;
248     az)
249           COUNTRY="C"
250       SETLANG="az"
251       LOCALE="en_US"
252       ;;
253     ca)
254           COUNTRY="es"
255       SETLANG="es:ca"
256       LOCALE="ca_ES"
257       ;;
258     be)
259           COUNTRY="be"
260       SETLANG="be"
261       LOCALE="be_BY"
262       ;;
263     bn)
264           COUNTRY="bn"
265       SETLANG="bn"
266       LOCALE="en_US"
267       ;;
268     bg)
269           COUNTRY="bg"
270       SETLANG="bg"
271       LOCALE="bg_BG"
272       ;;
273     cs)
274           COUNTRY="cz"
275       SETLANG="cs"
276       LOCALE="cs_CZ"
277       ;;
278     da)
279           COUNTRY="dk"
280       SETLANG="da"
281       LOCALE="da_DK"
282       ;;
283     de)
284           COUNTRY="de"
285       SETLANG="de"
286       LOCALE="de_DE"
287       ;;
288     en_GB)
289           COUNTRY="gb"
290       SETLANG="en_GB:cy"
291       LOCALE="en_GB"
292       ;;
293     el)
294           COUNTRY="gr"
295       SETLANG="el:gr"
296       LOCALE="el_GR"
297       ;;
298     es)
299           COUNTRY="es"
300       SETLANG="es"
301       LOCALE="es_ES"
302       ;;
303     es_LA)
304           COUNTRY="us"
305       SETLANG="es:en_US"
306       LOCALE="es_ES"
307       ;;
308     et)
309           COUNTRY="ee"
310       SETLANG="et"
311       LOCALE="et_EE"
312       ;;
313     fr)
314           COUNTRY="fr"
315       SETLANG="fr"
316       LOCALE="fr_FR"
317       ;;
318     he)
319           COUNTRY="il"
320       SETLANG="he:ar"
321       LOCALE="he_IL"
322       ;;
323     hr)
324           COUNTRY="hr"
325       SETLANG="hr"
326       LOCALE="hr_HR"
327       ;;
328     hu)
329           COUNTRY="hu"
330       SETLANG="hu"
331       LOCALE="hu_HU"
332       ;;
333     it)
334           COUNTRY="it"
335       SETLANG="it"
336       LOCALE="it_IT"
337       ;;
338     ja)
339           COUNTRY="jp"
340       SETLANG="ja"
341       LOCALE="ja_JP"
342       ;;
343     ko)
344           COUNTRY="kr"
345       SETLANG="ko"
346       LOCALE="ko_KR"
347       ;;
348     nl)
349           COUNTRY="nl"
350       SETLANG="nl"
351       LOCALE="nl_NL"
352       ;;
353     nn)
354           COUNTRY="no"
355       SETLANG="nn"
356       LOCALE="en_US"
357       ;;
358     pa)
359           COUNTRY="pa"
360       SETLANG="pa"
361       LOCALE="en_US"
362       ;;
363     pl)
364           COUNTRY="pl"
365       SETLANG="pl"
366       LOCALE="pl_PL"
367       ;;
368     pt)
369           COUNTRY="pt"
370       SETLANG="pt"
371       LOCALE="pt_PT"
372       ;;
373     pt_BR)
374           COUNTRY="br"
375       SETLANG="pt_BR"
376       LOCALE="pt_BR"
377       ;;
378     ru)
379           COUNTRY="ru"
380       SETLANG="ru"
381       LOCALE="ru_RU"
382       ;;
383     sl)
384           COUNTRY="si"
385       SETLANG="sl"
386       LOCALE="sl_SI"
387       ;;
388     sk)
389           COUNTRY="sk"
390       SETLANG="sk"
391       LOCALE="sk_SK"
392       ;;
393     sv)
394           COUNTRY="se"
395       SETLANG="sv"
396       LOCALE="sv_SE"
397       ;;
398     uk)
399           COUNTRY="ua"
400       SETLANG="uk"
401       LOCALE="uk_UA"
402       ;;
403     vi)
404           COUNTRY="vn"
405       SETLANG="vi"
406       LOCALE="en_US"
407       ;;
408     zh_CN)
409           COUNTRY="cn"
410       SETLANG="zh_CN"
411       LOCALE="zh_CN"
412       ;;
413     zh_TW)
414           COUNTRY="tw"
415       SETLANG="zh_TW"
416       LOCALE="zh_TW"
417       ;;
418     *)
419           COUNTRY="C"
420       SETLANG="${TARGETLANG}"
421       LOCALE="en_US"
422       ;;
423   esac
424
425   export COUNTRY SETLANG LOCALE
426
427 };
428
429 # Function which sets the timezone on the system
430 set_timezone()
431 {
432   TZONE="$1"
433   cp ${FSMNT}/usr/share/zoneinfo/${TZONE} ${FSMNT}/etc/localtime
434 };
435
436 # Function which enables / disables NTP
437 set_ntp()
438 {
439   ENABLED="$1"
440   if [ "$ENABLED" = "yes" -o "${ENABLED}" = "YES" ]
441   then
442     cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'ntpd_enable="YES"' 2>/dev/null
443     if [ $? -ne 0 ]
444     then
445       echo 'ntpd_enable="YES"' >>${FSMNT}/etc/rc.conf
446       echo 'ntpd_sync_on_start="YES"' >>${FSMNT}/etc/rc.conf
447     fi
448   else
449     cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'ntpd_enable="YES"' 2>/dev/null
450     if [ $? -ne 0 ]
451     then
452       sed -i.bak 's|ntpd_enable="YES"||g' ${FSMNT}/etc/rc.conf
453     fi
454   fi
455 };
456
457 # Starts checking for localization directives
458 run_localize()
459 {
460   KEYLAYOUT="NONE"
461   KEYMOD="NONE"
462   KEYVAR="NONE"
463
464   while read line
465   do
466     # Check if we need to do any localization
467     echo $line | grep -q "^localizeLang=" 2>/dev/null
468     if [ $? -eq 0 ]
469     then
470
471       # Set our country / lang / locale variables
472       get_value_from_string "$line"
473       localize_get_codes ${VAL}
474
475       get_value_from_string "$line"
476       # If we are doing PC-BSD install, localize it as well as FreeBSD base
477       if [ "${INSTALLTYPE}" != "FreeBSD" ]
478       then
479         localize_pcbsd "$VAL"
480       fi
481
482       # Localize FreeBSD
483       localize_freebsd "$VAL"
484
485       # Localize any X pkgs
486       localize_x_desktops "$VAL"
487     fi
488
489     # Check if we need to do any keylayouts
490     echo $line | grep -q "^localizeKeyLayout=" 2>/dev/null
491     if [ $? -eq 0 ] ; then
492       get_value_from_string "$line"
493       KEYLAYOUT="$VAL"
494     fi
495
496     # Check if we need to do any key models
497     echo $line | grep -q "^localizeKeyModel=" 2>/dev/null
498     if [ $? -eq 0 ] ; then
499       get_value_from_string "$line"
500       KEYMOD="$VAL"
501     fi
502
503     # Check if we need to do any key variant
504     echo $line | grep -q "^localizeKeyVariant=" 2>/dev/null
505     if [ $? -eq 0 ] ; then
506       get_value_from_string "$line"
507       KEYVAR="$VAL"
508     fi
509
510
511     # Check if we need to set a timezone
512     echo $line | grep -q "^timeZone=" 2>/dev/null
513     if [ $? -eq 0 ] ; then
514       get_value_from_string "$line"
515       set_timezone "$VAL"
516     fi
517
518     # Check if we need to set a timezone
519     echo $line | grep -q "^enableNTP=" 2>/dev/null
520     if [ $? -eq 0 ] ; then
521       get_value_from_string "$line"
522       set_ntp "$VAL"
523     fi
524   done <${CFGF}
525
526   if [ "${INSTALLTYPE}" != "FreeBSD" ] ; then
527     # Do our X keyboard localization
528     localize_x_keyboard "${KEYMOD}" "${KEYLAYOUT}" "${KEYVAR}" "${COUNTRY}"
529   fi
530
531   # Check if we want to prunt any other KDE lang files to save some disk space
532   get_value_from_cfg localizePrune
533   if [ "${VAL}" = "yes" -o "${VAL}" = "YES" ] ; then
534     localize_prune_langs
535   fi
536
537   # Update the login.conf db, even if we didn't localize, its a good idea to make sure its up2date
538   run_chroot_cmd "/usr/bin/cap_mkdb /etc/login.conf" >/dev/null 2>/dev/null
539
540 };