1180767Simpif [ ! "$_TIMEZONE_ISO3166_SUBR" ]; then _TIMEZONE_ISO3166_SUBR=1
2180767Simp#
3180767Simp# Copyright (c) 2011-2012 Devin Teske
4180767Simp# All rights reserved.
5180767Simp#
6180767Simp# Redistribution and use in source and binary forms, with or without
7180767Simp# modification, are permitted provided that the following conditions
8180767Simp# are met:
9180767Simp# 1. Redistributions of source code must retain the above copyright
10180767Simp#    notice, this list of conditions and the following disclaimer.
11180767Simp# 2. Redistributions in binary form must reproduce the above copyright
12194207Ssimon#    notice, this list of conditions and the following disclaimer in the
13194207Ssimon#    documentation and/or other materials provided with the distribution.
14194207Ssimon#
15194207Ssimon# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16194207Ssimon# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17194207Ssimon# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18194207Ssimon# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19194207Ssimon# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20194207Ssimon# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21194207Ssimon# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22194207Ssimon# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23194207Ssimon# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24180767Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25180767Simp# SUCH DAMAGE.
26180767Simp#
27180767Simp# $FreeBSD$
28180767Simp#
29180767Simp############################################################ INCLUDES
30180767Simp
31180767SimpBSDCFG_SHARE="/usr/share/bsdconfig"
32180767Simp. $BSDCFG_SHARE/common.subr || exit 1
33180767Simpf_dprintf "%s: loading includes..." timezone/iso3166.subr
34180767Simp
35180767SimpBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="090.timezone"
36180767Simpf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
37180767Simp
38180767Simp############################################################ CONFIGURATION
39180767Simp
40180767Simp#
41180767Simp# Standard pathnames
42180767Simp#
43180767Simp_PATH_ISO3166="/usr/share/misc/iso3166"
44180767Simp
45180767Simp#
46180767Simp# Export required i18n messages for awk(1) ENVIRON visibility
47180767Simp#
48180767Simpexport msg_country_code_multiply_defined
49180767Simpexport msg_invalid_code
50180767Simpexport msg_invalid_format
51180767Simp
52180767Simp############################################################ FUNCTIONS
53180767Simp
54180767Simp# f_read_iso3166_table
55180767Simp#
56180767Simp# Read the ISO 3166 country code database in _PATH_ISO3166:
57180767Simp# 	/usr/share/misc/iso3166 on FreeBSD
58180767Simp# 	/usr/share/zoneinfo/iso3166.tab on Linux, Mac OS X, and Cygwin
59180767Simp#
60180767Simp# The format of this file on FreeBSD is:
61180767Simp# 	two	three	number	name
62180767Simp#
63180767Simp# The format of this file on Linux, Mac OS X, and Cygwin is:
64180767Simp# 	two	name
65180767Simp#
66180767Simp# With each of the following elements (described below) being separated by a
67180767Simp# single tab character:
68180767Simp#
69180767Simp# 	two      ISO 3166 2-character country code
70180767Simp# 	three    ISO 3166 3-character country code (if provided)
71180767Simp# 	number   ISO 3166 numeric country code (if provided)
72180767Simp# 	name     Human-readable country name (may contain spaces)
73180767Simp#
74180767Simp# Variables created by this function:
75180767Simp#
76180767Simp# 	COUNTRIES
77180767Simp# 		A space-separated list of 2-character country codes.
78180767Simp# 	country_CODE_name
79180767Simp# 		The country `name' (as described above).
80180767Simp#
81180767Simp# where CODE is the 2-character country code.
82180767Simp#
83180767Simp# This function is a two-parter. Below is the awk(1) portion of the function,
84180767Simp# afterward is the sh(1) function which utilizes the below awk script.
85180767Simp#
86180767Simpf_read_iso3166_table_awk='
87180767Simp# Variables that should be defined on the invocation line:
88180767Simp# 	-v progname="progname"
89180767Simp#
90180767SimpBEGIN {
91180767Simp	lineno = 0
92180767Simp	failed = 0
93180767Simp}
94180767Simpfunction die(fmt, argc, argv)
95180767Simp{
96180767Simp	printf "f_die 1 \"%%s: %s\" \"%s\"", fmt, progname
97180767Simp	for (n = 1; n <= argc; n++)
98180767Simp		printf " \"%s\"", argv[n]
99180767Simp	print ""
100180767Simp	failed++
101180767Simp	exit 1
102180767Simp}
103180767Simpfunction add_country(tlc, name)
104180767Simp{
105180767Simp	if (country_name[tlc])
106180767Simp	{
107180767Simp		argv[1] = FILENAME
108180767Simp		argv[2] = lineno
109180767Simp		argv[3] = tlc
110180767Simp		argv[4] = name
111180767Simp		die(ENVIRON["msg_country_code_multiply_defined"], 4, argv)
112180767Simp	}
113180767Simp
114180767Simp	country_name[tlc] = name
115180767Simp}
116180767Simpfunction print_country_name(tlc)
117180767Simp{
118180767Simp	name = country_name[tlc]
119180767Simp	gsub(/"/, "\\\"", name)
120180767Simp	printf "country_%s_name=\"%s\"\n", tlc, name
121180767Simp	printf "export country_%s_name\n", tlc
122180767Simp}
123180767Simp/^#/ {
124180767Simp	lineno++
125180767Simp	next
126180767Simp}
127180767Simp!/^#/ {
128180767Simp	lineno++
129180767Simp
130180767Simp	# Split the current record (on TAB) into an array
131180767Simp	split($0, line, /\t/)
132180767Simp
133180767Simp	# Get the ISO3166-1 (Alpha 1) 2-letter country code
134180767Simp	tlc = line[1]
135180767Simp
136180767Simp	#
137180767Simp	# Validate the two-character country code
138180767Simp	#
139180767Simp	if (length(tlc) != 2)
140180767Simp	{
141180767Simp		argv[1] = FILENAME
142180767Simp		argv[2] = lineno
143180767Simp		die(ENVIRON["msg_invalid_format"], 2, argv)
144180767Simp	}
145180767Simp	if (!match(tlc, /^[A-Z][A-Z]$/))
146180767Simp	{
147180767Simp		argv[1] = FILENAME
148180767Simp		argv[2] = lineno
149180767Simp		argv[3] = tlc
150180767Simp		die(ENVIRON["msg_invalid_code"], 3, argv)
151180767Simp	}
152180767Simp
153180767Simp	#
154180767Simp	# Calculate the substr start-position of the name
155180767Simp	#
156180767Simp	name_start = 0
157180767Simp	n = 4
158180767Simp	if (FILENAME ~ /\.tab$/)
159180767Simp		n = 2
160180767Simp	while (--n)
161180767Simp	{
162180767Simp		#
163180767Simp		# Validate field-length of 2nd/3rd columns while we are here
164180767Simp		#
165180767Simp		if (n > 1  && length(line[n]) != 3)
166180767Simp		{
167180767Simp			argv[1] = FILENAME
168180767Simp			argv[2] = lineno
169180767Simp			die(ENVIRON["msg_invalid_format"], 2, argv)
170180767Simp		}
171180767Simp
172180767Simp		name_start += length(line[n]) + 1
173180767Simp	}
174180767Simp
175180767Simp	# Get the name field
176180767Simp	name = substr($0, name_start + 1)
177180767Simp
178180767Simp	add_country(tlc, name)
179180767Simp}
180180767SimpEND {
181180767Simp	list = ""
182180767Simp	for (tlc in country_name)
183180767Simp	{
184180767Simp		list = list (length(list) > 0 ? " " : "") tlc
185180767Simp		print_country_name(tlc)
186180767Simp	}
187180767Simp	printf "COUNTRIES=\"%s\"\n", list
188180767Simp	print "export COUNTRIES"
189180767Simp}
190180767Simp'
191180767Simpf_read_iso3166_table()
192180767Simp{
193180767Simp	eval $( awk -v progname="$pgm"          \
194180767Simp	            "$f_read_iso3166_table_awk" \
195180767Simp	            "$_PATH_ISO3166"            )
196180767Simp}
197180767Simp
198180767Simp############################################################ MAIN
199180767Simp
200180767Simpf_dprintf "%s: Successfully loaded." timezone/iso3166.subr
201180767Simp
202180767Simpfi # ! $_TIMEZONE_ISO3166_SUBR
203180767Simp