xref: /freebsd/usr.sbin/bsdconfig/timezone/share/countries.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1ab2043b8SDevin Teskeif [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
2ab2043b8SDevin Teske#
3*fb1f1beeSDevin Teske# Copyright (c) 2011-2015 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
5ab2043b8SDevin Teske#
6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without
7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions
8ab2043b8SDevin Teske# are met:
9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright
10ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer.
11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer in the
13ab2043b8SDevin Teske#    documentation and/or other materials provided with the distribution.
14ab2043b8SDevin Teske#
15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab2043b8SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab2043b8SDevin Teske# SUCH DAMAGE.
26ab2043b8SDevin Teske#
27*fb1f1beeSDevin Teske#
28*fb1f1beeSDevin Teske############################################################ FUNCTIONS
29ab2043b8SDevin Teske
30*fb1f1beeSDevin Teske# f_country $code $property [$var_to_set]
31ab2043b8SDevin Teske#
32ab2043b8SDevin Teske# Returns a single property of a given country. Available properties are:
33ab2043b8SDevin Teske#
34ab2043b8SDevin Teske# 	name         Name of the country as read from _PATH_ISO3166.
35ab2043b8SDevin Teske# 	nzones       Number of zones within the country (-1 if country has
36ab2043b8SDevin Teske# 	             only a single zone).
37ab2043b8SDevin Teske# 	filename     The filename portion of the TZ field (after the `/') as
38ab2043b8SDevin Teske# 	             read from _PATH_ZONETAB.
39ab2043b8SDevin Teske# 	cont         The principal continent in which the country lies (appears
40ab2043b8SDevin Teske# 	             before the `/' in the TZ field of _PATH_ZONETAB).
41ab2043b8SDevin Teske# 	filename_N   Like filename, but for the Nth zone when the country has
42ab2043b8SDevin Teske# 	             multiple zones (nzones > 0).
43ab2043b8SDevin Teske# 	cont_N       Like cont, but for the Nth zone when the country has
44ab2043b8SDevin Teske# 	             multiple zones (nzones > 0).
45ab2043b8SDevin Teske# 	descr_N      Like name, but for the Nth zone when the country has
46ab2043b8SDevin Teske# 	             multiple zones (nzones > 0)
47ab2043b8SDevin Teske#
48*fb1f1beeSDevin Teske# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
49*fb1f1beeSDevin Teske# standard output for capturing in a sub-shell (which is less-recommended
50*fb1f1beeSDevin Teske# because of performance degredation; for example, when called in a loop).
51*fb1f1beeSDevin Teske#
52ab2043b8SDevin Teskef_country()
53ab2043b8SDevin Teske{
54*fb1f1beeSDevin Teske	f_getvar "country_${1}_$2" $3
55ab2043b8SDevin Teske}
56ab2043b8SDevin Teske
57ab2043b8SDevin Teske# f_sort_countries
58ab2043b8SDevin Teske#
59ab2043b8SDevin Teske# Sorts alphabetically the 2-character country codes listed in $COUNTRIES based
60ab2043b8SDevin Teske# on the name of each country.
61ab2043b8SDevin Teske#
62ab2043b8SDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
63ab2043b8SDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
64ab2043b8SDevin Teske#
65ab2043b8SDevin Teskef_sort_countries_awk='
66*fb1f1beeSDevin Teskefunction _asorti(src, dest)
67ab2043b8SDevin Teske{
68*fb1f1beeSDevin Teske	k = nitems = 0
69*fb1f1beeSDevin Teske	for (i in src) dest[++nitems] = i
70*fb1f1beeSDevin Teske	for (i = 1; i <= nitems; k = i++) {
71*fb1f1beeSDevin Teske		idx = dest[i]
72*fb1f1beeSDevin Teske		while ((k > 0) && (dest[k] > idx)) {
73*fb1f1beeSDevin Teske			dest[k+1] = dest[k]; k--
74*fb1f1beeSDevin Teske		}
75*fb1f1beeSDevin Teske		dest[k+1] = idx
76*fb1f1beeSDevin Teske	}
77*fb1f1beeSDevin Teske	return nitems
78*fb1f1beeSDevin Teske}
79*fb1f1beeSDevin TeskeBEGIN {
80*fb1f1beeSDevin Teske	split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/)
81ab2043b8SDevin Teske	for (item in array)
82ab2043b8SDevin Teske	{
83ab2043b8SDevin Teske		tlc = array[item]
84*fb1f1beeSDevin Teske		name = ENVIRON["country_" tlc "_name"]
85*fb1f1beeSDevin Teske		countries[name] = tlc
86ab2043b8SDevin Teske	}
87*fb1f1beeSDevin Teske	n = _asorti(countries, sorted_countries)
88*fb1f1beeSDevin Teske	for (i = 1; i <= n; i++)
89*fb1f1beeSDevin Teske		print countries[sorted_countries[i]]
90*fb1f1beeSDevin Teske	exit
91ab2043b8SDevin Teske}
92ab2043b8SDevin Teske'
93ab2043b8SDevin Teskef_sort_countries()
94ab2043b8SDevin Teske{
95*fb1f1beeSDevin Teske	export COUNTRIES # for awk(1) ENVIRON[] visibility
96*fb1f1beeSDevin Teske	COUNTRIES=$( awk "$f_sort_countries_awk" )
97*fb1f1beeSDevin Teske	export COUNTRIES # Pedantic
98ab2043b8SDevin Teske}
99ab2043b8SDevin Teske
100*fb1f1beeSDevin Teske############################################################ MAIN
101*fb1f1beeSDevin Teske
10256961fd7SDevin Teskef_dprintf "%s: Successfully loaded." timezone/countries.subr
10356961fd7SDevin Teske
104ab2043b8SDevin Teskefi # ! $_TIMEZONE_COUNTRIES_SUBR
105