1238438Sdteskeif [ ! "$_TIMEZONE_MENUS_SUBR" ]; then _TIMEZONE_MENUS_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$ 28238438Sdteske# 29238438Sdteske############################################################ INCLUDES 30238438Sdteske 31240684SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32240684Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33244675Sdteskef_dprintf "%s: loading includes..." timezone/menus.subr 34240684Sdteskef_include $BSDCFG_SHARE/dialog.subr 35238438Sdteske 36238438Sdteske############################################################ GLOBALS 37238438Sdteske 38238438Sdteske# 39238438Sdteske# Export special included variables required by awk(1) for `ENVIRON' visibility 40238438Sdteske# 41238438Sdteskeexport DIALOG_MENU_TAGS 42238438Sdteske 43238438Sdteske############################################################ FUNCTIONS 44238438Sdteske 45238438Sdteske# f_make_menus 46238438Sdteske# 47238438Sdteske# Creates the tag/item ordered-pair list environment variables for the 48238438Sdteske# continent and country menus. 49238438Sdteske# 50238438Sdteske# Required variables [from continents.subr]: 51238438Sdteske# 52238438Sdteske# CONTINENTS 53238438Sdteske# Space-separated list of continents. 54238438Sdteske# continent_*_title 55238438Sdteske# Desired menu text for the continent represented by *. 56238438Sdteske# 57238438Sdteske# Required variables [created by f_read_iso3166_table from iso3166.subr]: 58238438Sdteske# 59238438Sdteske# COUNTRIES 60238438Sdteske# Space-separated list of 2-character country codes. 61238438Sdteske# country_*_name :: when country_*_nzones < 0 62238438Sdteske# Desired menu text for the country-zone represented by *, the 2- 63238438Sdteske# character country code. 64238438Sdteske# 65238438Sdteske# Required variables [created by f_read_zones from zones.subr]: 66238438Sdteske# 67238438Sdteske# country_*_nzones 68238438Sdteske# Number of zones for the country represented by *, the 2- 69238438Sdteske# character country code. Should be -1 if the country has only 70238438Sdteske# one single zone, otherwise 1 or greater to indicate how many 71238438Sdteske# zones the country has. 72238438Sdteske# country_*_cont :: when country_*_nzones < 0 73238438Sdteske# Principal continent (or ocean) in which the country-zone 74238438Sdteske# represented by *, the 2-character country code, resides. 75238438Sdteske# country_*_cont_N :: when country_*_nzones > 0 76238438Sdteske# Principal continent (or ocean) in which zone-N of the country 77238438Sdteske# represented by * resides, the 2-character country code. 78238438Sdteske# country_*_descr_N :: when country_*_nzones > 0 79238438Sdteske# Desired submenu text for zone-N of the country represented by 80238438Sdteske# *, the 2-character country code. 81238438Sdteske# 82238438Sdteske# Variables created by this function: 83238438Sdteske# 84238438Sdteske# continent_menu_list 85238438Sdteske# Menu-list of continents. 86238438Sdteske# continent_*_nitems 87238438Sdteske# Number of items associated with the continent represented by *, 88238438Sdteske# the continent identifier. 89238438Sdteske# continent_*_tlc_N 90238438Sdteske# 2-character country code of the Nth item in the continent menu 91238438Sdteske# for the continent represented by *, the continent identifier. 92238438Sdteske# continent_*_menu_list 93238438Sdteske# Menu-list of countries/zones for each continent represented by 94238438Sdteske# *, the continent identifier. 95238438Sdteske# country_*_menu_list 96238438Sdteske# For countries that have multiple zones, this is the submenu- 97238438Sdteske# list of zones for said country represented by *, the 2- 98238438Sdteske# character country code. 99238438Sdteske# 100238438Sdteske# This function is a two-parter. Below is the awk(1) portion of the function, 101238438Sdteske# afterward is the sh(1) function which utilizes the below awk script. 102238438Sdteske# 103238438Sdteskef_make_menus_awk=' 104238438Sdteskefunction add_zone_n_to_country_menu(tlc, n) 105238438Sdteske{ 106238438Sdteske zone_title = ENVIRON["country_" tlc "_descr_" n] 107263980Sdteske gsub(/'\''/, "'\''\\'\'\''", zone_title) 108238438Sdteske country_menu_list[tlc] = country_menu_list[tlc] \ 109238438Sdteske ( length(country_menu_list[tlc]) > 0 ? "\n" : "" ) \ 110238438Sdteske n " '\''" zone_title "'\''" 111238438Sdteske} 112238438SdteskeBEGIN { 113238438Sdteske # 114238438Sdteske # First, count up all the countries in each continent/ocean. 115238438Sdteske # Be careful to count those countries which have multiple zones 116238438Sdteske # only once for each. NB: some countries are in multiple 117238438Sdteske # continents/oceans. 118238438Sdteske # 119238438Sdteske i = split(ENVIRON["COUNTRIES"], countries, /[[:space:]]+/) 120238438Sdteske for (cp = 1; cp <= i; cp++) 121238438Sdteske { 122238438Sdteske tlc = countries[cp] 123238438Sdteske title = ENVIRON["country_" tlc "_name"] 124263980Sdteske gsub(/'\''/, "'\''\\'\'\''", title) 125238438Sdteske nzones = ENVIRON["country_" tlc "_nzones"] 126238438Sdteske if (!nzones) 127238438Sdteske { 128238438Sdteske # Country has no zones 129238438Sdteske continue 130238438Sdteske } 131238438Sdteske else if (nzones < 0) 132238438Sdteske { 133238438Sdteske # Country has only one zone 134238438Sdteske cont = ENVIRON["country_" tlc "_cont"] 135238438Sdteske nitems = ++continent_nitems[cont] 136238438Sdteske continent_tlc[cont,nitems] = tlc 137238438Sdteske continent_title[cont,nitems] = title 138238438Sdteske } 139238438Sdteske else 140238438Sdteske { 141238438Sdteske # Country has one or more zones 142238438Sdteske for (n = 1; n <= nzones; n++) 143238438Sdteske { 144238438Sdteske add_zone_n_to_country_menu(tlc, n) 145238438Sdteske cont = ENVIRON["country_" tlc "_cont_" n] 146238438Sdteske for (x = 1; x < n; x++) 147238438Sdteske { 148238438Sdteske contx = ENVIRON["country_"tlc"_cont_"x] 149238438Sdteske if (cont == contx) break 150238438Sdteske } 151238438Sdteske if (x == n) 152238438Sdteske { 153238438Sdteske nitems = ++continent_nitems[cont] 154238438Sdteske continent_tlc[cont,nitems] = tlc 155238438Sdteske continent_title[cont,nitems] = title 156238438Sdteske } 157238438Sdteske } 158238438Sdteske } 159238438Sdteske } 160238438Sdteske} 161238438SdteskeEND { 162238438Sdteske tags = ENVIRON["DIALOG_MENU_TAGS"] 163238438Sdteske cont_menu_list = "" 164238438Sdteske tagn = 0 165238438Sdteske 166238438Sdteske # 167238438Sdteske # Assemble the menu items in the menu list for each continent/ocean. 168238438Sdteske # 169238438Sdteske i = split(ENVIRON["CONTINENTS"], array, /[[:space:]]+/) 170238438Sdteske for (item = 1; item <= i; item++) 171238438Sdteske { 172238438Sdteske cont = array[item] 173238438Sdteske if (!cont) continue 174238438Sdteske 175238438Sdteske if (++tagn >= length(tags)) break 176238438Sdteske tag = substr(tags, tagn, 1) 177238438Sdteske cont_menu_list = cont_menu_list \ 178238438Sdteske ( length(cont_menu_list) > 0 ? "\n" : "" ) \ 179238438Sdteske "'\''" tag "'\'' '\''" \ 180238438Sdteske ENVIRON["continent_" cont "_title"] "'\''" 181238438Sdteske 182238438Sdteske nitems = continent_nitems[cont] 183238438Sdteske printf "continent_%s_nitems=%d\n", cont, nitems 184238438Sdteske 185238438Sdteske menu_list = "" 186238438Sdteske for (n = 1; n <= nitems; n++) 187238438Sdteske { 188238438Sdteske printf "continent_%s_tlc_%d=%s\n", 189238438Sdteske cont, n, continent_tlc[cont,n] 190238438Sdteske 191238438Sdteske title = continent_title[cont,n] 192238438Sdteske menu_list = menu_list \ 193238438Sdteske ( length(menu_list) > 0 ? "\n" : "" ) \ 194238438Sdteske n " '\''" title "'\''" 195238438Sdteske } 196238438Sdteske 197238438Sdteske gsub(/"/, "\\\"", menu_list) 198238438Sdteske printf "continent_%s_menu_list=\"%s\"\n", cont, menu_list 199238438Sdteske } 200238438Sdteske 201238438Sdteske gsub(/"/, "\\\"", continent_menu_list) 202238438Sdteske printf "continent_menu_list=\"%s\"\n", cont_menu_list 203238438Sdteske print "export continent_menu_list" 204238438Sdteske 205238438Sdteske # 206238438Sdteske # Dump the submenus of countries with multiple zones 207238438Sdteske # 208238438Sdteske for (tlc in country_menu_list) 209238438Sdteske { 210238438Sdteske menu_list = country_menu_list[tlc] 211238438Sdteske gsub(/"/, "\\\"", menu_list) 212238438Sdteske printf "country_%s_menu_list=\"%s\"\n", tlc, menu_list 213238438Sdteske } 214238438Sdteske} 215238438Sdteske' 216238438Sdteskef_make_menus() 217238438Sdteske{ 218238438Sdteske eval $( :| awk "$f_make_menus_awk" ) 219238438Sdteske} 220238438Sdteske 221244675Sdteske############################################################ MAIN 222244675Sdteske 223244675Sdteskef_dprintf "%s: Successfully loaded." timezone/menus.subr 224244675Sdteske 225238438Sdteskefi # ! $_TIMEZONE_MENUS_SUBR 226