3 # Copyright (c) 2011-2015 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..." "$0"
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/mustberoot.subr
36 f_include $BSDCFG_SHARE/strings.subr
37 f_include $BSDCFG_SHARE/timezone/continents.subr
38 f_include $BSDCFG_SHARE/timezone/countries.subr
39 f_include $BSDCFG_SHARE/timezone/iso3166.subr
40 f_include $BSDCFG_SHARE/timezone/menus.subr
41 f_include $BSDCFG_SHARE/timezone/zones.subr
43 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
44 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
46 f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
49 ############################################################ CONFIGURATION
54 _PATH_DB="/var/db/zoneinfo"
55 _PATH_WALL_CMOS_CLOCK="/etc/wall_cmos_clock"
57 ############################################################ GLOBALS
71 # Dummy vars (populated dynamically)
73 COUNTRIES= # list of 2-character country codes created by f_read_iso3166_table
75 ############################################################ FUNCTIONS
79 # Display the dialog(1)-based application main menu.
83 local title="$DIALOG_TITLE"
84 local btitle="$DIALOG_BACKTITLE"
85 local prompt="$msg_select_region"
86 local defaultitem= # Calculated below
89 local height width rows
90 eval f_dialog_menu_size height width rows \
97 # Obtain default-item from previously stored selection
98 f_dialog_default_fetch defaultitem
101 menu_choice=$( eval $DIALOG \
102 --title \"\$title\" \
103 --backtitle \"\$btitle\" \
104 --hline \"\$hline\" \
105 --ok-label \"\$msg_ok\" \
106 --cancel-label \"\$msg_cancel\" \
107 --default-item \"\$defaultitem\" \
108 --menu \"\$prompt\" \
109 $height $width $rows \
110 $continent_menu_list \
111 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
114 f_dialog_data_sanitize menu_choice
115 f_dialog_menutag_store "$menu_choice"
116 f_dialog_default_store "$menu_choice"
120 ############################################################ MAIN
122 # Incorporate rc-file if it exists
123 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
126 # Process command-line arguments
128 while getopts C:ehnrsv$GETOPTS_STDARGS flag; do
130 C) CHROOTENV="$OPTARG" ;;
137 h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
140 shift $(( $OPTIND - 1 ))
145 f_dialog_title "$msg_time_zone"
146 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
150 # Process `-C chroot_directory' command-line argument
152 if [ "$CHROOTENV" ]; then
153 _PATH_ZONETAB="$CHROOTENV$_PATH_ZONETAB"
154 _PATH_ISO3166="$CHROOTENV$_PATH_ISO3166"
155 _PATH_ZONEINFO="$CHROOTENV$_PATH_ZONEINFO"
156 _PATH_LOCALTIME="$CHROOTENV$_PATH_LOCALTIME"
157 _PATH_DB="$CHROOTENV$_PATH_DB"
158 _PATH_WALL_CMOS_CLOCK="$CHROOTENV$_PATH_WALL_CMOS_CLOCK"
162 # Process `-r' command-line option
164 if [ "$REINSTALL" ]; then
165 [ -f "$_PATH_DB" -a -r "$_PATH_DB" ] ||
166 f_die 1 "$msg_cannot_open_for_reading" "$_PATH_DB"
167 f_eval_catch -dk zoneinfo "$0" cat 'cat "%s"' "$_PATH_DB" ||
168 f_die 1 "$msg_error_reading" "$_PATH_DB"
170 f_die 1 "$msg_unable_to_determine_name_from_db" "$_PATH_DB"
171 f_install_zoneinfo "$zoneinfo"
176 # If the arguments on the command-line do not specify a file,
177 # then interpret it as a zoneinfo name
179 if [ $# -ge 1 ]; then
182 if [ ! -f "$zoneinfo" ]; then
184 f_install_zoneinfo "$zoneinfo"
192 # Process the UTC option
194 if [ "$_PATH_WALL_CMOS_CLOCK" -a ! "$SKIPUTC" ]; then
195 f_dialog_title "$msg_select_local_or_utc"
196 title="$DIALOG_TITLE"
197 btitle="$DIALOG_BACKTITLE"
198 f_dialog_title_restore
199 msg="$msg_is_machine_clock_utc"
201 if [ "$USE_XDIALOG" ]; then
202 defaultno="default-no"
205 defaultno="defaultno"
209 if [ "$USE_XDIALOG" ]; then
212 --backtitle "$btitle" \
214 --ok-label "$msg_yes" \
215 --cancel-label "$msg_no" \
216 --yesno "$msg" $height $width
221 --backtitle "$btitle" \
223 --yes-label "$msg_yes" \
224 --no-label "$msg_no" \
225 --yesno "$msg" $height $width
229 if [ $result -eq $DIALOG_OK ]; then
232 f_quietly rm -f "$_PATH_WALL_CMOS_CLOCK"
234 # User chose NO, pressed ESC (or Ctrl-C), or closed box
236 ( umask 222 && :> "$_PATH_WALL_CMOS_CLOCK" )
241 # Process optional default zone argument
243 if [ $# -ge 1 ]; then
246 f_dialog_title "$msg_default_zone_provided"
247 f_sprintf msg "\n$msg_use_default_zone" "$default"
249 f_dialog_yesno "$msg" "$hline"
251 f_dialog_title_restore
253 if [ $result -eq $DIALOG_OK ]; then
255 f_install_zoneinfo_file "$default"
257 [ ! "$USE_XDIALOG" ] && f_dialog_clear
261 [ ! "$USE_XDIALOG" ] && f_dialog_clear
265 # Override the user-supplied umask
270 # Read databases and perform initialization
272 f_read_iso3166_table # creates $COUNTRIES and $country_*_name
273 f_read_zones # creates $country_*_{descr,cont,filename}
274 f_sort_countries # sorts the countries listed for each continent
275 f_make_menus # creates $continent_menu_list and $continent_*_menu_list
278 # Launch application main menu
285 if [ "$NEED_CONTINENT" ]; then
286 dialog_menu_main # prompt the user to select a continent/ocean
288 f_dialog_menutag_fetch mtag
290 if [ $retval -ne $DIALOG_OK ]; then
291 [ "$TZ_OR_FAIL" ] && f_die
297 continent=$( eval f_dialog_menutag2item \"\$mtag\" \
298 $continent_menu_list )
299 f_find_continent "$continent" cont
300 f_continent $cont title cont_title
301 f_continent $cont nitems nitems
302 f_OCEANP $cont isocean
305 if [ "$NEED_COUNTRY" ]; then
306 if [ "$cont_title" = "$continent_utc_title" ]; then
307 if f_set_zone_utc; then
316 # Short cut -- if there's only one country, don't post a menu.
318 if [ $nitems -eq 1 ]; then
322 # It's amazing how much good grammar really matters...
324 if [ ! "$isocean" ]; then
325 f_sprintf title "$msg_country_title" \
327 f_dialog_title "$title"
328 title="$DIALOG_TITLE"
329 btitle="$DIALOG_BACKTITLE"
330 f_dialog_title_restore
331 prompt="$msg_select_country"
333 f_sprintf title "$msg_island_and_group_title" \
335 f_dialog_title "$title"
336 title="$DIALOG_TITLE"
337 btitle="$DIALOG_BACKTITLE"
338 f_dialog_title_restore
339 prompt="$msg_select_island_or_group"
343 # Calculate size of menu
345 f_continent $cont menu_list menu_list
346 eval f_dialog_menu_size height width rows \
354 # Launch the country selection menu
356 tag=$( eval $DIALOG \
357 --title \"\$title\" \
358 --backtitle \"\$btitle\" \
359 --ok-label \"\$msg_ok\" \
360 --cancel-label \"\$msg_cancel\" \
361 --default-item \"\$defaultctry\" \
362 --menu \"\$prompt\" \
363 $height $width $rows \
365 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
368 f_dialog_data_sanitize tag
371 if [ $retval -ne $DIALOG_OK ]; then
373 continue # back to main menu
377 # Get the country code from the user's selection
378 f_continent $cont tlc_$tag tlc
384 # If the selection has only one zone (nzones == -1),
387 f_country $tlc nzones nzones
388 if [ $nzones -lt 0 ]; then
389 f_country $tlc cont real_cont
390 f_continent $real_cont name real_continent
391 f_country $tlc name name
392 f_country $tlc filename filename
394 if ! f_confirm_zone "$real_continent/$filename"; then
395 [ $nitems -eq 1 ] && NEED_CONTINENT=1
400 f_country $tlc name name
401 f_sprintf title "$msg_country_time_zones" "$name"
402 f_dialog_title "$title"
403 title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE"
404 f_dialog_title_restore
405 prompt="$msg_select_zone"
406 f_country $tlc menu_list menu_list
407 eval f_dialog_menu_size height width rows \
408 \"\$title\" \"\$btitle\" \"\$prompt\" \"\" $menu_list
411 # Launch the zone selection menu
412 # NOTE: This is as deep as we go
415 --title \"\$title\" \
416 --backtitle \"\$btitle\" \
417 --ok-label \"\$msg_ok\" \
418 --cancel-label \"\$msg_cancel\" \
419 --default-item \"\$defaultzone\" \
420 --menu \"\$prompt\" \
421 $height $width $rows \
423 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
426 f_dialog_data_sanitize n
429 if [ $retval -ne $DIALOG_OK ]; then
430 [ $nitems -eq 1 ] && NEED_CONTINENT=1
435 f_country $tlc cont_$n real_cont
436 f_continent $real_cont name real_continent
437 f_country $tlc name name
438 f_country $tlc filename_$n filename
440 f_confirm_zone "$real_continent/$filename" || continue
443 [ $retval -eq $DIALOG_OK ] || continue # back to main menu
445 if ! f_install_zoneinfo "$real_continent/$filename"; then
446 [ $nzones -lt 0 ] && NEED_COUNTRY=1
452 ################################################################################
454 ################################################################################