1 if [ ! "$_TIMEZONE_ZONES_SUBR" ]; then _TIMEZONE_ZONES_SUBR=1
3 # Copyright (c) 2011-2012 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." timezone/zones.subr
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/strings.subr
36 f_include $BSDCFG_SHARE/timezone/continents.subr
38 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
39 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41 ############################################################ CONFIGURATION
46 _PATH_ZONETAB="/usr/share/zoneinfo/zone.tab"
47 _PATH_ZONEINFO="/usr/share/zoneinfo"
48 _PATH_LOCALTIME="/etc/localtime"
49 _PATH_DB="/var/db/zoneinfo"
52 # Export required i18n messages for awk(1) ENVIRON visibility
54 export msg_conflicting_zone_definition
55 export msg_country_code_invalid
56 export msg_country_code_unknown
57 export msg_invalid_country_code
58 export msg_invalid_format
59 export msg_invalid_region
60 export msg_invalid_zone_name
61 export msg_zone_multiply_defined
62 export msg_zone_must_have_description
64 ############################################################ FUNCTIONS
68 # Read the zone descriptions database in _PATH_ZONETAB:
69 # /usr/share/zoneinfo/zone.tab on all OSes
71 # The format of this file (on all OSes) is:
72 # code coordinates TZ comments
74 # With each of the following elements (described below) being separated by a
75 # single tab character:
78 # The ISO 3166 2-character country code.
80 # Latitude and logitude of the zone's principal location in ISO
81 # 6709 sign-degrees-minutes-seconds format, either +-DDMM+-DDDMM
82 # or +-DDMMSS+-DDDMMSS, first latitude (+ is north), then long-
85 # Zone name used in value of TZ environment variable.
87 # Comments; present if and only if the country has multiple rows.
89 # Required variables [from continents.subr]:
92 # Space-separated list of continents.
94 # Directory element in _PATH_ZONEINFO for the continent
97 # Required variables [created by f_read_iso3166_table from iso3166.subr]:
100 # Country name of the country represented by CODE, the 2-
101 # character country code.
103 # Variables created by this function:
105 # country_CODE_nzones
106 # Either set to `-1' to indicate that the 2-character country
107 # code has only a single zone associated with it (and therefore
108 # you should query the `country_CODE_*' environment variables),
109 # or set to `0' or higher to indicate how many zones are assoc-
110 # iated with the given country code. When multiple zones are
111 # configured for a single code, you should instead query the
112 # `country_CODE_*_N' environment variables (e.g., `echo
113 # $country_AQ_descr_1' prints the description of the first
114 # timezone in Antarctica).
115 # country_CODE_filename
116 # The ``filename'' portion of the TZ value that appears after the
117 # `/' (e.g., `Hong_Kong' from `Asia/Hong_Kong' or `Isle_of_Man'
118 # from `Europe/Isle_of_Man').
120 # The ``continent'' portion of the TZ value that appears before
121 # the `/' (e.g., `Asia' from `Asia/Hong_Kong' or `Europe' from
122 # `Europe/Isle_of_Man').
124 # The comments associated with the ISO 3166 code entry (if any).
126 # NOTE: CODE is the 2-character country code.
128 # This function is a two-parter. Below is the awk(1) portion of the function,
129 # afterward is the sh(1) function which utilizes the below awk script.
132 # Variables that should be defined on the invocation line:
133 # -v progname="progname"
140 # Initialize continents array/map (name => id)
142 split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/)
147 name = ENVIRON["continent_" cont "_name"]
148 continents[name] = cont
151 function die(fmt, argc, argv)
153 printf "f_die 1 \"%%s: %s\" \"%s\"", fmt, progname
154 for (n = 1; n <= argc; n++)
155 printf " \"%s\"", argv[n]
160 function find_continent(name)
162 return continents[name]
164 function add_zone_to_country(lineno, tlc, descr, file, cont)
167 # Validate the two-character country code
169 if (!match(tlc, /^[A-Z][A-Z]$/))
174 die(ENVRION["msg_country_code_invalid"], 3, argv)
176 if (!ENVIRON["country_" tlc "_name"])
181 die(ENVIRON["msg_country_code_unknown"], 3, argv)
185 # Add Zone to an array that we will parse at the end
187 if (length(descr) > 0)
189 if (country_nzones[tlc] < 0)
193 die(ENVIRON["msg_conflicting_zone_definition"], 2, argv)
196 n = ++country_nzones[tlc]
197 country_cont[tlc,n] = cont
198 country_filename[tlc,n] = file
199 country_descr[tlc,n] = descr
203 if (country_nzones[tlc] > 0)
207 die(ENVIRON["msg_zone_must_have_description"], 2, argv)
209 if (country_nzones[tlc] < 0)
213 die(ENVIRON["msg_zone_multiply_defined"], 2, argv)
216 country_nzones[tlc] = -1
217 country_cont[tlc] = cont
218 country_filename[tlc] = file
221 function print_country_code(tlc)
223 nz = country_nzones[tlc]
225 printf "country_%s_nzones=%d\n", tlc, nz
226 printf "export country_%s_nzones\n", tlc
230 printf "country_%s_cont=\"%s\"\n", tlc, country_cont[tlc]
231 printf "export country_%s_cont\n", tlc
232 printf "country_%s_filename=\"%s\"\n",
233 tlc, country_filename[tlc]
240 printf "country_%s_cont_%d=\"%s\"\n",
241 tlc, n, country_cont[tlc,n]
242 printf "export country_%s_cont_%d\n", tlc, n
243 printf "country_%s_filename_%d=\"%s\"\n",
244 tlc, n, country_filename[tlc,n]
245 printf "country_%s_descr_%d=\"%s\"\n",
246 tlc, n, country_descr[tlc,n]
258 # Split the current record (on TAB) into an array
260 if (split($0, line, /\t/) < 2)
264 die(ENVIRON["msg_invalid_format"], 2, argv)
267 # Get the ISO3166-1 (Alpha 1) 2-letter country code
271 # Validate the two-character country code
273 if (length(tlc) != 2)
278 die(ENVIRON["msg_invalid_country_code"], 3, argv)
285 # Validate the TZ field
292 die(ENVIRON["msg_invalid_zone_name"], 3, argv)
296 # Get the continent portion of the TZ field
299 sub("/.*$", "", contbuf)
302 # Validate the continent
304 cont = find_continent(contbuf)
310 die(ENVIRON["msg_invalid_region"], 3, argv)
314 # Get the filename portion of the TZ field
317 sub("^[^/]*/", "", filename)
320 # Calculate the substr start-position of the comment
325 descr_start += length(line[n]) + 1
327 # Get the comment field
328 descr = substr($0, descr_start + 1)
330 add_zone_to_country(lineno, tlc, descr, filename, cont)
333 if (failed) exit failed
334 for (tlc in country_nzones)
335 print_country_code(tlc)
340 eval $( awk -v progname="$pgm" \
341 "$f_read_zones_awk" \
345 # f_install_zoneinfo_file $filename
347 # Installs a zone file to _PATH_LOCALTIME.
349 f_install_zoneinfo_file()
351 local funcname=f_install_zoneinfo_file
352 local zoneinfo_file="$1"
353 local copymode title msg height width
355 if [ -L "$_PATH_LOCALTIME" ]; then
357 elif [ ! -e "$_PATH_LOCALTIME" ]; then
358 # Nothing there yet...
364 if [ "$VERBOSE" ]; then
365 if [ ! "$zoneinfo_file" ]; then
366 f_sprintf msg "$msg_removing_file" "$_PATH_LOCALTIME"
367 elif [ "$copymode" ]; then
368 f_sprintf msg "$msg_copying_file" \
369 "$zoneinfo_file" "$_PATH_LOCALTIME"
371 f_sprintf msg "$msg_creating_symlink" \
372 "$_PATH_LOCALTIME" "$zoneinfo_file"
374 if [ "$USEDIALOG" ]; then
375 f_dialog_title "$msg_info"
376 f_dialog_msgbox "$msg"
377 f_dialog_title_restore
383 [ "$REALLYDOIT" ] || return $SUCCESS
385 local catch_args="-de"
386 [ "$USEDIALOG" ] && catch_args=
388 if [ ! "$zoneinfo_file" ]; then
389 f_eval_catch $catch_args $funcname rm \
390 'rm -f "%s"' "$_PATH_LOCALTIME" || return $FAILURE
391 f_eval_catch $catch_args $funcname rm \
392 'rm -f "%s"' "$_PATH_DB" || return $FAILURE
394 if [ "$VERBOSE" ]; then
395 f_sprintf msg "$msg_removed_file" "$_PATH_LOCALTIME"
396 if [ "$USEDIALOG" ]; then
397 f_dialog_title "$msg_done"
398 f_dialog_msgbox "$msg"
399 f_dialog_title_restore
407 if [ "$copymode" ]; then
408 f_eval_catch $catch_args $funcname rm \
409 'rm -f "%s"' "$_PATH_LOCALTIME" || return $FAILURE
410 f_eval_catch $catch_args $funcname sh \
411 'umask 222 && :> "%s"' "$_PATH_LOCALTIME" ||
413 f_eval_catch $catch_args $funcname sh \
415 "$zoneinfo_file" "$_PATH_LOCALTIME" || return $FAILURE
417 f_eval_catch $catch_args $funcname sh \
418 '( :< "%s" )' "$zoneinfo_file" || return $FAILURE
419 f_eval_catch $catch_args $funcname rm \
420 'rm -f "%s"' "$_PATH_LOCALTIME" || return $FAILURE
421 f_eval_catch $catch_args $funcname ln \
423 "$zoneinfo_file" "$_PATH_LOCALTIME" || return $FAILURE
426 if [ "$VERBOSE" ]; then
427 if [ "$copymode" ]; then
428 f_sprintf msg "$msg_copied_timezone_file" \
429 "$zoneinfo_file" "$_PATH_LOCALTIME"
431 f_sprintf msg "$msg_created_symlink" \
432 "$_PATH_LOCALTIME" "$zoneinfo_file"
434 if [ "$USEDIALOG" ]; then
435 f_dialog_title "$msg_done"
436 f_dialog_msgbox "$msg"
437 f_dialog_title_restore
446 # f_install_zoneinfo $zoneinfo
448 # Install a zoneinfo file relative to _PATH_ZONEINFO. The given $zoneinfo
449 # will be written to _PATH_DB (usable later with the `-r' flag).
456 f_install_zoneinfo_file "$_PATH_ZONEINFO/$zoneinfo"
459 # Save knowledge for later
460 if [ "$REALLYDOIT" -a $rv -eq $SUCCESS ]; then
461 if true 2> /dev/null > "$_PATH_DB"; then
462 cat <<-EOF > "$_PATH_DB"
471 # f_confirm_zone $filename
473 # Prompt the user to confirm the new timezone data. The first (and only)
474 # argument should be the pathname to the zoneinfo file, either absolute or
475 # relative to `/usr/share/zoneinfo' (e.g., "America/Los_Angeles").
477 # The return status is 0 if "Yes" is chosen, 1 if "No", and 255 if Esc is
478 # pressed (see dialog(1) for additional details).
483 f_dialog_title "$msg_confirmation"
484 local title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE"
485 f_dialog_title_restore
486 local tm_zone="$( TZ="$filename" date +%Z )"
487 local prompt # Calculated below
488 local height=5 width=72
490 f_sprintf prompt "$msg_look_reasonable" "$tm_zone"
491 if [ "$USE_XDIALOG" ]; then
492 height=$(( $height + 4 ))
495 --backtitle "$btitle" \
496 --ok-label "$msg_yes" \
497 --cancel-label "$msg_no" \
498 --yesno "$prompt" $height $width
502 --backtitle "$btitle" \
503 --yes-label "$msg_yes" \
504 --no-label "$msg_no" \
505 --yesno "$prompt" $height $width
511 # Resets to the UTC timezone.
515 f_confirm_zone "" || return $FAILURE
516 f_install_zoneinfo_file ""
519 ############################################################ MAIN
521 f_dprintf "%s: Successfully loaded." timezone/zones.subr
523 fi # ! $_TIMEZONE_ZONES_SUBR