167760Smsmithif [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
269459Smsmith#
367760Smsmith# Copyright (c) 2011-2015 Devin Teske
467760Smsmith# All rights reserved.
567760Smsmith#
667760Smsmith# Redistribution and use in source and binary forms, with or without
767760Smsmith# modification, are permitted provided that the following conditions
867760Smsmith# are met:
967760Smsmith# 1. Redistributions of source code must retain the above copyright
1067760Smsmith#    notice, this list of conditions and the following disclaimer.
1167760Smsmith# 2. Redistributions in binary form must reproduce the above copyright
1267760Smsmith#    notice, this list of conditions and the following disclaimer in the
1367760Smsmith#    documentation and/or other materials provided with the distribution.
1467760Smsmith#
1567760Smsmith# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1667760Smsmith# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1767760Smsmith# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1867760Smsmith# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1967760Smsmith# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2067760Smsmith# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2167760Smsmith# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2267760Smsmith# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2367760Smsmith# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2467760Smsmith# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2567760Smsmith# SUCH DAMAGE.
2667760Smsmith#
2767760Smsmith# $FreeBSD: releng/11.0/usr.sbin/bsdconfig/timezone/share/countries.subr 278489 2015-02-10 02:55:10Z dteske $
2867760Smsmith#
2967760Smsmith############################################################ FUNCTIONS
3067760Smsmith
3167760Smsmith# f_country $code $property [$var_to_set]
3267760Smsmith#
33148318Snjl# Returns a single property of a given country. Available properties are:
34148318Snjl#
35148318Snjl# 	name         Name of the country as read from _PATH_ISO3166.
36150003Sobrien# 	nzones       Number of zones within the country (-1 if country has
3767760Smsmith# 	             only a single zone).
3867760Smsmith# 	filename     The filename portion of the TZ field (after the `/') as
3967760Smsmith# 	             read from _PATH_ZONETAB.
4067760Smsmith# 	cont         The principal continent in which the country lies (appears
4167760Smsmith# 	             before the `/' in the TZ field of _PATH_ZONETAB).
4267760Smsmith# 	filename_N   Like filename, but for the Nth zone when the country has
43128225Snjl# 	             multiple zones (nzones > 0).
4467760Smsmith# 	cont_N       Like cont, but for the Nth zone when the country has
4567760Smsmith# 	             multiple zones (nzones > 0).
4692118Speter# 	descr_N      Like name, but for the Nth zone when the country has
4767760Smsmith# 	             multiple zones (nzones > 0)
48128225Snjl#
4967760Smsmith# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
5067760Smsmith# standard output for capturing in a sub-shell (which is less-recommended
5167760Smsmith# because of performance degredation; for example, when called in a loop).
52128225Snjl#
5367760Smsmithf_country()
5467760Smsmith{
5567760Smsmith	f_getvar "country_${1}_$2" $3
5667760Smsmith}
5767760Smsmith
58128225Snjl# f_sort_countries
59128225Snjl#
6067760Smsmith# Sorts alphabetically the 2-character country codes listed in $COUNTRIES based
6167760Smsmith# on the name of each country.
6267760Smsmith#
63128225Snjl# This function is a two-parter. Below is the awk(1) portion of the function,
64128225Snjl# afterward is the sh(1) function which utilizes the below awk script.
6567760Smsmith#
6667760Smsmithf_sort_countries_awk='
6767760Smsmithfunction _asorti(src, dest)
68128225Snjl{
6967760Smsmith	k = nitems = 0
7067760Smsmith	for (i in src) dest[++nitems] = i
7167760Smsmith	for (i = 1; i <= nitems; k = i++) {
7267760Smsmith		idx = dest[i]
7380071Smsmith		while ((k > 0) && (dest[k] > idx)) {
74128225Snjl			dest[k+1] = dest[k]; k--
75128225Snjl		}
7680071Smsmith		dest[k+1] = idx
77128225Snjl	}
78128225Snjl	return nitems
7980071Smsmith}
8080071SmsmithBEGIN {
8167760Smsmith	split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/)
8267760Smsmith	for (item in array)
8367760Smsmith	{
8467760Smsmith		tlc = array[item]
8567760Smsmith		name = ENVIRON["country_" tlc "_name"]
86117534Smarcel		countries[name] = tlc
8767760Smsmith	}
88128225Snjl	n = _asorti(countries, sorted_countries)
8967760Smsmith	for (i = 1; i <= n; i++)
9067760Smsmith		print countries[sorted_countries[i]]
9167760Smsmith	exit
92117534Smarcel}
9367760Smsmith'
94128225Snjlf_sort_countries()
9567760Smsmith{
9667760Smsmith	export COUNTRIES # for awk(1) ENVIRON[] visibility
9780071Smsmith	COUNTRIES=$( awk "$f_sort_countries_awk" )
98128225Snjl	export COUNTRIES # Pedantic
9969459Smsmith}
10069459Smsmith
10169459Smsmith############################################################ MAIN
102128225Snjl
103128225Snjlf_dprintf "%s: Successfully loaded." timezone/countries.subr
10469459Smsmith
10580071Smsmithfi # ! $_TIMEZONE_COUNTRIES_SUBR
10680071Smsmith