1238438Sdteskeif [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1 2238438Sdteske# 3279616Sdteske# 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: releng/10.3/usr.sbin/bsdconfig/timezone/share/countries.subr 279616 2015-03-05 00:42:06Z dteske $ 28279616Sdteske# 29279616Sdteske############################################################ FUNCTIONS 30238438Sdteske 31279616Sdteske# 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# 49279616Sdteske# If $var_to_set is missing or NULL, the value of $var_to_get is printed to 50279616Sdteske# standard output for capturing in a sub-shell (which is less-recommended 51279616Sdteske# because of performance degredation; for example, when called in a loop). 52279616Sdteske# 53238438Sdteskef_country() 54238438Sdteske{ 55279616Sdteske 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=' 67279616Sdteskefunction _asorti(src, dest) 68238438Sdteske{ 69279616Sdteske k = nitems = 0 70279616Sdteske for (i in src) dest[++nitems] = i 71279616Sdteske for (i = 1; i <= nitems; k = i++) { 72279616Sdteske idx = dest[i] 73279616Sdteske while ((k > 0) && (dest[k] > idx)) { 74279616Sdteske dest[k+1] = dest[k]; k-- 75279616Sdteske } 76279616Sdteske dest[k+1] = idx 77279616Sdteske } 78279616Sdteske return nitems 79279616Sdteske} 80279616SdteskeBEGIN { 81279616Sdteske split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/) 82238438Sdteske for (item in array) 83238438Sdteske { 84238438Sdteske tlc = array[item] 85279616Sdteske name = ENVIRON["country_" tlc "_name"] 86279616Sdteske countries[name] = tlc 87238438Sdteske } 88279616Sdteske n = _asorti(countries, sorted_countries) 89279616Sdteske for (i = 1; i <= n; i++) 90279616Sdteske print countries[sorted_countries[i]] 91279616Sdteske exit 92238438Sdteske} 93238438Sdteske' 94238438Sdteskef_sort_countries() 95238438Sdteske{ 96279616Sdteske export COUNTRIES # for awk(1) ENVIRON[] visibility 97279616Sdteske COUNTRIES=$( awk "$f_sort_countries_awk" ) 98279616Sdteske export COUNTRIES # Pedantic 99238438Sdteske} 100238438Sdteske 101279616Sdteske############################################################ MAIN 102279616Sdteske 103244675Sdteskef_dprintf "%s: Successfully loaded." timezone/countries.subr 104244675Sdteske 105238438Sdteskefi # ! $_TIMEZONE_COUNTRIES_SUBR 106