1238438Sdteskeif [ ! "$_TIMEZONE_ISO3166_SUBR" ]; then _TIMEZONE_ISO3166_SUBR=1
2238438Sdteske#
3238438Sdteske# Copyright (c) 2011-2012 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/11.0/usr.sbin/bsdconfig/timezone/share/iso3166.subr 252987 2013-07-07 18:51:44Z dteske $
28238438Sdteske#
29238438Sdteske############################################################ INCLUDES
30238438Sdteske
31240684SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
32240684Sdteske. $BSDCFG_SHARE/common.subr || exit 1
33244675Sdteskef_dprintf "%s: loading includes..." timezone/iso3166.subr
34238438Sdteske
35240684SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
36238438Sdteskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
37238438Sdteske
38238438Sdteske############################################################ CONFIGURATION
39238438Sdteske
40238438Sdteske#
41238438Sdteske# Standard pathnames
42238438Sdteske#
43238438Sdteske_PATH_ISO3166="/usr/share/misc/iso3166"
44238438Sdteske
45238438Sdteske#
46238438Sdteske# Export required i18n messages for awk(1) ENVIRON visibility
47238438Sdteske#
48238438Sdteskeexport msg_country_code_multiply_defined
49238438Sdteskeexport msg_invalid_code
50238438Sdteskeexport msg_invalid_format
51238438Sdteske
52238438Sdteske############################################################ FUNCTIONS
53238438Sdteske
54238438Sdteske# f_read_iso3166_table
55238438Sdteske#
56238438Sdteske# Read the ISO 3166 country code database in _PATH_ISO3166:
57238438Sdteske# 	/usr/share/misc/iso3166 on FreeBSD
58238438Sdteske# 	/usr/share/zoneinfo/iso3166.tab on Linux, Mac OS X, and Cygwin
59238438Sdteske#
60238438Sdteske# The format of this file on FreeBSD is:
61238438Sdteske# 	two	three	number	name
62238438Sdteske#
63238438Sdteske# The format of this file on Linux, Mac OS X, and Cygwin is:
64238438Sdteske# 	two	name
65238438Sdteske#
66238438Sdteske# With each of the following elements (described below) being separated by a
67238438Sdteske# single tab character:
68238438Sdteske#
69238438Sdteske# 	two      ISO 3166 2-character country code
70238438Sdteske# 	three    ISO 3166 3-character country code (if provided)
71238438Sdteske# 	number   ISO 3166 numeric country code (if provided)
72238438Sdteske# 	name     Human-readable country name (may contain spaces)
73238438Sdteske#
74238438Sdteske# Variables created by this function:
75238438Sdteske#
76238438Sdteske# 	COUNTRIES
77238438Sdteske# 		A space-separated list of 2-character country codes.
78238438Sdteske# 	country_CODE_name
79238438Sdteske# 		The country `name' (as described above).
80238438Sdteske#
81238438Sdteske# where CODE is the 2-character country code.
82238438Sdteske#
83238438Sdteske# This function is a two-parter. Below is the awk(1) portion of the function,
84238438Sdteske# afterward is the sh(1) function which utilizes the below awk script.
85238438Sdteske#
86238438Sdteskef_read_iso3166_table_awk='
87238438Sdteske# Variables that should be defined on the invocation line:
88238438Sdteske# 	-v progname="progname"
89238438Sdteske#
90238438SdteskeBEGIN {
91238438Sdteske	lineno = 0
92238438Sdteske	failed = 0
93238438Sdteske}
94238438Sdteskefunction die(fmt, argc, argv)
95238438Sdteske{
96238438Sdteske	printf "f_die 1 \"%%s: %s\" \"%s\"", fmt, progname
97238438Sdteske	for (n = 1; n <= argc; n++)
98238438Sdteske		printf " \"%s\"", argv[n]
99238438Sdteske	print ""
100238438Sdteske	failed++
101238438Sdteske	exit 1
102238438Sdteske}
103238438Sdteskefunction add_country(tlc, name)
104238438Sdteske{
105238438Sdteske	if (country_name[tlc])
106238438Sdteske	{
107238438Sdteske		argv[1] = FILENAME
108238438Sdteske		argv[2] = lineno
109238438Sdteske		argv[3] = tlc
110238438Sdteske		argv[4] = name
111238438Sdteske		die(ENVIRON["msg_country_code_multiply_defined"], 4, argv)
112238438Sdteske	}
113238438Sdteske
114238438Sdteske	country_name[tlc] = name
115238438Sdteske}
116238438Sdteskefunction print_country_name(tlc)
117238438Sdteske{
118238438Sdteske	name = country_name[tlc]
119238438Sdteske	gsub(/"/, "\\\"", name)
120238438Sdteske	printf "country_%s_name=\"%s\"\n", tlc, name
121238438Sdteske	printf "export country_%s_name\n", tlc
122238438Sdteske}
123238438Sdteske/^#/ {
124238438Sdteske	lineno++
125238438Sdteske	next
126238438Sdteske}
127238438Sdteske!/^#/ {
128238438Sdteske	lineno++
129238438Sdteske
130238438Sdteske	# Split the current record (on TAB) into an array
131238438Sdteske	split($0, line, /\t/)
132238438Sdteske
133238438Sdteske	# Get the ISO3166-1 (Alpha 1) 2-letter country code
134238438Sdteske	tlc = line[1]
135238438Sdteske
136238438Sdteske	#
137238438Sdteske	# Validate the two-character country code
138238438Sdteske	#
139238438Sdteske	if (length(tlc) != 2)
140238438Sdteske	{
141238438Sdteske		argv[1] = FILENAME
142238438Sdteske		argv[2] = lineno
143238438Sdteske		die(ENVIRON["msg_invalid_format"], 2, argv)
144238438Sdteske	}
145238438Sdteske	if (!match(tlc, /^[A-Z][A-Z]$/))
146238438Sdteske	{
147238438Sdteske		argv[1] = FILENAME
148238438Sdteske		argv[2] = lineno
149238438Sdteske		argv[3] = tlc
150238438Sdteske		die(ENVIRON["msg_invalid_code"], 3, argv)
151238438Sdteske	}
152238438Sdteske
153238438Sdteske	#
154238438Sdteske	# Calculate the substr start-position of the name
155238438Sdteske	#
156238438Sdteske	name_start = 0
157238438Sdteske	n = 4
158238438Sdteske	if (FILENAME ~ /\.tab$/)
159238438Sdteske		n = 2
160238438Sdteske	while (--n)
161238438Sdteske	{
162238438Sdteske		#
163238438Sdteske		# Validate field-length of 2nd/3rd columns while we are here
164238438Sdteske		#
165238438Sdteske		if (n > 1  && length(line[n]) != 3)
166238438Sdteske		{
167238438Sdteske			argv[1] = FILENAME
168238438Sdteske			argv[2] = lineno
169238438Sdteske			die(ENVIRON["msg_invalid_format"], 2, argv)
170238438Sdteske		}
171238438Sdteske
172238438Sdteske		name_start += length(line[n]) + 1
173238438Sdteske	}
174238438Sdteske
175238438Sdteske	# Get the name field
176238438Sdteske	name = substr($0, name_start + 1)
177238438Sdteske
178238438Sdteske	add_country(tlc, name)
179238438Sdteske}
180238438SdteskeEND {
181238438Sdteske	list = ""
182238438Sdteske	for (tlc in country_name)
183238438Sdteske	{
184238438Sdteske		list = list (length(list) > 0 ? " " : "") tlc
185238438Sdteske		print_country_name(tlc)
186238438Sdteske	}
187238438Sdteske	printf "COUNTRIES=\"%s\"\n", list
188238438Sdteske	print "export COUNTRIES"
189238438Sdteske}
190238438Sdteske'
191238438Sdteskef_read_iso3166_table()
192238438Sdteske{
193238438Sdteske	eval $( awk -v progname="$pgm"          \
194238438Sdteske	            "$f_read_iso3166_table_awk" \
195238438Sdteske	            "$_PATH_ISO3166"            )
196238438Sdteske}
197238438Sdteske
198244675Sdteske############################################################ MAIN
199244675Sdteske
200244675Sdteskef_dprintf "%s: Successfully loaded." timezone/iso3166.subr
201244675Sdteske
202238438Sdteskefi # ! $_TIMEZONE_ISO3166_SUBR
203