rc.country 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/bin/sh
  2. #(c) Copyright 2012 Barry Kauler
  3. #2012 GPL licence v3 (refer: /usr/share/doc/legal)
  4. #120328 rewrite script.
  5. #120627 use keymaps from kbd pkg if present.
  6. #120629 raspberry pi does not have a hw clock, set date to reasonable value.
  7. #130501 passed param from rc.sysinit.
  8. #130513 modify handling of "ask" passed param.
  9. . /etc/rc.d/PUPSTATE
  10. . /etc/DISTRO_SPECS #has DISTRO_XORG_AUTO
  11. STATUS=0
  12. . /etc/clock
  13. [ $pkeys ] && PKEYS=$pkeys #boot parameter
  14. ASKKMAP="yes"; ASKLANG="yes"; ASKCLOCK="no"
  15. [ "$DISTRO_XORG_AUTO" = "yes" ] && ASKKMAP="no" && ASKLANG="no"
  16. [ -f /tmp/bootcnt.txt ] && ASKKMAP="yes" && ASKLANG="yes" #file created in 'init' if pfix=nox.
  17. #130501 passed param from rc.sysinit (rc.country is called 2nd time)... 130513 no, only one call.
  18. if [ $1 ];then
  19. ASKKMAP="yes"
  20. ASKLANG="yes"
  21. fi
  22. #120627
  23. KMAPPATH='/lib/keymaps'
  24. [ -d /usr/share/kbd/keymaps/i386 ] && KMAPPATH='/usr/share/kbd/keymaps/i386'
  25. [ -d /usr/share/keymaps/i386 ] && KMAPPATH='/usr/share/keymaps/i386' #debian, gentoo. note, debian files have extension .kmap.gz, gentoo have .map.gz
  26. KEXT=''
  27. [ -f ${KMAPPATH}/qwerty/us.map ] && KEXT='.map'
  28. [ -f ${KMAPPATH}/qwerty/us.map.gz ] && KEXT='.map'
  29. [ -f ${KMAPPATH}/qwerty/us.kmap ] && KEXT='.kmap'
  30. [ -f ${KMAPPATH}/qwerty/us.kmap.gz ] && KEXT='.kmap'
  31. ######keyboard section######
  32. askkbdlayout ()
  33. {
  34. echo -n "us${KEXT}" > /etc/keymap #default.
  35. echo -n "" > /etc/codepage # "
  36. FIRSTITEM="us \"(USA)\""
  37. echo -n "" >/tmp/keybdlist.txt
  38. case $KMAPPATH in #120627
  39. /lib/keymaps) KMAPFILES="`find /lib/keymaps -mindepth 1 -maxdepth 1 -name '*.gz' | sort`" ;; #ex: us.gz
  40. *) KMAPFILES="`find ${KMAPPATH}/[^i]* -name '*.map' -o -name '*.kmap' -o -name '*.map.gz' -o -name '*.kmap.gz' -not -name 'defkeymap*' | sort`" ;;
  41. esac
  42. for AFILE in $KMAPFILES
  43. do
  44. AKMAP="`basename $AFILE .gz`" #us, fr, br, etc.
  45. AKMAP="`basename $AKMAP .kmap`" #120627
  46. AKMAP="`basename $AKMAP .map`" #120627
  47. case $AKMAP in
  48. us*) ACNTRY="(USA)";;
  49. fr*) ACNTRY="(France)";;
  50. it*) ACNTRY="(Italy)";;
  51. ba*) ACNTRY="(Bashkir)";;
  52. be*) ACNTRY="(Belgium)";;
  53. bg*) ACNTRY="(Bulgaria)";;
  54. br*) ACNTRY="(Brazil)";;
  55. by*) ACNTRY="(Byelorussian)";;
  56. cf*) ACNTRY="(French-Canadian)";;
  57. croat*) ACNTRY="(Croatia)";;
  58. cz*) ACNTRY="(Czech)";;
  59. de) ACNTRY="(Germany)";;
  60. de_*) ACNTRY="(Germany)";;
  61. de-*) ACNTRY="(Germany)";;
  62. dk*) ACNTRY="(Denmark)";;
  63. es*) ACNTRY="(Spain)";;
  64. fi*) ACNTRY="(Finland)";;
  65. hu*) ACNTRY="(Hungary)";;
  66. ka*) ACNTRY="(Kazakhstan)";;
  67. ky*) ACNTRY="(Kyrgyzstan)";;
  68. et*) ACNTRY="(Estonia)";;
  69. lt*) ACNTRY="(Lithuania)";;
  70. mk*) ACNTRY="(Macedonia)";;
  71. sr*) ACNTRY="(Serbia)";;
  72. tr*) ACNTRY="(Turkey)";;
  73. tt*) ACNTRY="(Tatar)";;
  74. sg*) ACNTRY="(Sango)";;
  75. ua*) ACNTRY="(Ukraine)";;
  76. gr*) ACNTRY="(Greece)";;
  77. il*) ACNTRY="(Israel)";;
  78. is*) ACNTRY="(Iceland)";;
  79. jp*) ACNTRY="(Japan)";;
  80. pc*) ACNTRY="(Japanese/English)";;
  81. la*) ACNTRY="(Latin_America)";;
  82. nl*) ACNTRY="(Netherlands)";;
  83. no*) ACNTRY="(Norway)";;
  84. pl*) ACNTRY="(Poland)";;
  85. pt*) ACNTRY="(Portugal)";;
  86. ro*) ACNTRY="(Romania)";;
  87. ru*) ACNTRY="(Russia)";;
  88. se*) ACNTRY="(Sweden)";;
  89. sv*) ACNTRY="(Sweden)";;
  90. sk*) ACNTRY="(Slovakia)";;
  91. sl*) ACNTRY="(Slovenia)";;
  92. uk*) ACNTRY="(UK)";;
  93. wangbe*) ACNTRY="(Belgium)";;
  94. azerty) ACNTRY="(Tandon)";;
  95. dvorak-r) ACNTRY="(Right_single-handed)";;
  96. dvorak-l) ACNTRY="(Left_single-handed)";;
  97. *) ACNTRY="-";;
  98. esac
  99. echo -e "$AKMAP \"${ACNTRY}\"" >> /tmp/keybdlist.txt
  100. done
  101. KEYLIST="${FIRSTITEM} `sort /tmp/keybdlist.txt | tr '\n' ' '`"
  102. echo '#!/bin/sh' >/tmp/keydlg
  103. echo 'dialog --aspect 10 --no-cancel --menu "Select the keyboard layout:\n(UP/DOWN arrows then ENTER key)" 0 0 0 \' >>/tmp/keydlg #' geany fix
  104. echo "$KEYLIST 2> /tmp/kbdextlayout.txt" >>/tmp/keydlg
  105. echo 'exit $?' >>/tmp/keydlg
  106. chmod 755 /tmp/keydlg
  107. /tmp/keydlg >/dev/console
  108. [ $? != 0 ] && return 1
  109. clear > /dev/console 2>&1 #clear screen
  110. KMAP=`cat /tmp/kbdextlayout.txt`
  111. case $KMAP in #note, same/similar code in /usr/sbin/input-wizard, init, quicksetup.
  112. de*|be*|br*|dk*|es*|fi*|fr*|it*|no*|se*|sv*|pt*) echo -n "850" > /etc/codepage ;;
  113. cz*|hu*|pl*|ro*|sk*|croat*|slovene*) echo -n "852" > /etc/codepage ;;
  114. esac
  115. echo -n "${KMAP}${KEXT}" > /etc/keymap
  116. return 0
  117. }
  118. if [ "$ASKKMAP" = "no" ];then
  119. #'quicksetup' is run 1st time X starts.
  120. [ ! -f /etc/keymap ] && echo -n "us${KEXT}" > /etc/keymap
  121. [ ! -f /etc/codepage ] && echo -n "" > /etc/codepage
  122. else
  123. [ ! -e /etc/keymap ] && askkbdlayout #creates /etc/keymap, /etc/codepage.
  124. fi
  125. if [ -s /etc/codepage ];then
  126. CODEPAGE="`cat /etc/codepage`"
  127. case $CODEPAGE in
  128. 850) modprobe nls_cp850 ;;
  129. 852) modprobe nls_cp852 ; modprobe nls_iso8859-2 ;;
  130. esac
  131. fi
  132. #note, "keymap-set --xopt" is run in xorgwizard and xorgwizard-automatic,
  133. # "keymap-set --load" is run in quicksetup.
  134. if [ -s /etc/keymap ];then
  135. NEED2LOADKMAP="yes"
  136. [ -d /initrd -a "$PKEYS" ] && NEED2LOADKMAP="no" #already done in initrd.
  137. if [ "$NEED2LOADKMAP" = "yes" ];then
  138. KMAP="`cat /etc/keymap`"
  139. case $KMAPPATH in #120627
  140. /lib/keymaps) gzip -dcf /lib/keymaps/$KMAP.gz | loadkmap ;;
  141. *) loadkeys $KMAP ;; #note i think don't need to specify the extension (but it is in KMAP).
  142. esac
  143. fi
  144. fi
  145. ######end keyboard section######
  146. ######locale section######
  147. LANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '='`"
  148. CLANG="`echo "$LANG" | sed -e 's/\.UTF-8/\.utf8/'`" #fix, locale -a always prints .utf8
  149. if [ "$ASKLANG" = "no" ];then
  150. #'quicksetup' is run 1st time X starts.
  151. #need this default locale for X to start first time...
  152. [ ! -d /usr/lib/locale/en_US ] && localedef -f ISO-8859-1 -i en_US --no-archive en_US >/dev/null
  153. [ ! -d /usr/lib/locale/${CLANG} ] && /usr/sbin/chooselocale cli quiet #ask no questions, will run localedef.
  154. else
  155. #localedef creates locale files in /usr/lib/locale...
  156. if [ "`locale -a | grep "$CLANG"`" = "" ];then
  157. if [ "$LANG" = "en_US" ];then
  158. /usr/sbin/chooselocale cli > /dev/console
  159. else
  160. /usr/sbin/chooselocale cli quiet #ask no questions, will run localedef.
  161. fi
  162. /sbin/load_consolefont #note, also called from rc.sysinit, xwin.
  163. ASKCLOCK="yes"
  164. fi
  165. fi
  166. ######end locale section######
  167. ######clock section######
  168. #120629 raspberry pi does not have a hw clock, set date to reasonable value...
  169. HWDATE="`hwclock --show 2>/dev/null`" #ex: "Fri 29 Jun 2012 07:45:28 AM WST -0.725833 seconds"
  170. if [ "$HWDATE" = "" ];then
  171. if [ -f /var/local/shutdown_date_saved ];then #see /etc/rc.d/rc.shutdown
  172. date -s "`cat /var/local/shutdown_date_saved`"
  173. else
  174. #either of these formats can set the date: "29 JUN 2012 10:00:00" "2012-06-28 16:20:08"
  175. HWDATE="`stat --format=%z /etc/DISTRO_SPECS | cut -f 1 -d '.' | cut -f 1,2 -d ' '`" #creation date of build. ex: "2012-06-28 16:20:08"
  176. date -s "$HWDATE"
  177. [ ! -e /etc/localtime ] && ln -s /usr/share/zoneinfo/Australia/Perth /etc/localtime
  178. fi
  179. else
  180. if [ "$ASKCLOCK" = "yes" ];then
  181. /usr/sbin/timezone-set cli > /dev/console
  182. #choose hardware clock set to local or UTC...
  183. dialog --title "Set hardware-clock type" --yes-label "Local" --no-label "UTC" --yesno "Is the hardware clock on the computer set to the local time, or to UTC? Note, most PCs with Windows installed have the hardware clock set to the local time, so if in doubt choose that..." 0 0 > /dev/console
  184. if [ $? -eq 1 ];then #UTC
  185. HWCLOCKTIME='utc'
  186. hwPATTERN="s/^HWCLOCKTIME=[^#]*/HWCLOCKTIME='utc'/"
  187. sed -i "$hwPATTERN" /etc/clock #note, it is default 'localtime'.
  188. fi
  189. fi
  190. [ ! -e /etc/localtime ] && ln -s /usr/share/zoneinfo/Australia/Perth /etc/localtime
  191. #Ref: http://www.gnu.org/s/libc/manual/html_node/TZ-Variable.html
  192. TZ=$(readlink /etc/localtime|sed -e 's,/usr/share/zoneinfo/,,' -e 's,Etc/,,') #shinobar
  193. export TZ
  194. HWCLOCKTIME="localtime"
  195. [ -s /etc/clock ] && grep -qw '^[^#]*utc' /etc/clock && HWCLOCKTIME="utc" #shinobar
  196. #need to set Linux system time/date, from hardware clock...
  197. #hwclock --hctosys --localtime
  198. hwclock --hctosys --${HWCLOCKTIME}
  199. #...--hctosys reads cmos clock to system.
  200. #...--localtime means that cmos clock is set to local-time.
  201. fi
  202. ######end clock section######
  203. exit $STATUS
  204. ###END###