1if [ ! "$_TIMEZONE_MENUS_SUBR" ]; then _TIMEZONE_MENUS_SUBR=1
2#
3# Copyright (c) 2011-2012 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." timezone/menus.subr
33f_include $BSDCFG_SHARE/dialog.subr
34
35############################################################ GLOBALS
36
37#
38# Export special included variables required by awk(1) for `ENVIRON' visibility
39#
40export DIALOG_MENU_TAGS
41
42############################################################ FUNCTIONS
43
44# f_make_menus
45#
46# Creates the tag/item ordered-pair list environment variables for the
47# continent and country menus.
48#
49# Required variables [from continents.subr]:
50#
51# 	CONTINENTS
52# 		Space-separated list of continents.
53# 	continent_*_title
54# 		Desired menu text for the continent represented by *.
55#
56# Required variables [created by f_read_iso3166_table from iso3166.subr]:
57#
58# 	COUNTRIES
59# 		Space-separated list of 2-character country codes.
60# 	country_*_name :: when country_*_nzones < 0
61# 		Desired menu text for the country-zone represented by *, the 2-
62# 		character country code.
63#
64# Required variables [created by f_read_zones from zones.subr]:
65#
66# 	country_*_nzones
67# 		Number of zones for the country represented by *, the 2-
68# 		character country code. Should be -1 if the country has only
69# 		one single zone, otherwise 1 or greater to indicate how many
70# 		zones the country has.
71# 	country_*_cont :: when country_*_nzones < 0
72# 		Principal continent (or ocean) in which the country-zone
73# 		represented by *, the 2-character country code, resides.
74# 	country_*_cont_N :: when country_*_nzones > 0
75# 		Principal continent (or ocean) in which zone-N of the country
76# 		represented by * resides, the 2-character country code.
77# 	country_*_descr_N :: when country_*_nzones > 0
78# 		Desired submenu text for zone-N of the country represented by
79# 		*, the 2-character country code.
80#
81# Variables created by this function:
82#
83# 	continent_menu_list
84# 		Menu-list of continents.
85# 	continent_*_nitems
86# 		Number of items associated with the continent represented by *,
87# 		the continent identifier.
88# 	continent_*_tlc_N
89# 		2-character country code of the Nth item in the continent menu
90# 		for the continent represented by *, the continent identifier.
91# 	continent_*_menu_list
92# 		Menu-list of countries/zones for each continent represented by
93# 		*, the continent identifier.
94# 	country_*_menu_list
95# 		For countries that have multiple zones, this is the submenu-
96# 		list of zones for said country represented by *, the 2-
97# 		character country code.
98#
99# This function is a two-parter. Below is the awk(1) portion of the function,
100# afterward is the sh(1) function which utilizes the below awk script.
101#
102f_make_menus_awk='
103function add_zone_n_to_country_menu(tlc, n)
104{
105	zone_title = ENVIRON["country_" tlc "_descr_" n]
106	gsub(/'\''/, "'\''\\'\'\''", zone_title)
107	country_menu_list[tlc] = country_menu_list[tlc] \
108		( length(country_menu_list[tlc]) > 0 ? "\n" : "" ) \
109		n " '\''" zone_title "'\''"
110}
111BEGIN {
112	#
113	# First, count up all the countries in each continent/ocean.
114	# Be careful to count those countries which have multiple zones
115	# only once for each.  NB: some countries are in multiple
116	# continents/oceans.
117	#
118	i = split(ENVIRON["COUNTRIES"], countries, /[[:space:]]+/)
119	for (cp = 1; cp <= i; cp++)
120	{
121		tlc = countries[cp]
122		title = ENVIRON["country_" tlc "_name"]
123		gsub(/'\''/, "'\''\\'\'\''", title)
124		nzones = ENVIRON["country_" tlc "_nzones"]
125		if (!nzones)
126		{
127			# Country has no zones
128			continue
129		}
130		else if (nzones < 0)
131		{
132			# Country has only one zone
133			cont = ENVIRON["country_" tlc "_cont"]
134			nitems = ++continent_nitems[cont]
135			continent_tlc[cont,nitems] = tlc
136			continent_title[cont,nitems] = title
137		}
138		else
139		{
140			# Country has one or more zones
141			for (n = 1; n <= nzones; n++)
142			{
143				add_zone_n_to_country_menu(tlc, n)
144				cont = ENVIRON["country_" tlc "_cont_" n]
145				for (x = 1; x < n; x++)
146				{
147					contx = ENVIRON["country_"tlc"_cont_"x]
148					if (cont == contx) break
149				}
150				if (x == n)
151				{
152					nitems = ++continent_nitems[cont]
153					continent_tlc[cont,nitems] = tlc
154					continent_title[cont,nitems] = title
155				}
156			}
157		}
158	}
159}
160END {
161	tags = ENVIRON["DIALOG_MENU_TAGS"]
162	cont_menu_list = ""
163	tagn = 0
164
165	#
166	# Assemble the menu items in the menu list for each continent/ocean.
167	#
168	i = split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/)
169	for (item = 1; item <= i; item++)
170	{
171		cont = array[item]
172		if (!cont) continue
173
174		if (++tagn >= length(tags)) break
175		tag = substr(tags, tagn, 1)
176		cont_menu_list = cont_menu_list \
177			( length(cont_menu_list) > 0 ? "\n" : "" ) \
178			"'\''" tag "'\'' '\''" \
179			ENVIRON["continent_" cont "_title"] "'\''"
180
181		nitems = continent_nitems[cont]
182		printf "continent_%s_nitems=%d\n", cont, nitems
183
184		menu_list = ""
185		for (n = 1; n <= nitems; n++)
186		{
187			printf "continent_%s_tlc_%d=%s\n",
188			       cont, n, continent_tlc[cont,n]
189
190			title = continent_title[cont,n]
191			menu_list = menu_list \
192				( length(menu_list) > 0 ? "\n" : "" ) \
193				n " '\''" title "'\''"
194		}
195
196		gsub(/"/, "\\\"", menu_list)
197		printf "continent_%s_menu_list=\"%s\"\n", cont, menu_list
198	}
199
200	gsub(/"/, "\\\"", continent_menu_list)
201	printf "continent_menu_list=\"%s\"\n", cont_menu_list
202	print "export continent_menu_list"
203
204	#
205	# Dump the submenus of countries with multiple zones
206	#
207	for (tlc in country_menu_list)
208	{
209		menu_list = country_menu_list[tlc]
210		gsub(/"/, "\\\"", menu_list)
211		printf "country_%s_menu_list=\"%s\"\n", tlc, menu_list
212	}
213}
214'
215f_make_menus()
216{
217	eval $( :| awk "$f_make_menus_awk" )
218}
219
220############################################################ MAIN
221
222f_dprintf "%s: Successfully loaded." timezone/menus.subr
223
224fi # ! $_TIMEZONE_MENUS_SUBR
225