forked from omniosorg/kayak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog-tzselect
executable file
·346 lines (314 loc) · 7.16 KB
/
dialog-tzselect
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
#!/usr/bin/ksh
#
# Copyright 2005 Sun Microsystems, Inc. All rights reserved.
# Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
#
# Ask the user about the time zone, and place the resulting TZ in a file
# whose name is specified in the first argument.
# Contributed by Paul Eggert
. /kayak/lib/dialog.sh
output="${1:?Output}"
AWK=/usr/bin/nawk
GREP=/usr/bin/grep
EXPR=/usr/bin/expr
SORT=/usr/bin/sort
PRINTF=/usr/bin/printf
DATE=/usr/bin/date
TZDIR=/usr/share/lib/zoneinfo
# Messages
ERR_NO_SETUP="%s: time zone files are not set up correctly"
INFO_LOCATION="Please identify a location so that time zone rules \
can be set correctly."
INFO_SELECT_CONT="Please select a continent or ocean."
INFO_POSIX="none - I want to specify the time zone using the POSIX \
TZ format."
WARN_ENTER_NUM="Please enter a number in range."
INFO_ENTER_POSIX="Please enter the desired value of the TZ environment \
variable."
INFO_POSIX_EX="For example, GST-10 is a zone named GST that is 10 hours \
ahead (east) of UTC."
ERR_INV_POSIX="\`%s\' is not a conforming POSIX time zone string."
INFO_SELECT_CNTRY="Please select a country or region."
INFO_SELECT_TZ="Please select one of the following time zone regions."
INFO_EXTRA1="Local time is now: %s"
INFO_EXTRA2="Universal Time is now: %s"
INFO_INFO="The following information has been given:"
INFO_TZ="Therefore TZ='%s' will be used."
INFO_OK="Is the above information OK?"
# Make sure the tables are readable.
TZ_CONTINENT_TABLE=$TZDIR/tab/continent.tab
TZ_COUNTRY_TABLE=$TZDIR/tab/country.tab
TZ_ZONE_TABLE=$TZDIR/tab/zone_sun.tab
for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE; do
<$f || {
$PRINTF >&2 "$ERR_NO_SETUP\n" $0
exit 1
}
done
newline='
'
IFS=$newline
function get_continent {
dialog \
--colors \
--title "$INFO_LOCATION" \
--no-ok \
--no-tags \
--no-lines \
--nocancel \
--default-item UTC \
--menu "\n$INFO_SELECT_CONT\n\Zn" \
20 0 12 \
Africa Africa \
America Americas \
Antarctica Antarctica \
Arctic "Arctic Ocean" \
Asia Asia \
Atlantic "Atlantic Ocean" \
Australia Australia \
Europe Europe \
Pacific "Pacific Ocean" \
Indian "Indian Ocean" \
UTC UTC \
none "$INFO_POSIX" \
2> $tmpf
stat=$?
[ $stat -eq 0 ] || return 1
continent="`cat $tmpf`"
rm -f $tmpf
return 0
}
function get_posix_tz {
# Ask the user for a POSIX TZ string. Check that it conforms.
dialog \
--title "$INFO_ENTER_POSIX" \
--colors \
--inputbox "\n$INFO_POSIX_EX\n\Zn" \
12 69 "" 2> $tmpf
[ $? -ne 0 ] && continue 2
TZ="`cat $tmpf`"
rm -f $tmpf
LC_ALL=C $AWK -v TZ="$TZ" 'BEGIN {
tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+"
time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?"
offset = "[-+]?" time
date = "(J?[0-9]+|M[0-9]+\.[0-9]+\.[0-9]+)"
datetime = "," date "(/" time ")?"
tzpattern = "^(:.*|" tzname offset "(" tzname \
"(" offset ")?(" datetime datetime ")?)?)$"
if (TZ ~ tzpattern) exit 1
exit 0
}'
if [ $? -ne 0 ]; then
d_msg $($PRINTF "$ERR_INV_POSIX" "$TZ")
return 1
fi
TZ_for_date=$TZ
return 0
}
function get_country {
# Get list of names of countries in the continent or ocean.
countries=$($AWK -F'\t' \
-v continent="$continent" \
-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
'
/^#/ { next }
$3 ~ ("^" continent "/") {
if (!cc_seen[$1]++) cc_list[++ccs] = $1
}
END {
while (getline <TZ_COUNTRY_TABLE) {
if ($0 !~ /^#/) cc_name[$1] = $2
}
for (i = 1; i <= ccs; i++) {
country = cc_list[i]
if (cc_name[country]) {
country = cc_name[country]
}
print country
}
}
' < $TZ_ZONE_TABLE | $SORT -f)
# i18n country names
c=0
set -A icountry
unset args; set -a args
for country in $countries; do
icountry[c]=$country
ocountry[c]="$country"
args+=("$country" "${icountry[$c]}")
((c = c + 1))
done
if [[ $countries == *"$newline"* ]]; then
# If there's more than one country, ask the user which one.
dialog \
--colors \
--title "$INFO_SELECT_CNTRY" \
--no-ok \
--no-tags \
--nocancel \
--menu "" \
19 0 11 \
${args[@]} 2> $tmpf
[ $? -ne 0 ] && return 1
xcountry="`cat $tmpf`"
country="$xcountry"
rm -f $tmpf
else
country=$countries
xcountry=$countries
fi
return 0
}
function get_region {
# Get list of names of time zone rule regions in the country.
regions=$($AWK -F'\t' \
-v country="$country" \
-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
'
BEGIN {
cc = country
while (getline <TZ_COUNTRY_TABLE) {
if ($0 !~ /^#/ && country == $2) {
cc = $1
break
}
}
}
$1 == cc {
if (NF > 4)
print $5
else
print $3
}
' < $TZ_ZONE_TABLE)
# I18n region names
c=0
set -A iregion
unset args; set -a args
for region in $regions; do
iregion[c]=$region
oregion[c]="$region"
args+=("$region" "${iregion[$c]}")
((c = c + 1))
done
if [[ $regions == *"$newline"* ]]; then
# If there's one or more region, ask the user which one.
dialog \
--colors \
--title "$INFO_SELECT_TZ" \
--no-ok \
--no-tags \
--nocancel \
--menu "" \
19 0 11 \
${args[@]} 2> $tmpf
[ $? -ne 0 ] && return 1
xregion="`cat $tmpf`"
region="$xregion"
rm -f $tmpf
else
region=$regions
xregion=$regions
fi
return 0
}
function build_tz {
# Determine TZ from country and region.
TZ=$($AWK -F'\t' \
-v country="$country" \
-v region="$region" \
-v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
'
BEGIN {
cc = country
while (getline <TZ_COUNTRY_TABLE) {
if ($0 !~ /^#/ && country == $2) {
cc = $1
break
}
}
}
$1 == cc && ((NF > 4 && $5 == region) || $3 == region) {
# Check if tzname mapped to
# backward compatible tzname
if ($4 == "-")
print $3
else
print $4
}
' < $TZ_ZONE_TABLE)
# Make sure the corresponding zoneinfo file exists.
TZ_for_date=$TZDIR/$TZ
<$TZ_for_date || return 1
# Absolute path TZ's not supported
TZ_for_date=$TZ
return 0
}
function check_tz {
# Use the proposed TZ to output the current date relative to UTC.
# Loop until they agree in seconds.
# Give up after 8 unsuccessful tries.
extra_info1=
extra_info2=
for i in 1 2 3 4 5 6 7 8; do
TZdate=$(LANG=C TZ="$TZ_for_date" $DATE)
UTdate=$(LANG=C TZ=UTC0 $DATE)
TZsec=$($EXPR "$TZdate" : '.*:\([0-5][0-9]\)')
UTsec=$($EXPR "$UTdate" : '.*:\([0-5][0-9]\)')
case $TZsec in
$UTsec)
extra_info1=$($PRINTF "$INFO_EXTRA1" "$TZdate")
extra_info2=$($PRINTF "$INFO_EXTRA2" "$UTdate")
break
;;
esac
done
case $country+$region in
?*+?*) msg="$xcountry\n $xregion\n" ;;
?*+) msg=" $xcountry\n" ;;
+) msg=" TZ='$TZ'\n"
esac
tzmsg=$($PRINTF "$INFO_TZ" $TZ)
dialog \
--colors \
--title "$INFO_INFO" \
--yesno "\n$msg\n
$tzmsg\n
\n
$extra_info1\n
$extra_info2\n
\n
$INFO_OK\n\Zn
" 0 0
[ $? -ne 0 ] && return 1
return 0
}
# Begin the main loop. We come back here if the user wants to retry.
while :; do
continent=
country=
region=
tmpf=`mktemp`
get_continent || continue
case $continent in
none)
get_posix_tz || continue
;;
UTC)
TZ=UTC
TZ_for_date=UTC
;;
*)
get_country || continue
get_region || continue
if ! build_tz; then
d_msg $($PRINTF "$ERR_NO_SETUP\n" $0$)
continue
fi
;;
esac
check_tz || continue
break
done
$PRINTF "%s\n" "$TZ" > $output