Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / mk / scripts / genindex.awk
blob53c4976a82ad6eb2db901231ec2f2b0d092c2077
1 #!/usr/bin/awk -f
2 # $NetBSD: genindex.awk,v 1.7 2008/09/07 11:13:51 wiz Exp $
4 # Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5 # All rights reserved.
7 # This code is derived from software contributed to The NetBSD Foundation
8 # by Dan McMahill.
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 # 3. All advertising materials mentioning features or use of this software
19 # must display the following acknowledgement:
20 # This product includes software developed by the NetBSD
21 # Foundation, Inc. and its contributors.
22 # 4. Neither the name of The NetBSD Foundation nor the names of its
23 # contributors may be used to endorse or promote products derived
24 # from this software without specific prior written permission.
26 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 # POSSIBILITY OF SUCH DAMAGE.
40 # Global variables
41 #-----------------
42 # The following associative arrays are used for storing the dependency
43 # information and other information for the packages
45 # topdepends[] : index=pkgdir (math/scilab)
46 # List of explicitly listed depencencies by name.
47 # I.e. "xless-[0-9]* pvm-3.4.3"
49 # alldepends[] : index=pkgdir (math/scilab)
50 # Flattened dependency list by name.
55 BEGIN {
56 debug = 0;
57 printf("Reading database file\n");
60 #conflicts /usr/pkgsrc/math/scilab
61 #depends /usr/pkgsrc/math/scilab xless-[0-9]*:../../x11/xless pvm-3.4.3:../../parallel/pvm3
64 # XXX Need to handle BUILD_DEPENDS/TOOL_DEPENDS split.
65 /^(build_)?depends / {
67 # Read in the entire depends tree
68 # These lines look like:
70 #depends /usr/pkgsrc/math/scilab xless-[0-9]*:../../x11/xless pvm-3.4.3:../../parallel/pvm3
71 #build_depends /usr/pkgsrc/math/scilab libtool-base>=1.4.20010614nb9:../../devel/libtool-base
73 deptype=$1;
74 # pkg=fulldir2pkgdir($2);
75 pkg = $2;
76 if (pkg in topdepends) {}
77 else {topdepends[pkg] = "";}
78 if (pkg in topbuilddepends) {}
79 else {topbuilddepends[pkg] = "";}
81 for (i = 3; i <= NF; i++) {
82 split($i, a,":");
83 pkgpat = a[1];
84 pkgdir = a[2];
85 sub(/[\.\/]*/, "", pkgdir);
86 if (pkgdir !~ /\//) {
87 pkgcat = pkg;
88 gsub(/\/.*/, "", pkgcat);
89 pkgdir=pkgcat "/" pkgdir;
90 if (debug)
91 printf("Corrected missing category directory to get \"%s\"\n",
92 pkgdir);
94 if (debug){
95 printf("package in directory %s %s on:\n",
96 pkg, deptype);
97 printf("\tpkgpat = %s\n", pkgpat);
98 printf("\tpkgdir = %s\n", pkgdir);
103 # store the package directory in a associative array with the wildcard
104 # pattern as the index since we will need to be able to look this up later
106 pat2dir[pkgpat] = pkgdir;
108 if (deptype == "depends") {
109 topdepends[pkg] = topdepends[pkg] " " pkgpat " " ;
110 if (debug) {
111 printf("Appending %s to topdepends[%s] (%s)\n",
112 pkgpat, pkg, topdepends[pkg]);
115 else {
116 if (debug) {
117 printf("Appending %s to topbuilddepends[%s] (%s)\n",
118 pkgpat, pkg, topbuilddepends[pkg]);
120 topbuilddepends[pkg] = topbuilddepends[pkg] " " pkgpat " " ;
124 next;
127 /^categories /{
128 # note: we pick out the categories slightly differently than the comment
129 # and homepage because the category name will be included in the directory
130 # name and hence the index() call points to the wrong location
131 categories[$2] = $3;
132 for(i = 4; i <= NF; i = i + 1) {
133 categories[$2] = categories[$2] " " $i;
135 next;
138 /^comment /{
139 comment[$2] = substr($0, index($0, $3));
140 next;
143 /^descr /{
144 descr[$2] = substr($0, index($0, $3));
145 next;
148 /^homepage /{
149 if( NF>=3 ) {
150 homepage[$2] = substr($0, index($0, $3));
151 } else {
152 homepage[$2] = "";
154 next;
157 /^index / {
159 # read lines like:
160 #index /usr/pkgsrc/math/scilab scilab-2.6nb3
161 # and store the directory name in a associative array where the index
162 # is the package name and in a associative array that lets us lookup
163 # name from directory. We use fuldir2pkgdir to get "math/scilab"
164 # and drop the /usr/pkgsrc part.
166 # pkgname2dir[$3] = fulldir2pkgdir($2);
167 # pkgdir2name[fulldir2pkgdir($2)] = $3;
168 pkgname2dir[$3] = $2;
169 pkgdir2name[$2] = $3;
170 next;
173 /^license /{
174 license[$2] = substr($0, index($0, $3));
175 next;
178 /^maintainer /{
179 maintainer[$2] = substr($0, index($0, $3));
180 next;
183 /^notfor /{
184 notfor[$2] = substr($0, index($0, $3));
185 next;
188 /^onlyfor /{
189 onlyfor[$2] = substr($0, index($0, $3));
190 next;
193 /^prefix /{
194 prefix[$2] = substr($0, index($0, $3));
195 next;
198 /^wildcard /{
199 wildcard[$2] = substr($0, index($0, $3));
200 next;
204 # Now recurse the tree to give a flattened depends list for each pkg
207 END {
208 if( SORT == "" ) { SORT = "sort"; }
209 indexf = SORT " > INDEX";
210 if ( dependsfile == "" ) dependsfile = "/dev/null";
211 if ( builddependsfile == "" ) builddependsfile = "/dev/null";
213 printf("Flattening dependencies\n");
214 printf("") > dependsfile;
215 for (toppkg in topdepends){
216 if (debug) printf("calling find_all_depends(%s, run)\n", toppkg);
217 find_all_depends(toppkg, "run");
218 if (debug) printf("%s depends on: %s, topdepends on %s\n",
219 toppkg, alldepends[toppkg],
220 topdepends[toppkg]);
221 printf("%s depends on: %s\n",
222 toppkg, alldepends[toppkg]) >> dependsfile;
223 flatdepends[toppkg] = alldepends[toppkg];
225 close(dependsfile);
228 # clear out the flattened depends list and repeat for the build depends
229 for( pkg in alldepends) {
230 delete alldepends[pkg];
233 printf("Flattening build dependencies\n");
234 printf("") > builddependsfile;
235 for (toppkg in topbuilddepends){
236 find_all_depends(toppkg, "build");
237 printf("%s build_depends on: %s\n",
238 toppkg, alldepends[toppkg]) >> builddependsfile;
240 close(builddependsfile);
242 printf("Generating INDEX file\n");
244 # Output format:
245 # package-name|package-path|installation-prefix|comment| \
246 # description-file|maintainer|categories|build deps|run deps|for arch| \
247 # not for opsys|homepage
249 pkgcnt = 0;
250 for (toppkg in topdepends){
251 pkgcnt++;
252 printf("%s|", pkgdir2name[toppkg]) | indexf;
253 printf("%s|", toppkg) | indexf;
254 printf("%s|", prefix[toppkg]) | indexf;
255 printf("%s|", comment[toppkg]) | indexf;
256 printf("%s|", descr[toppkg]) | indexf;
257 printf("%s|", maintainer[toppkg]) | indexf;
258 printf("%s|", categories[toppkg]) | indexf;
259 gsub(/^ /, "", alldepends[toppkg]);
260 gsub(/ $/, "", alldepends[toppkg]);
261 printf("%s|", alldepends[toppkg]) | indexf;
262 gsub(/^ /, "", flatdepends[toppkg]);
263 gsub(/ $/, "", flatdepends[toppkg]);
264 printf("%s|", flatdepends[toppkg]) | indexf;
265 printf("%s|", onlyfor[toppkg]) | indexf;
266 printf("%s|", notfor[toppkg]) | indexf;
267 printf("%s", homepage[toppkg]) | indexf;
268 printf("\n") | indexf;
270 close(indexf);
271 printf("Indexed %d packages\n", pkgcnt);
272 exit 0;
275 function find_all_depends(pkg, type, pkgreg, i, deps, depdir, topdep){
276 # pkg is the package directory, like math/scilab
278 # printf("find_all_depends(%s, %s)\n", pkg, type);
279 # if we find the package already has been fully depended
280 # then return the depends list
281 if (pkg in alldepends){
282 if (debug) printf("\t%s is allready depended. Returning %s\n",
283 pkg, alldepends[pkg]);
284 return(alldepends[pkg]);
287 # if this package has no top dependencies, enter an empty flat dependency
288 # list for it.
289 if( type == "run" ) {
290 # we only want DEPENDS
291 topdep = topdepends[pkg];
292 } else {
293 # we want BUILD_DEPENDS and DEPENDS
294 topdep = topdepends[pkg] " " topbuilddepends[pkg];
296 if (topdep ~ "^[ \t]*$") {
297 alldepends[pkg] = " ";
298 if (debug) printf("\t%s has no depends(%s). Returning %s\n",
299 pkg, topdep, alldepends[pkg]);
300 return(alldepends[pkg]);
303 # recursively gather depends that each of the depends has
304 pkgreg = reg2str(pkg);
305 split(topdep, deps);
306 i = 1;
307 alldepends[pkg] = " ";
308 while ( i in deps ) {
310 # figure out the directory name associated with the package hame
311 # in (wild card/dewey) version form
312 depdir = pat2dir[deps[i]];
313 if (debug) printf("\tadding dependency #%d on \"%s\" (%s)\n",
314 i, deps[i], depdir);
316 # do not add ourselves to the list (should not happen, but
317 # we would like to not get stuck in a loop if one exists)
318 # if (" "deps[i]" " !~ pkgreg){
320 # if we do not already have this dependency (deps[i]) listed, then add
321 # it. However, we may have already added it because another package
322 # we depend on may also have depended on
323 # deps[i].
324 if (alldepends[pkg] !~ reg2str(deps[i])){
325 alldepends[pkg] = alldepends[pkg] " " deps[i] " " find_all_depends(depdir, type);
327 else {
328 if (debug) printf("\t%s is already listed in %s\n",
329 deps[i], alldepends[pkg]);
332 i = i + 1;
333 } # while i
335 if (debug) printf("\tcalling uniq() on alldepends[%s] = %s\n",
336 pkg, alldepends[pkg]);
337 alldepends[pkg] = uniq(alldepends[pkg]);
338 if (debug) printf("\tuniq() output alldepends[%s] = %s\n",
339 pkg, alldepends[pkg]);
340 return(alldepends[pkg]);
344 # take a string which has special characters like '+' in it and
345 # escape them. Also put a space before and after since that's how
346 # we'll distinguish things like gnome from gnome-libs
348 function reg2str(reg){
349 gsub(/\./, "\\.", reg);
350 gsub(/\+/, "\\+", reg);
351 gsub(/\*/, "\\*", reg);
352 gsub(/\?/, "\\?", reg);
353 gsub(/\[/, "\\[", reg);
354 gsub(/\]/, "\\]", reg);
355 reg = " "reg" ";
356 return(reg);
360 # accepts a full path to a package directory, like "/usr/pkgsrc/math/scilab"
361 # and returns just the last 2 directories, like "math/scilab"
363 function fulldir2pkgdir(d, i){
364 i = match(d, /\/[^\/]+\/[^\/]+$/);
365 return substr(d, i + 1);
369 # take the depends lists and uniq them.
371 function uniq(list, deps, i, ulist){
373 # split out the depends
374 split(list, deps);
376 i = 1;
377 ulist = " ";
378 while (i in deps){
379 # printf("uniq(): Checking \"%s\"\n", ulist);
380 # printf(" for \"%s\"\n", reg2str(deps[i]));
381 if (ulist !~reg2str(deps[i])){
382 ulist = ulist deps[i]" ";
384 i++;
386 return(ulist);