#!/bin/bash
# Ask the user about the time zone, and output the resulting TZ value to stdout.
# Interact with the user via stderr and stdin.
PKGVERSION='(tzcode) '
TZVERSION=see_Makefile
REPORT_BUGS_TO=tz@iana.org
# Contributed by Paul Eggert. This file is in the public domain.
# Porting notes:
#
# This script requires a POSIX-like shell and prefers the extension of a
# 'select' statement. The 'select' statement was introduced in the
# Korn shell and is available in Bash and other shell implementations.
# If your host lacks both Bash and the Korn shell, you can get their
# source from one of these locations:
#
# Bash <https://www.gnu.org/software/bash/>
# Korn Shell <http://www.kornshell.com/>
# MirBSD Korn Shell <http://www.mirbsd.org/mksh.htm>
#
# This script also uses several features of POSIX awk.
# If your host lacks awk, or has an old awk that does not conform to POSIX,
# you can use any of the following free programs instead:
#
# Gawk (GNU awk) <https://www.gnu.org/software/gawk/>
# mawk <https://invisible-island.net/mawk/>
# nawk <https://github.com/onetrueawk/awk>
#
# Because 'awk "VAR=VALUE" ...' and 'awk -v "VAR=VALUE" ...' are not portable
# if VALUE contains \, ", or newline, awk scripts in this file use:
# awk 'BEGIN { VAR = substr(ARGV[1], 2); ARGV[1] = "" } ...' ="VALUE"
# The substr avoids problems when VALUE is of the form X=Y and would be
# misinterpreted as an assignment.
# This script does not want path expansion.
set -f
# Specify default values for environment variables if they are unset.
: ${AWK=awk}
: ${TZDIR=$PWD}
# Output one argument as-is to standard output, with trailing newline.
# Safer than 'echo', which can mishandle '\' or leading '-'.
say() {
printf '%s\n' "$1"
}
coord=
location_limit=10
zonetabtype=zone1970
usage="Usage: tzselect [--version] [--help] [-c COORD] [-n LIMIT]
Select a timezone interactively.
Options:
-c COORD
Instead of asking for continent and then country and then city,
ask for selection from time zones whose largest cities
are closest to the location with geographical coordinates COORD.
COORD should use ISO 6709 notation, for example, '-c +4852+00220'
for Paris (in degrees and minutes, North and East), or
'-c -35-058' for Buenos Aires (in degrees, South and West).
-n LIMIT
Display at most LIMIT locations when -c is used (default $location_limit).
--version
Output version information.
--help
Output this help.
Report bugs to $REPORT_BUGS_TO."
# Ask the user to select from the function's arguments,
# and assign the selected argument to the variable 'select_result'.
# Exit on EOF or I/O error. Use the shell's nicer 'select' builtin if
# available, falling back on a portable substitute otherwise.
if
case $BASH_VERSION in
?*) :;;
'')
# '; exit' should be redundant, but Dash doesn't properly fail without it.
(eval 'set --; select x; do break; done; exit') <>/dev/null 2>&0
esac
then
# Do this inside 'eval', as otherwise the shell might exit when parsing it
# even though it is never executed.
eval '
doselect() {
select select_result
do
case $select_result in
"") echo >&2 "Please enter a number in range.";;
?*) break
esac
done || exit
}
'
else
doselect() {
# Field width of the prompt numbers.
select_width=${##}
select_i=
while :
do
case $select_i in
'')
select_i=0
for select_word
do
select_i=$(($select_i + 1))
printf >&2 "%${select_width}d) %s\\n" $select_i "$select_word"
done;;
*[!0-9]*)
echo >&2 'Please enter a number in range.';;
*)
if test 1 -le $select_i && test $select_i -le $#; then
shift $(($select_i - 1))
select_result=$1
break
fi
echo >&2 'Please enter a number in range.'
esac