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