aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/pc-sysinstall/backend/functions-localize.sh
blob: b92e710f19602babf711cecf9d7facb87b0e62a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/bin/sh
#-
# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$

# Functions which runs commands on the system

. ${BACKEND}/functions.sh
. ${BACKEND}/functions-parse.sh


# Function which localizes a FreeBSD install
localize_freebsd()
{
  sed -i.bak "s/lang=en_US/lang=${LOCALE}/g" ${FSMNT}/etc/login.conf
  rm ${FSMNT}/etc/login.conf.bak
};

localize_x_desktops() {

  # Check for and customize KDE lang
  ##########################################################################

  # Check if we can localize KDE via skel
  if [ -e "${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals" ] ; then
    sed -i '' "s/Country=us/Country=${COUNTRY}/g" ${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals
    sed -i '' "s/Country=us/Country=${COUNTRY}/g" ${FSMNT}/root/.kde4/share/config/kdeglobals
    sed -i '' "s/Language=en_US/Language=${SETLANG}:${LOCALE}/g" ${FSMNT}/usr/share/skel/.kde4/share/config/kdeglobals
  fi

  # Check if we have a KDE root config
  if [ -e "${FSMNT}/root/.kde4/share/config/kdeglobals" ] ; then
    sed -i '' "s/Language=en_US/Language=${SETLANG}:${LOCALE}/g" ${FSMNT}/root/.kde4/share/config/kdeglobals
  fi

  # Check for KDM
  if [ -e "${FSMNT}/usr/local/kde4/share/config/kdm/kdmrc" ] ; then
    sed -i '' "s/Language=en_US/Language=${LOCALE}.UTF-8/g" ${FSMNT}/usr/local/kde4/share/config/kdm/kdmrc
  fi

  # Check for and customize GNOME / GDM lang
  ##########################################################################

  # See if GDM is enabled and customize its lang
  cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q "gdm_enable=\"YES\"" 2>/dev/null
  if [ "$?" = "0" ] ; then
    echo "gdm_lang=\"${LOCALE}.UTF-8\"" >> ${FSMNT}/etc/rc.conf
  fi

};

# Function which localizes a PC-BSD install
localize_pcbsd()
{
  # Check if we have a localized splash screen and copy it
  if [ -e "${FSMNT}/usr/local/share/pcbsd/splash-screens/loading-screen-${SETLANG}.pcx" ]
  then
    cp ${FSMNT}/usr/local/share/pcbsd/splash-screens/loading-screen-${SETLANG}.pcx ${FSMNT}/boot/loading-screen.pcx    
  fi

};

localize_x_keyboard()
{
  KEYMOD="$1"
  KEYLAY="$2"
  KEYVAR="$3"
  COUNTRY="$4"
  OPTION="grp:alt_shift_toggle"
  SETXKBMAP=""

  if [ "${COUNTRY}" = "NONE" -o "${COUNTRY}" = "us" -o "${COUNTRY}" = "C" ] ; then
    #In this case we don't need any additional language
    COUNTRY=""
    OPTION=""
  else
    COUNTRY=",${COUNTRY}"
  fi

  if [ "${KEYMOD}" != "NONE" ]
  then
    SETXKBMAP="-model ${KEYMOD}"
    KXMODEL="${KEYMOD}"
  else
    KXMODEL="pc104"
  fi

  if [ "${KEYLAY}" != "NONE" ]
  then
    localize_key_layout "$KEYLAY"
    SETXKBMAP="${SETXKBMAP} -layout ${KEYLAY}"
    KXLAYOUT="${KEYLAY}"
  else
    KXLAYOUT="us"
  fi

  if [ "${KEYVAR}" != "NONE" ]
  then
    SETXKBMAP="${SETXKBMAP} -variant ${KEYVAR}"
    KXVAR="(${KEYVAR})"
  else
    KXVAR=""
  fi

  # Setup .xprofile with our setxkbmap call now
  if [ ! -z "${SETXKBMAP}" ]
  then
    if [ ! -e "${FSMNT}/usr/share/skel/.xprofile" ]
    then
      echo "#!/bin/sh" >${FSMNT}/usr/share/skel/.xprofile
    fi

    # Save the keyboard layout for user / root X logins
    echo "setxkbmap ${SETXKBMAP}" >>${FSMNT}/usr/share/skel/.xprofile
    chmod 755 ${FSMNT}/usr/share/skel/.xprofile
    cp ${FSMNT}/usr/share/skel/.xprofile ${FSMNT}/root/.xprofile

    # Save it for KDM
    if [ -e "${FSMNT}/usr/local/kde4/share/config/kdm/Xsetup" ] ; then
      echo "setxkbmap ${SETXKBMAP}" >>${FSMNT}/usr/local/kde4/share/config/kdm/Xsetup
    fi
  fi
 
  # Create the kxkbrc configuration using these options
  if [ -d "${FSMNT}/usr/share/skel/.kde4/share/config" ] ; then
    echo "[Layout]
DisplayNames=${KXLAYOUT}${COUNTRY}
IndicatorOnly=false
LayoutList=${KXLAYOUT}${KXVAR}${COUNTRY}
Model=${KXMODEL}
Options=${OPTION}
ResetOldOptions=true
ShowFlag=true
ShowSingle=false
SwitchMode=WinClass
Use=true " >${FSMNT}/usr/share/skel/.kde4/share/config/kxkbrc
  fi

};

localize_key_layout()
{

  KEYLAYOUT="$1"

  # Set the keylayout in rc.conf
  case ${KEYLAYOUT} in
    am) KEYLAYOUT_CONSOLE="hy.armscii-8" ;;
    ca) KEYLAYOUT_CONSOLE="fr_CA.acc.iso" ;;
    ch) KEYLAYOUT_CONSOLE="swissgerman.iso" ;;
    cz) KEYLAYOUT_CONSOLE="cz.iso2" ;;
    de) KEYLAYOUT_CONSOLE="german.iso" ;;
    dk) KEYLAYOUT_CONSOLE="danish.iso" ;;
    ee) KEYLAYOUT_CONSOLE="estonian.iso" ;;
    es) KEYLAYOUT_CONSOLE="spanish.iso" ;;
    fi) KEYLAYOUT_CONSOLE="finnish.iso" ;;
    is) KEYLAYOUT_CONSOLE="icelandic.iso" ;;
    jp) KEYLAYOUT_CONSOLE="jp.106" ;;
    nl) KEYLAYOUT_CONSOLE="dutch.iso.acc" ;;
    no) KEYLAYOUT_CONSOLE="norwegian.iso" ;;
    pl) KEYLAYOUT_CONSOLE="pl_PL.ISO8859-2" ;;
    ru) KEYLAYOUT_CONSOLE="ru.koi8-r" ;;
    sk) KEYLAYOUT_CONSOLE="sk.iso2" ;;
    se) KEYLAYOUT_CONSOLE="swedish.iso" ;;
    tr) KEYLAYOUT_CONSOLE="tr.iso9.q" ;;
    gb) KEYLAYOUT_CONSOLE="uk.iso" ;;
     *)  if [ ! -z "${KEYLAYOUT}" ]
         then
           KEYLAYOUT_CONSOLE="${KEYLAYOUT}.iso"
         fi
        ;;
  esac

  if [ -n "${KEYLAYOUT_CONSOLE}" ]
  then
    echo "keymap=\"${KEYLAYOUT_CONSOLE}\"" >>${FSMNT}/etc/rc.conf
  fi

};

#  Function which prunes other l10n files from the KDE install
localize_prune_langs()
{
  get_value_from_cfg localizeLang
  KEEPLANG="$VAL"
  if [ -z "$KEEPLANG" ] ; then
    KEEPLANG="en"
  fi
  export KEEPLANG 

  echo_log "Pruning other l10n files, keeping ${KEEPLANG}"

  # Create the script to do uninstalls
  echo '#!/bin/sh

  for i in `pkg_info -xEI kde-l10n`
  do
    echo "$i" | grep "${KEEPLANG}-kde"
    if [ $? -ne 0 ] ; then
      pkg_delete ${i}
    fi
  done
  ' > ${FSMNT}/.pruneLangs.sh

  chmod 755 ${FSMNT}/.pruneLangs.sh
  chroot ${FSMNT} /.pruneLangs.sh >/dev/null 2>/dev/null
  rm ${FSMNT}/.pruneLangs.sh

};

# Function which sets COUNTRY SETLANG and LOCALE based upon $1
localize_get_codes()
{ 
  TARGETLANG="${1}"
  # Setup the presets for the specific lang
  case $TARGETLANG in
    af)
      COUNTRY="C"
      SETLANG="af"
      LOCALE="af_ZA"
      ;;
    ar)
	  COUNTRY="C"
      SETLANG="ar"
      LOCALE="en_US"
      ;;
    az)
	  COUNTRY="C"
      SETLANG="az"
      LOCALE="en_US"
      ;;
    ca)
	  COUNTRY="es"
      SETLANG="es:ca"
      LOCALE="ca_ES"
      ;;
    be)
	  COUNTRY="be"
      SETLANG="be"
      LOCALE="be_BY"
      ;;
    bn)
	  COUNTRY="bn"
      SETLANG="bn"
      LOCALE="en_US"
      ;;
    bg)
	  COUNTRY="bg"
      SETLANG="bg"
      LOCALE="bg_BG"
      ;;
    cs)
	  COUNTRY="cz"
      SETLANG="cs"
      LOCALE="cs_CZ"
      ;;
    da)
	  COUNTRY="dk"
      SETLANG="da"
      LOCALE="da_DK"
      ;;
    de)
	  COUNTRY="de"
      SETLANG="de"
      LOCALE="de_DE"
      ;;
    en_GB)
	  COUNTRY="gb"
      SETLANG="en_GB:cy"
      LOCALE="en_GB"
      ;;
    el)
	  COUNTRY="gr"
      SETLANG="el:gr"
      LOCALE="el_GR"
      ;;
    es)
	  COUNTRY="es"
      SETLANG="es"
      LOCALE="es_ES"
      ;;
    es_LA)
	  COUNTRY="us"
      SETLANG="es:en_US"
      LOCALE="es_ES"
      ;;
    et)
	  COUNTRY="ee"
      SETLANG="et"
      LOCALE="et_EE"
      ;;
    fr)
	  COUNTRY="fr"
      SETLANG="fr"
      LOCALE="fr_FR"
      ;;
    he)
	  COUNTRY="il"
      SETLANG="he:ar"
      LOCALE="he_IL"
      ;;
    hr)
	  COUNTRY="hr"
      SETLANG="hr"
      LOCALE="hr_HR"
      ;;
    hu)
	  COUNTRY="hu"
      SETLANG="hu"
      LOCALE="hu_HU"
      ;;
    it)
	  COUNTRY="it"
      SETLANG="it"
      LOCALE="it_IT"
      ;;
    ja)
	  COUNTRY="jp"
      SETLANG="ja"
      LOCALE="ja_JP"
      ;;
    ko)
	  COUNTRY="kr"
      SETLANG="ko"
      LOCALE="ko_KR"
      ;;
    nl)
	  COUNTRY="nl"
      SETLANG="nl"
      LOCALE="nl_NL"
      ;;
    nn)
	  COUNTRY="no"
      SETLANG="nn"
      LOCALE="en_US"
      ;;
    pa)
	  COUNTRY="pa"
      SETLANG="pa"
      LOCALE="en_US"
      ;;
    pl)
	  COUNTRY="pl"
      SETLANG="pl"
      LOCALE="pl_PL"
      ;;
    pt)
	  COUNTRY="pt"
      SETLANG="pt"
      LOCALE="pt_PT"
      ;;
    pt_BR)
	  COUNTRY="br"
      SETLANG="pt_BR"
      LOCALE="pt_BR"
      ;;
    ru)
	  COUNTRY="ru"
      SETLANG="ru"
      LOCALE="ru_RU"
      ;;
    sl)
	  COUNTRY="si"
      SETLANG="sl"
      LOCALE="sl_SI"
      ;;
    sk)
	  COUNTRY="sk"
      SETLANG="sk"
      LOCALE="sk_SK"
      ;;
    sv)
	  COUNTRY="se"
      SETLANG="sv"
      LOCALE="sv_SE"
      ;;
    uk)
	  COUNTRY="ua"
      SETLANG="uk"
      LOCALE="uk_UA"
      ;;
    vi)
	  COUNTRY="vn"
      SETLANG="vi"
      LOCALE="en_US"
      ;;
    zh_CN)
	  COUNTRY="cn"
      SETLANG="zh_CN"
      LOCALE="zh_CN"
      ;;
    zh_TW)
	  COUNTRY="tw"
      SETLANG="zh_TW"
      LOCALE="zh_TW"
      ;;
    *)
	  COUNTRY="C"
      SETLANG="${TARGETLANG}"
      LOCALE="en_US"
      ;;
  esac

  export COUNTRY SETLANG LOCALE

};

# Function which sets the timezone on the system
set_timezone()
{
  TZONE="$1"
  cp ${FSMNT}/usr/share/zoneinfo/${TZONE} ${FSMNT}/etc/localtime
};

# Function which enables / disables NTP
set_ntp()
{
  ENABLED="$1"
  if [ "$ENABLED" = "yes" -o "${ENABLED}" = "YES" ]
  then
    cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'ntpd_enable="YES"' 2>/dev/null
    if [ $? -ne 0 ]
    then
      echo 'ntpd_enable="YES"' >>${FSMNT}/etc/rc.conf
      echo 'ntpd_sync_on_start="YES"' >>${FSMNT}/etc/rc.conf
    fi
  else
    cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'ntpd_enable="YES"' 2>/dev/null
    if [ $? -ne 0 ]
    then
      sed -i.bak 's|ntpd_enable="YES"||g' ${FSMNT}/etc/rc.conf
    fi
  fi
};

# Starts checking for localization directives
run_localize()
{
  KEYLAYOUT="NONE"
  KEYMOD="NONE"
  KEYVAR="NONE"

  while read line
  do
    # Check if we need to do any localization
    echo $line | grep -q "^localizeLang=" 2>/dev/null
    if [ $? -eq 0 ]
    then

      # Set our country / lang / locale variables
      get_value_from_string "$line"
      localize_get_codes ${VAL}

      get_value_from_string "$line"
      # If we are doing PC-BSD install, localize it as well as FreeBSD base
      if [ "${INSTALLTYPE}" != "FreeBSD" ]
      then
        localize_pcbsd "$VAL"
      fi

      # Localize FreeBSD
      localize_freebsd "$VAL"

      # Localize any X pkgs
      localize_x_desktops "$VAL"
    fi

    # Check if we need to do any keylayouts
    echo $line | grep -q "^localizeKeyLayout=" 2>/dev/null
    if [ $? -eq 0 ] ; then
      get_value_from_string "$line"
      KEYLAYOUT="$VAL"
    fi

    # Check if we need to do any key models
    echo $line | grep -q "^localizeKeyModel=" 2>/dev/null
    if [ $? -eq 0 ] ; then
      get_value_from_string "$line"
      KEYMOD="$VAL"
    fi

    # Check if we need to do any key variant
    echo $line | grep -q "^localizeKeyVariant=" 2>/dev/null
    if [ $? -eq 0 ] ; then
      get_value_from_string "$line"
      KEYVAR="$VAL"
    fi


    # Check if we need to set a timezone
    echo $line | grep -q "^timeZone=" 2>/dev/null
    if [ $? -eq 0 ] ; then
      get_value_from_string "$line"
      set_timezone "$VAL"
    fi

    # Check if we need to set a timezone
    echo $line | grep -q "^enableNTP=" 2>/dev/null
    if [ $? -eq 0 ] ; then
      get_value_from_string "$line"
      set_ntp "$VAL"
    fi
  done <${CFGF}

  if [ "${INSTALLTYPE}" != "FreeBSD" ] ; then
    # Do our X keyboard localization
    localize_x_keyboard "${KEYMOD}" "${KEYLAYOUT}" "${KEYVAR}" "${COUNTRY}"
  fi

  # Check if we want to prunt any other KDE lang files to save some disk space
  get_value_from_cfg localizePrune
  if [ "${VAL}" = "yes" -o "${VAL}" = "YES" ] ; then
    localize_prune_langs
  fi

  # Update the login.conf db, even if we didn't localize, its a good idea to make sure its up2date
  run_chroot_cmd "/usr/bin/cap_mkdb /etc/login.conf" >/dev/null 2>/dev/null

};