xref: /freebsd/usr.sbin/bsdconfig/timezone/share/menus.subr (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
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# $FreeBSD$
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." timezone/menus.subr
34f_include $BSDCFG_SHARE/dialog.subr
35
36############################################################ GLOBALS
37
38#
39# Export special included variables required by awk(1) for `ENVIRON' visibility
40#
41export DIALOG_MENU_TAGS
42
43############################################################ FUNCTIONS
44
45# f_make_menus
46#
47# Creates the tag/item ordered-pair list environment variables for the
48# continent and country menus.
49#
50# Required variables [from continents.subr]:
51#
52# 	CONTINENTS
53# 		Space-separated list of continents.
54# 	continent_*_title
55# 		Desired menu text for the continent represented by *.
56#
57# Required variables [created by f_read_iso3166_table from iso3166.subr]:
58#
59# 	COUNTRIES
60# 		Space-separated list of 2-character country codes.
61# 	country_*_name :: when country_*_nzones < 0
62# 		Desired menu text for the country-zone represented by *, the 2-
63# 		character country code.
64#
65# Required variables [created by f_read_zones from zones.subr]:
66#
67# 	country_*_nzones
68# 		Number of zones for the country represented by *, the 2-
69# 		character country code. Should be -1 if the country has only
70# 		one single zone, otherwise 1 or greater to indicate how many
71# 		zones the country has.
72# 	country_*_cont :: when country_*_nzones < 0
73# 		Principal continent (or ocean) in which the country-zone
74# 		represented by *, the 2-character country code, resides.
75# 	country_*_cont_N :: when country_*_nzones > 0
76# 		Principal continent (or ocean) in which zone-N of the country
77# 		represented by * resides, the 2-character country code.
78# 	country_*_descr_N :: when country_*_nzones > 0
79# 		Desired submenu text for zone-N of the country represented by
80# 		*, the 2-character country code.
81#
82# Variables created by this function:
83#
84# 	continent_menu_list
85# 		Menu-list of continents.
86# 	continent_*_nitems
87# 		Number of items associated with the continent represented by *,
88# 		the continent identifier.
89# 	continent_*_tlc_N
90# 		2-character country code of the Nth item in the continent menu
91# 		for the continent represented by *, the continent identifier.
92# 	continent_*_menu_list
93# 		Menu-list of countries/zones for each continent represented by
94# 		*, the continent identifier.
95# 	country_*_menu_list
96# 		For countries that have multiple zones, this is the submenu-
97# 		list of zones for said country represented by *, the 2-
98# 		character country code.
99#
100# This function is a two-parter. Below is the awk(1) portion of the function,
101# afterward is the sh(1) function which utilizes the below awk script.
102#
103f_make_menus_awk='
104function add_zone_n_to_country_menu(tlc, n)
105{
106	zone_title = ENVIRON["country_" tlc "_descr_" n]
107	gsub(/'\''/, "'\''\\'\'\''", zone_title)
108	country_menu_list[tlc] = country_menu_list[tlc] \
109		( length(country_menu_list[tlc]) > 0 ? "\n" : "" ) \
110		n " '\''" zone_title "'\''"
111}
112BEGIN {
113	#
114	# First, count up all the countries in each continent/ocean.
115	# Be careful to count those countries which have multiple zones
116	# only once for each.  NB: some countries are in multiple
117	# continents/oceans.
118	#
119	i = split(ENVIRON["COUNTRIES"], countries, /[[:space:]]+/)
120	for (cp = 1; cp <= i; cp++)
121	{
122		tlc = countries[cp]
123		title = ENVIRON["country_" tlc "_name"]
124		gsub(/'\''/, "'\''\\'\'\''", title)
125		nzones = ENVIRON["country_" tlc "_nzones"]
126		if (!nzones)
127		{
128			# Country has no zones
129			continue
130		}
131		else if (nzones < 0)
132		{
133			# Country has only one zone
134			cont = ENVIRON["country_" tlc "_cont"]
135			nitems = ++continent_nitems[cont]
136			continent_tlc[cont,nitems] = tlc
137			continent_title[cont,nitems] = title
138		}
139		else
140		{
141			# Country has one or more zones
142			for (n = 1; n <= nzones; n++)
143			{
144				add_zone_n_to_country_menu(tlc, n)
145				cont = ENVIRON["country_" tlc "_cont_" n]
146				for (x = 1; x < n; x++)
147				{
148					contx = ENVIRON["country_"tlc"_cont_"x]
149					if (cont == contx) break
150				}
151				if (x == n)
152				{
153					nitems = ++continent_nitems[cont]
154					continent_tlc[cont,nitems] = tlc
155					continent_title[cont,nitems] = title
156				}
157			}
158		}
159	}
160}
161END {
162	tags = ENVIRON["DIALOG_MENU_TAGS"]
163	cont_menu_list = ""
164	tagn = 0
165
166	#
167	# Assemble the menu items in the menu list for each continent/ocean.
168	#
169	i = split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/)
170	for (item = 1; item <= i; item++)
171	{
172		cont = array[item]
173		if (!cont) continue
174
175		if (++tagn >= length(tags)) break
176		tag = substr(tags, tagn, 1)
177		cont_menu_list = cont_menu_list \
178			( length(cont_menu_list) > 0 ? "\n" : "" ) \
179			"'\''" tag "'\'' '\''" \
180			ENVIRON["continent_" cont "_title"] "'\''"
181
182		nitems = continent_nitems[cont]
183		printf "continent_%s_nitems=%d\n", cont, nitems
184
185		menu_list = ""
186		for (n = 1; n <= nitems; n++)
187		{
188			printf "continent_%s_tlc_%d=%s\n",
189			       cont, n, continent_tlc[cont,n]
190
191			title = continent_title[cont,n]
192			menu_list = menu_list \
193				( length(menu_list) > 0 ? "\n" : "" ) \
194				n " '\''" title "'\''"
195		}
196
197		gsub(/"/, "\\\"", menu_list)
198		printf "continent_%s_menu_list=\"%s\"\n", cont, menu_list
199	}
200
201	gsub(/"/, "\\\"", continent_menu_list)
202	printf "continent_menu_list=\"%s\"\n", cont_menu_list
203	print "export continent_menu_list"
204
205	#
206	# Dump the submenus of countries with multiple zones
207	#
208	for (tlc in country_menu_list)
209	{
210		menu_list = country_menu_list[tlc]
211		gsub(/"/, "\\\"", menu_list)
212		printf "country_%s_menu_list=\"%s\"\n", tlc, menu_list
213	}
214}
215'
216f_make_menus()
217{
218	eval $( :| awk "$f_make_menus_awk" )
219}
220
221############################################################ MAIN
222
223f_dprintf "%s: Successfully loaded." timezone/menus.subr
224
225fi # ! $_TIMEZONE_MENUS_SUBR
226