1if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
2#
3# Copyright (c) 2011-2015 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
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.
14#
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
25# SUCH DAMAGE.
26#
27#
28############################################################ FUNCTIONS
29
30# f_country $code $property [$var_to_set]
31#
32# Returns a single property of a given country. Available properties are:
33#
34# 	name         Name of the country as read from _PATH_ISO3166.
35# 	nzones       Number of zones within the country (-1 if country has
36# 	             only a single zone).
37# 	filename     The filename portion of the TZ field (after the `/') as
38# 	             read from _PATH_ZONETAB.
39# 	cont         The principal continent in which the country lies (appears
40# 	             before the `/' in the TZ field of _PATH_ZONETAB).
41# 	filename_N   Like filename, but for the Nth zone when the country has
42# 	             multiple zones (nzones > 0).
43# 	cont_N       Like cont, but for the Nth zone when the country has
44# 	             multiple zones (nzones > 0).
45# 	descr_N      Like name, but for the Nth zone when the country has
46# 	             multiple zones (nzones > 0)
47#
48# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
49# standard output for capturing in a sub-shell (which is less-recommended
50# because of performance degredation; for example, when called in a loop).
51#
52f_country()
53{
54	f_getvar "country_${1}_$2" $3
55}
56
57# f_sort_countries
58#
59# Sorts alphabetically the 2-character country codes listed in $COUNTRIES based
60# on the name of each country.
61#
62# This function is a two-parter. Below is the awk(1) portion of the function,
63# afterward is the sh(1) function which utilizes the below awk script.
64#
65f_sort_countries_awk='
66function _asorti(src, dest)
67{
68	k = nitems = 0
69	for (i in src) dest[++nitems] = i
70	for (i = 1; i <= nitems; k = i++) {
71		idx = dest[i]
72		while ((k > 0) && (dest[k] > idx)) {
73			dest[k+1] = dest[k]; k--
74		}
75		dest[k+1] = idx
76	}
77	return nitems
78}
79BEGIN {
80	split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/)
81	for (item in array)
82	{
83		tlc = array[item]
84		name = ENVIRON["country_" tlc "_name"]
85		countries[name] = tlc
86	}
87	n = _asorti(countries, sorted_countries)
88	for (i = 1; i <= n; i++)
89		print countries[sorted_countries[i]]
90	exit
91}
92'
93f_sort_countries()
94{
95	export COUNTRIES # for awk(1) ENVIRON[] visibility
96	COUNTRIES=$( awk "$f_sort_countries_awk" )
97	export COUNTRIES # Pedantic
98}
99
100############################################################ MAIN
101
102f_dprintf "%s: Successfully loaded." timezone/countries.subr
103
104fi # ! $_TIMEZONE_COUNTRIES_SUBR
105