1238438Sdteskeif [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
2238438Sdteske#
3278489Sdteske# Copyright (c) 2011-2015 Devin Teske
4252980Sdteske# All rights reserved.
5238438Sdteske#
6238438Sdteske# Redistribution and use in source and binary forms, with or without
7238438Sdteske# modification, are permitted provided that the following conditions
8238438Sdteske# are met:
9238438Sdteske# 1. Redistributions of source code must retain the above copyright
10238438Sdteske#    notice, this list of conditions and the following disclaimer.
11238438Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12238438Sdteske#    notice, this list of conditions and the following disclaimer in the
13238438Sdteske#    documentation and/or other materials provided with the distribution.
14238438Sdteske#
15238438Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16252987Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17238438Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18238438Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19238438Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20252987Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21238438Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22238438Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23238438Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24238438Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25238438Sdteske# SUCH DAMAGE.
26238438Sdteske#
27238438Sdteske# $FreeBSD$
28278489Sdteske#
29278489Sdteske############################################################ FUNCTIONS
30238438Sdteske
31278489Sdteske# f_country $code $property [$var_to_set]
32238438Sdteske#
33238438Sdteske# Returns a single property of a given country. Available properties are:
34238438Sdteske#
35238438Sdteske# 	name         Name of the country as read from _PATH_ISO3166.
36238438Sdteske# 	nzones       Number of zones within the country (-1 if country has
37238438Sdteske# 	             only a single zone).
38238438Sdteske# 	filename     The filename portion of the TZ field (after the `/') as
39238438Sdteske# 	             read from _PATH_ZONETAB.
40238438Sdteske# 	cont         The principal continent in which the country lies (appears
41238438Sdteske# 	             before the `/' in the TZ field of _PATH_ZONETAB).
42238438Sdteske# 	filename_N   Like filename, but for the Nth zone when the country has
43238438Sdteske# 	             multiple zones (nzones > 0).
44238438Sdteske# 	cont_N       Like cont, but for the Nth zone when the country has
45238438Sdteske# 	             multiple zones (nzones > 0).
46238438Sdteske# 	descr_N      Like name, but for the Nth zone when the country has
47238438Sdteske# 	             multiple zones (nzones > 0)
48238438Sdteske#
49278489Sdteske# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
50278489Sdteske# standard output for capturing in a sub-shell (which is less-recommended
51278489Sdteske# because of performance degredation; for example, when called in a loop).
52278489Sdteske#
53238438Sdteskef_country()
54238438Sdteske{
55278489Sdteske	f_getvar "country_${1}_$2" $3
56238438Sdteske}
57238438Sdteske
58238438Sdteske# f_sort_countries
59238438Sdteske#
60238438Sdteske# Sorts alphabetically the 2-character country codes listed in $COUNTRIES based
61238438Sdteske# on the name of each country.
62238438Sdteske#
63238438Sdteske# This function is a two-parter. Below is the awk(1) portion of the function,
64238438Sdteske# afterward is the sh(1) function which utilizes the below awk script.
65238438Sdteske#
66238438Sdteskef_sort_countries_awk='
67278489Sdteskefunction _asorti(src, dest)
68238438Sdteske{
69278489Sdteske	k = nitems = 0
70278489Sdteske	for (i in src) dest[++nitems] = i
71278489Sdteske	for (i = 1; i <= nitems; k = i++) {
72278489Sdteske		idx = dest[i]
73278489Sdteske		while ((k > 0) && (dest[k] > idx)) {
74278489Sdteske			dest[k+1] = dest[k]; k--
75278489Sdteske		}
76278489Sdteske		dest[k+1] = idx
77278489Sdteske	}
78278489Sdteske	return nitems
79278489Sdteske}
80278489SdteskeBEGIN {
81278489Sdteske	split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/)
82238438Sdteske	for (item in array)
83238438Sdteske	{
84238438Sdteske		tlc = array[item]
85278489Sdteske		name = ENVIRON["country_" tlc "_name"]
86278489Sdteske		countries[name] = tlc
87238438Sdteske	}
88278489Sdteske	n = _asorti(countries, sorted_countries)
89278489Sdteske	for (i = 1; i <= n; i++)
90278489Sdteske		print countries[sorted_countries[i]]
91278489Sdteske	exit
92238438Sdteske}
93238438Sdteske'
94238438Sdteskef_sort_countries()
95238438Sdteske{
96278489Sdteske	export COUNTRIES # for awk(1) ENVIRON[] visibility
97278489Sdteske	COUNTRIES=$( awk "$f_sort_countries_awk" )
98278489Sdteske	export COUNTRIES # Pedantic
99238438Sdteske}
100238438Sdteske
101278489Sdteske############################################################ MAIN
102278489Sdteske
103244675Sdteskef_dprintf "%s: Successfully loaded." timezone/countries.subr
104244675Sdteske
105238438Sdteskefi # ! $_TIMEZONE_COUNTRIES_SUBR
106