index.subr revision 250410
1228753Smmif [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1
2228753Smm#
3228753Smm# Copyright (c) 2013 Devin Teske
4228753Smm# All Rights Reserved.
5228753Smm#
6228753Smm# Redistribution and use in source and binary forms, with or without
7228753Smm# modification, are permitted provided that the following conditions
8228753Smm# are met:
9228753Smm# 1. Redistributions of source code must retain the above copyright
10228753Smm#    notice, this list of conditions and the following disclaimer.
11228753Smm# 2. Redistributions in binary form must reproduce the above copyright
12228753Smm#    notice, this list of conditions and the following disclaimer in the
13228753Smm#    documentation and/or other materials provided with the distribution.
14228753Smm#
15228753Smm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16228753Smm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17228753Smm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18228753Smm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19228753Smm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20228753Smm# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21228753Smm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22228753Smm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23228753Smm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24228753Smm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25228753Smm# SUCH DAMAGE.
26228763Smm#
27228753Smm# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 250410 2013-05-09 16:09:39Z dteske $
28228753Smm#
29228753Smm############################################################ INCLUDES
30228753Smm
31228753SmmBSDCFG_SHARE="/usr/share/bsdconfig"
32228753Smm. $BSDCFG_SHARE/common.subr || exit 1
33228753Smmf_dprintf "%s: loading includes..." packages/index.subr
34228753Smmf_include $BSDCFG_SHARE/device.subr
35228753Smmf_include $BSDCFG_SHARE/media/common.subr
36228753Smmf_include $BSDCFG_SHARE/strings.subr
37228753Smm
38228753SmmBSDCFG_LIBE="/usr/libexec/bsdconfig"
39228753Smmf_include_lang $BSDCFG_LIBE/include/messages.subr
40228753Smm
41228753Smm############################################################ GLOBALS
42228753Smm
43228753SmmPACKAGE_INDEX=
44228753Smm_INDEX_INITTED=
45228753Smm
46228753Smm############################################################ FUNCTIONS
47228753Smm
48228753Smm# f_index_initialize $path [$var_to_set]
49228753Smm#
50228753Smm# Read and initialize the global index. $path is to be relative to the chosen
51228753Smm# media (not necessarily the filesystem; e.g. FTP) -- this is usually going to
52228753Smm# be `packages/INDEX'. Returns success unless media cannot be initialized for
53228753Smm# any reason (e.g. user cancels media selection dialog) or an error occurs. The
54228753Smm# index is sorted before being loaded into $var_to_set.
55228753Smm#
56228753Smm# NOTE: The index is processed with f_index_read() [below] after being loaded.
57228753Smm#
58228753Smmf_index_initialize()
59228753Smm{
60228753Smm	local __path="$1" __var_to_set="${2:-PACKAGE_INDEX}"
61232153Smm
62228753Smm	[ "$_INDEX_INITTED" ] && return $SUCCESS
63228753Smm	[ "$__path" ] || return $FAILURE
64228753Smm
65232153Smm	# Got any media?
66228753Smm	f_media_verify || return $FAILURE
67228753Smm
68232153Smm	# Does it move when you kick it?
69232153Smm	f_device_init media || return $FAILURE
70232153Smm
71232153Smm	f_show_info "$msg_attempting_to_fetch_file_from_selected_media" \
72228753Smm	            "$__path"
73228753Smm	eval "$__var_to_set"='$( f_device_get media "$__path" | sort )'
74228753Smm	if [ $? -ne $SUCCESS ]; then
75228753Smm		f_show_msg "$msg_unable_to_get_file_from_selected_media" \
76232153Smm		           "$__path"
77228753Smm		f_device_shutdown media
78228753Smm		return $FAILURE
79228753Smm	fi
80228753Smm
81228753Smm	f_show_info "$msg_located_index_now_reading_package_data_from_it"
82228753Smm	if ! f_index_read "$__var_to_set"; then
83228753Smm		f_show_msg "$msg_io_or_format_error_on_index_file" "$__path"
84228753Smm		return $FAILURE
85228753Smm	fi
86228753Smm
87232153Smm	_INDEX_INITTED=1
88228753Smm	return $SUCCESS
89228753Smm}
90228753Smm
91228753Smm# f_index_read [$var_to_get]
92228753Smm#
93232153Smm# Process the INDEX file (contents contained in $var_to_get) and...
94228753Smm#
95# 1. create a list ($CATEGORY_MENU_LIST) of categories with package counts
96# 2. For convenience, create $_npkgs holding the total number of all packages
97# 3. extract associative categories for each package into $_categories_$varpkg
98# 4. extract runtime dependencies for each package into $_rundeps_$varpkg
99# 5. extract a [sorted] list of categories into $PACKAGE_CATEGORIES
100# 6. create $_npkgs_$varcat holding the total number of packages in category
101#
102# NOTE: $varpkg is the product of f_str2varname $package varpkg
103# NOTE: $package is the name as it appears in the INDEX (no archive suffix)
104# NOTE: We only show categories for which there are at least one package.
105# NOTE: $varcat is the product of f_str2varname $category varcat
106#
107f_index_read()
108{
109	local var_to_get="${1:-PACKAGE_INDEX}"
110
111	# Export variables required by awk(1) below
112	export msg_no_description_provided
113	export msg_all msg_all_desc
114	export VALID_VARNAME_CHARS
115
116	eval "$( debug= f_getvar "$var_to_get" | awk -F'|' '
117	function asorti(src, dest)
118	{
119		# Copy src indices to dest and calculate array length
120		nitems = 0; for (i in src) dest[++nitems] = i
121
122		# Sort the array of indices (dest) using insertion sort method
123		for (i = 1; i <= nitems; k = i++)
124		{
125			idx = dest[i]
126			while ((k > 0) && (dest[k] > idx))
127			{
128				dest[k+1] = dest[k]
129				k--
130			}
131			dest[k+1] = idx
132		}
133
134		return nitems
135	}
136	function print_category(category, npkgs, desc)
137	{
138		cat = category
139		# Accent the category if the first page has been
140		# cached (also acting as a visitation indicator)
141		if ( ENVIRON["_index_page_" varcat "_1"] )
142			cat = cat "*"
143		printf "'\''%s'\'' '\''%s packages'\'' '\''%s'\''\n",
144		       cat, npkgs, desc
145	}
146	BEGIN {
147		valid_chars = ENVIRON["VALID_VARNAME_CHARS"]
148		default_desc = ENVIRON["msg_no_description_provided"]
149		tpkgs = 0
150		prefix = ""
151	}
152	{
153		tpkgs++
154		varpkg = $1
155		gsub("[^" valid_chars "]", "_", varpkg)
156		print "_categories_" varpkg "=\"" $7 "\""
157		split($7, pkg_categories, /[[:space:]]+/)
158		for (pkg_category in pkg_categories)
159			categories[pkg_categories[pkg_category]]++
160		print "_rundeps_" varpkg "=\"" $9 "\""
161	}
162	END {
163		print "_npkgs=" tpkgs # For convenience, total package count
164
165		n = asorti(categories, categories_sorted)
166
167		# Produce package counts for each category
168		for (i = 1; i <= n; i++)
169		{
170			cat = varcat = categories_sorted[i]
171			npkgs = categories[cat]
172			gsub("[^" valid_chars "]", "_", varcat)
173			print "_npkgs_" varcat "=\"" npkgs "\""
174		}
175
176		# Create menu list and generate list of categories at same time
177		print "CATEGORY_MENU_LIST=\""
178		print_category(ENVIRON["msg_all"], tpkgs,
179		               ENVIRON["msg_all_desc"])
180		category_list = ""
181		for (i = 1; i <= n; i++)
182		{
183			cat = varcat = categories_sorted[i]
184			npkgs = categories[cat]
185			cur_prefix = tolower(substr(cat, 1, 1))
186			if ( prefix != cur_prefix )
187				prefix = cur_prefix
188			else
189				cat = " " cat
190			gsub("[^" valid_chars "]", "_", varcat)
191			desc = ENVIRON["_category_" varcat]
192			if ( ! desc ) desc = default_desc
193			print_category(cat, npkgs, desc)
194			category_list = category_list " " cat
195		}
196		print "\""
197
198		# Produce the list of categories (calculated in above block)
199		sub(/^ /, "", category_list)
200		print "PACKAGE_CATEGORIES=\"" category_list "\""
201
202	}' )" # End-Quote
203}
204
205# f_index_extract_pages $var_to_get $var_basename $pagesize [$category]
206#
207# Extracts the package INDEX into a series of sequential variables
208# corresponding to "pages" containing up to $pagesize packages. The package
209# INDEX data must be contained in the variable $var_to_get. The extracted pages
210# are stored in variables ${var_basename}_# -- where "#" is a the page number.
211# If $category is set, only packages for that category are extracted.
212# Otherwise, if $category is "All", missing, or NULL, all packages are
213# extracted and no filtering is done.
214#
215f_index_extract_pages()
216{
217	local var_to_get="$1" var_basename="$2" pagesize="$3"
218	local category="$4" # Optional
219
220	eval "$(
221		debug= f_getvar "$var_to_get" | awk -F'|' \
222			-v cat="$category" \
223			-v pagesize="$pagesize" \
224			-v var_basename="$var_basename" '
225		BEGIN { n = page = 0 }
226		/'\''/{ gsub(/'\''/, "'\''\\'\'\''") }
227		{
228			if ( cat !~ /(^$|^All$)/ && $7 !~ \
229			     "(^|[[:space:]])" cat "([[:space:]]|$)" ) next
230			starting_new_page = (n++ == (pagesize * page))
231			if ( starting_new_page )
232				printf "%s%s", ( n > 1 ? "'\''\n" : "" ),
233				       var_basename "_" ++page "='\''"
234			printf "%s%s", ( starting_new_page ? "" : "\n" ), $0
235		}
236		END { if ( n > 0 ) print "'\''" }'
237	)"
238}
239
240############################################################ MAIN
241
242f_dprintf "%s: Successfully loaded." packages/index.subr
243
244fi # ! $_PACKAGES_INDEX_SUBR
245