]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pc-sysinstall/backend/functions-localize.sh
Bring in Kris Moore's pc-sysinstall shell script from PC-BSD. This
[FreeBSD/FreeBSD.git] / usr.sbin / pc-sysinstall / backend / functions-localize.sh
1 #!/bin/sh
2 #-
3 # Copyright (c) 2010 iX Systems, 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
42 # Function which localizes a PC-BSD install
43 localize_pcbsd()
44 {
45   #Change the skel files
46   ##########################################################################
47   sed -i.bak "s/Country=us/Country=${COUNTRY}/g" ${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals
48   sed -i.bak "s/Country=us/Country=${COUNTRY}/g" ${FSMNT}/root/.kde4/share/config/kdeglobals
49   sed -i.bak "s/Language=en_US/Language=${SETLANG}:${LOCALE}/g" ${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals
50   sed -i.bak "s/Language=en_US/Language=${SETLANG}:${LOCALE}/g" ${FSMNT}/root/.kde4/share/config/kdeglobals
51
52   #Change KDM Langs
53   ##########################################################################
54   sed -i.bak "s/Language=en_US/Language=${LOCALE}.UTF-8/g" ${FSMNT}/usr/local/kde4/share/config/kdm/kdmrc
55
56   # Check if we have a localized splash screen and copy it
57   if [ -e "${FSMNT}/usr/PCBSD/splash-screens/loading-screen-${SETLANG}.pcx" ]
58   then
59     cp ${FSMNT}/usr/PCBSD/splash-screens/loading-screen-${SETLANG}.pcx ${FSMNT}/boot/loading-screen.pcx    
60   fi
61
62 };
63
64 localize_x_keyboard()
65 {
66    KEYMOD="$1"
67    KEYLAY="$2"
68    KEYVAR="$3"
69    COUNTRY="$4"
70    OPTION="grp:alt_shift_toggle"
71    SETXKBMAP=""
72
73    if [ "${COUNTRY}" = "NONE" -o "${COUNTRY}" = "us" -o "${COUNTRY}" = "C" ] ; then
74      #In this case we don't need any additional language
75      COUNTRY=""
76      OPTION=""
77    else
78      COUNTRY=",${COUNTRY}"
79    fi
80
81    if [ "${KEYMOD}" != "NONE" ]
82    then
83      SETXKBMAP="-model ${KEYMOD}"
84      KXMODEL="${KEYMOD}"
85    else
86      KXMODEL="pc104"
87    fi
88
89    if [ "${KEYLAY}" != "NONE" ]
90    then
91      localize_key_layout "$KEYLAY"
92      SETXKBMAP="${SETXKBMAP} -layout ${KEYLAY}"
93      KXLAYOUT="${KEYLAY}"
94    else
95      KXLAYOUT="us"
96    fi
97
98    if [ "${KEYVAR}" != "NONE" ]
99    then
100      SETXKBMAP="${SETXKBMAP} -variant ${KEYVAR}"
101      KXVAR="(${KEYVAR})"
102    else
103      KXVAR=""
104    fi
105
106    # Setup .xprofile with our setxkbmap call now
107    if [ ! -z "${SETXKBMAP}" ]
108    then
109      if [ ! -e "${FSMNT}/usr/share/skel/.xprofile" ]
110      then
111        echo "#!/bin/sh" >${FSMNT}/usr/share/skel/.xprofile
112      fi
113
114      # Save the keyboard layout for user / root X logins
115      echo "setxkbmap ${SETXKBMAP}" >>${FSMNT}/usr/share/skel/.xprofile
116      chmod 755 ${FSMNT}/usr/share/skel/.xprofile
117      cp ${FSMNT}/usr/share/skel/.xprofile ${FSMNT}/root/.xprofile
118
119      # Save it for KDM
120      echo "setxkbmap ${SETXKBMAP}" >>${FSMNT}/usr/local/kde4/share/config/kdm/Xsetup
121    fi
122  
123
124    # Create the kxkbrc configuration using these options
125    echo "[Layout]
126 DisplayNames=${KXLAYOUT}${COUNTRY}
127 IndicatorOnly=false
128 LayoutList=${KXLAYOUT}${KXVAR}${COUNTRY}
129 Model=${KXMODEL}
130 Options=${OPTION}
131 ResetOldOptions=true
132 ShowFlag=true
133 ShowSingle=false
134 SwitchMode=WinClass
135 Use=true " >${FSMNT}/usr/share/skel/.kde4/share/config/kxkbrc
136
137 };
138
139 localize_key_layout()
140 {
141
142   KEYLAYOUT="$1"
143
144   # Set the keylayout in rc.conf
145   case ${KEYLAYOUT} in
146     am) KEYLAYOUT_CONSOLE="hy.armscii-8" ;;
147     ch) KEYLAYOUT_CONSOLE="swissgerman.iso" ;;
148     cz) KEYLAYOUT_CONSOLE="cz.iso2" ;;
149     de) KEYLAYOUT_CONSOLE="german.iso" ;;
150     dk) KEYLAYOUT_CONSOLE="danish.iso" ;;
151     ee) KEYLAYOUT_CONSOLE="estonian.iso" ;;
152     es) KEYLAYOUT_CONSOLE="spanish.iso" ;;
153     fi) KEYLAYOUT_CONSOLE="finnish.iso" ;;
154     is) KEYLAYOUT_CONSOLE="icelandic.iso" ;;
155     jp) KEYLAYOUT_CONSOLE="jp.106" ;;
156     nl) KEYLAYOUT_CONSOLE="dutch.iso.acc" ;;
157     no) KEYLAYOUT_CONSOLE="norwegian.iso" ;;
158     pl) KEYLAYOUT_CONSOLE="pl_PL.ISO8859-2" ;;
159     ru) KEYLAYOUT_CONSOLE="ru.koi8-r" ;;
160     sk) KEYLAYOUT_CONSOLE="sk.iso2" ;;
161     se) KEYLAYOUT_CONSOLE="swedish.iso" ;;
162     tr) KEYLAYOUT_CONSOLE="tr.iso9.q" ;;
163     gb) KEYLAYOUT_CONSOLE="uk.iso" ;;
164      *)  if [ ! -z "${KEYLAYOUT}" ]
165          then
166            KEYLAYOUT_CONSOLE="${KEYLAYOUT}.iso"
167          fi
168         ;;
169   esac
170
171   if [ ! -z "${KEYLAYOUT_CONSOLE}" ]
172   then
173     echo "keymap=\"${KEYLAYOUT_CONSOLE}\"" >>${FSMNT}/etc/rc.conf
174   fi
175
176 };
177
178 #  Function which prunes other l10n files from the KDE install
179 localize_prune_langs()
180 {
181   get_value_from_cfg localizeLang
182   KEEPLANG="$VAL"
183   if [ -z "$KEEPLANG" ] ; then
184     KEEPLANG="en"
185   fi
186   export KEEPLANG 
187
188   echo_log "Pruning other l10n files, keeping ${KEEPLANG}"
189
190   # Create the script to do uninstalls
191   echo '#!/bin/sh
192
193   for i in `pkg_info | grep "kde-l10n" | cut -d " " -f 1`
194   do
195     echo "$i" | grep "${KEEPLANG}-kde"
196     if [ "$?" != "0" ] ; then
197       pkg_delete ${i}
198     fi
199   done
200   ' > ${FSMNT}/.pruneLangs.sh
201
202   chmod 755 ${FSMNT}/.pruneLangs.sh
203   chroot ${FSMNT} /.pruneLangs.sh >/dev/null 2>/dev/null
204   rm ${FSMNT}/.pruneLangs.sh
205
206 };
207
208 # Function which sets COUNTRY SETLANG and LOCALE based upon $1
209 localize_get_codes()
210
211   TARGETLANG="${1}"
212   # Setup the presets for the specific lang
213   case $TARGETLANG in
214    af) COUNTRY="C"
215        SETLANG="af"
216        LOCALE="af_ZA"
217        ;;
218    ar) COUNTRY="C"
219        SETLANG="ar"
220        LOCALE="en_US"
221        ;;
222    az) COUNTRY="C"
223        SETLANG="az"
224        LOCALE="en_US"
225        ;;
226    ca) COUNTRY="es"
227        SETLANG="es:ca"
228        LOCALE="ca_ES"
229        ;;
230    be) COUNTRY="be"
231        SETLANG="be"
232        LOCALE="be_BY"
233        ;;
234    bn) COUNTRY="bn"
235        SETLANG="bn"
236        LOCALE="en_US"
237        ;;
238    bg) COUNTRY="bg"
239        SETLANG="bg"
240        LOCALE="bg_BG"
241        ;;
242    cs) COUNTRY="cz"
243        SETLANG="cs"
244        LOCALE="cs_CZ"
245        ;;
246    da) COUNTRY="dk"
247        SETLANG="da"
248        LOCALE="da_DK"
249        ;;
250    de) COUNTRY="de"
251        SETLANG="de"
252        LOCALE="de_DE"
253        ;;
254    en_GB) COUNTRY="gb"
255        SETLANG="en_GB:cy"
256        LOCALE="en_GB"
257        ;;
258    el) COUNTRY="gr"
259        SETLANG="el:gr"
260        LOCALE="el_GR"
261        ;;
262    es) COUNTRY="es"
263        SETLANG="es"
264        LOCALE="es_ES"
265        ;;
266    es_LA) COUNTRY="us"
267           SETLANG="es:en_US"
268           LOCALE="es_ES"
269        ;;
270    et) COUNTRY="ee"
271        SETLANG="et"
272        LOCALE="et_EE"
273        ;;
274    fr) COUNTRY="fr"
275        SETLANG="fr"
276        LOCALE="fr_FR"
277        ;;
278    he) COUNTRY="il"
279        SETLANG="he:ar"
280        LOCALE="he_IL"
281        ;;
282    hr) COUNTRY="hr"
283        SETLANG="hr"
284        LOCALE="hr_HR"
285        ;;
286    hu) COUNTRY="hu"
287        SETLANG="hu"
288        LOCALE="hu_HU"
289        ;;
290    it) COUNTRY="it"
291        SETLANG="it"
292        LOCALE="it_IT"
293        ;;
294    ja) COUNTRY="jp"
295        SETLANG="ja"
296        LOCALE="ja_JP"
297        ;;
298    ko) COUNTRY="kr"
299        SETLANG="ko"
300        LOCALE="ko_KR"
301        ;;
302    nl) COUNTRY="nl"
303        SETLANG="nl"
304        LOCALE="nl_NL"
305        ;;
306    nn) COUNTRY="no"
307        SETLANG="nn"
308        LOCALE="en_US"
309        ;;
310    pa) COUNTRY="pa"
311        SETLANG="pa"
312        LOCALE="en_US"
313        ;;
314    pl) COUNTRY="pl"
315        SETLANG="pl"
316        LOCALE="pl_PL"
317        ;;
318    pt) COUNTRY="pt"
319        SETLANG="pt"
320        LOCALE="pt_PT"
321        ;;
322    pt_BR) COUNTRY="br"
323           SETLANG="pt_BR"
324           LOCALE="pt_BR"
325        ;;
326    ru) COUNTRY="ru"
327        SETLANG="ru"
328        LOCALE="ru_RU"
329        ;;
330    sl) COUNTRY="si"
331        SETLANG="sl"
332        LOCALE="sl_SI"
333        ;;
334    sk) COUNTRY="sk"
335        SETLANG="sk"
336        LOCALE="sk_SK"
337        ;;
338    sv) COUNTRY="se"
339        SETLANG="sv"
340        LOCALE="sv_SE"
341        ;;
342    uk) COUNTRY="ua"
343        SETLANG="uk"
344        LOCALE="uk_UA"
345        ;;
346    vi) COUNTRY="vn"
347        SETLANG="vi"
348        LOCALE="en_US"
349        ;;
350    zh_CN) COUNTRY="cn"
351           SETLANG="zh_CN"
352           LOCALE="zh_CN"
353        ;;
354    zh_TW) COUNTRY="tw"
355           SETLANG="zh_TW"
356           LOCALE="zh_TW"
357        ;;
358    *) COUNTRY="C"
359       SETLANG="${TARGETLANG}"
360       LOCALE="en_US"
361       ;;
362   esac
363
364  export COUNTRY SETLANG LOCALE
365
366 };
367
368 # Function which sets the timezone on the system
369 set_timezone()
370 {
371   TZONE="$1"
372   cp ${FSMNT}/usr/share/zoneinfo/${TZONE} ${FSMNT}/etc/localtime
373 };
374
375 # Function which enables / disables NTP
376 set_ntp()
377 {
378   ENABLED="$1"
379   if [ "$ENABLED" = "yes" -o "${ENABLED}" = "YES" ]
380   then
381     cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep 'ntpd_enable="YES"' >/dev/null 2>/dev/null
382     if [ "$?" != "0" ]
383     then
384       echo 'ntpd_enable="YES"' >>${FSMNT}/etc/rc.conf
385       echo 'ntpd_sync_on_start="YES"' >>${FSMNT}/etc/rc.conf
386     fi
387   else
388     cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep 'ntpd_enable="YES"' >/dev/null 2>/dev/null
389     if [ "$?" != "0" ]
390     then
391       sed -i.bak 's|ntpd_enable="YES"||g' ${FSMNT}/etc/rc.conf
392     fi
393   fi
394 };
395
396 # Starts checking for localization directives
397 run_localize()
398 {
399   KEYLAYOUT="NONE"
400   KEYMOD="NONE"
401   KEYVAR="NONE"
402
403   while read line
404   do
405     # Check if we need to do any localization
406     echo $line | grep "^localizeLang=" >/dev/null 2>/dev/null
407     if [ "$?" = "0" ]
408     then
409
410       # Set our country / lang / locale variables
411       get_value_from_string "$line"
412       localize_get_codes ${VAL}
413
414       get_value_from_string "$line"
415       # If we are doing PC-BSD install, localize it as well as FreeBSD base
416       if [ "${INSTALLTYPE}" != "FreeBSD" ]
417       then
418         localize_pcbsd "$VAL"
419       fi
420       localize_freebsd "$VAL"
421     fi
422
423     # Check if we need to do any keylayouts
424     echo $line | grep "^localizeKeyLayout=" >/dev/null 2>/dev/null
425     if [ "$?" = "0" ] ; then
426       get_value_from_string "$line"
427       KEYLAYOUT="$VAL"
428     fi
429
430     # Check if we need to do any key models
431     echo $line | grep "^localizeKeyModel=" >/dev/null 2>/dev/null
432     if [ "$?" = "0" ] ; then
433       get_value_from_string "$line"
434       KEYMOD="$VAL"
435     fi
436
437     # Check if we need to do any key variant
438     echo $line | grep "^localizeKeyVariant=" >/dev/null 2>/dev/null
439     if [ "$?" = "0" ] ; then
440       get_value_from_string "$line"
441       KEYVAR="$VAL"
442     fi
443
444
445     # Check if we need to set a timezone
446     echo $line | grep "^timeZone=" >/dev/null 2>/dev/null
447     if [ "$?" = "0" ] ; then
448       get_value_from_string "$line"
449       set_timezone "$VAL"
450     fi
451
452     # Check if we need to set a timezone
453     echo $line | grep "^enableNTP=" >/dev/null 2>/dev/null
454     if [ "$?" = "0" ] ; then
455       get_value_from_string "$line"
456       set_ntp "$VAL"
457     fi
458   done <${CFGF}
459
460   if [ "${INSTALLTYPE}" != "FreeBSD" ] ; then
461     # Do our X keyboard localization
462     localize_x_keyboard "${KEYMOD}" "${KEYLAYOUT}" "${KEYVAR}" "${COUNTRY}"
463   fi
464
465   # Check if we want to prunt any other KDE lang files to save some disk space
466   get_value_from_cfg localizePrune
467   if [ "${VAL}" = "yes" -o "${VAL}" = "YES" ] ; then
468     localize_prune_langs
469   fi
470
471   # Update the login.conf db, even if we didn't localize, its a good idea to make sure its up2date
472   run_chroot_cmd "/usr/bin/cap_mkdb /etc/login.conf" >/dev/null 2>/dev/null
473
474 };