summaryrefslogtreecommitdiff
path: root/mk/scripts/mkreadme
blob: 231cdb36ee1374c4885e6ae58b9296368b7e2b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/bin/sh
# $NetBSD: mkreadme,v 1.4 2003/09/23 01:51:44 yyamano Exp $
#
# Script for README.html generation
#
# Copyright (c) 2002 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Dan McMahill.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by the NetBSD
#        Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#


TMPDIR=${TMPDIR:-/tmp/mkreadme}
PKGSRCDIR=${PKGSRCDIR:-/usr/pkgsrc}
BMAKE=${BMAKE:-make}
AWK=${AWK:-/usr/bin/awk}
EXPR=${EXPR:-expr}

usage(){
    echo "$prog - Generates README.html files for a pkgsrc tree"
    echo "Usage:      $prog [-c|--cdrom] [-C|--prune] [-d|--debug] [-f|--ftp] "
    echo "                  [-m|--multi-arch] [-p|--pkgsrc directory] "
    echo "                  [-P|--packages directory] [-r|--restart] "
    echo "                  [-s|--single-arch] [-S|--save-database]"
    echo " "
    echo "            $prog -h|--help"
    echo " "
    echo "            $prog -v|--version"
    echo " "
    echo "The options supported by $prog are: "
    echo " "
    echo "  -C|--prune          Prune unused README.html files which may exist in"
    echo "                      pkg directories which have been removed from CVS."
    echo " "
    echo "  -c|--cdrom          Generates CD-ROM README.html files"
    echo " "
    echo "  -d|--debug          Enables (extremely verbose) debug output"
    echo " "
    echo "  -f|--ftp            Generates FTP README.html files"
    echo " "
    echo "  -h|--help           Displays this help message"
    echo " "
    echo "  -m|--multi-arch     Assumes a packages directory layout like:"
    echo "                      OS_VERSION/MACHINE_ARCH/     for example:"
    echo "                      1.{5,6}/{alpha,i386,sparc,vax}"
    echo " "
    echo "  -p|--pkgsrc dir     Specifies the pkgsrc directory.  Defaults to"
    echo "                      The value of the PKGSRCDIR environment variable"
    echo "                      if set or /usr/pkgsrc otherwise."
    echo " "
    echo "  -P|--packages dir   Specifies the packages directory."
    echo " "
    echo "  -r|--restart        Restart.  This option assumes that the database file"
    echo "                      from a previous run still exists and that the script"
    echo "                      should use that instead of recreating the database."
    echo " "
    echo "  -s|--single-arch    Assumes a single OS release and MACHINE_ARCH binary"
    echo "                      packages directory structure."
    echo " "
    echo "  -S|--save-database  Does not delete the database file after the run."
    echo "                      This is useful for debugging or re-running this script"
    echo "                      with the -r option."
    echo " "
    echo "  -v|--version        Displays the version of this script and exits."
    echo " "
    echo "Example:    $prog -p /pub/NetBSD/packages/pkgsrc -P /pub/NetBSD/packages -m -f"
    echo " "
}

clean_and_exit(){
    if [ "x$DEBUG" = "xno" -a "x$restart" = "xno" -a "x$save" = "xno" ]; then
	rm -fr $TMPDIR
    else
	echo "Debugging output left in $TMP"
    fi
    exit 1
}

prog=$0


######################################################################
#
#  Handle command line options
#
######################################################################

ftp_readme=no
restart=no
prune=no
DEBUG=no
save=no

while
    test -n "$1"
do
    case "$1"
    in

    # We're generating README.html's for a CD-ROM
    -c|--cdrom)
	ftp_readme=no
	shift
	;;

    # Prune old README.html files from pkgs which no longer exist
    -C|--prune)
	prune=yes
	shift
	;;

    # Turn on debugging
    -d|--debug)
	DEBUG=yes
	shift
	;;

    # We're generating README.html's for a CD-ROM
    -f|--ftp)
	ftp_readme=yes
	shift
	;;

	
    # Help
    -h|--help)
	usage
	exit 0
	;;

    # assume a OS_RELEASE/MACHINE_ARCH/ layout for the binary pkgs
    -m|--multi-arch)
	multiarch=yes
	shift
	;;

    # Specify pkgsrc directory
    -p|--pkgsrc)
	PKGSRCDIR=$2
	shift 2
	;;

    # Specify PACKAGES directory
    -P|--packages)
	PKGDIR=$2
	shift 2
	;;

    # Restart (ie, don't re-generate the database file)
    -r|--restart)
	restart=yes
	shift
	;;

    # assume a single directory structure layout for the binary pkgs
    -s|--single-arch)
	multiarch=no
	shift
	;;

    # Save the database files
    -S|--save-database)
	save=yes
	shift
	;;

    # Version
    -v|--version)
	${AWK} '/^#[ \t]*\$NetBSD/ {gsub(/,v/,"",$3);printf("%s:  Version %s, %s\n",$3,$4,$5); exit 0;}' $prog
	exit 0
        ;;

    -*) echo "$prog:  ERROR:  $1 is not a valid option"
	usage
	clean_and_exit
	;;

    *) 
	break
	;;

    esac
done

if [ "x$DEBUG" = "xyes" ]; then
    set -v
fi

if [ ! -d ${PKGSRCDIR} ]; then
    echo "ERROR:  package source directory ${PKGSRCDIR} does not exist"
    echo ""
    clean_and_exit
fi

if [ ! -d $TMPDIR ]; then
	mkdir -p $TMPDIR
fi

DEPENDSTREEFILE=$TMPDIR/dependstree
export DEPENDSTREEFILE
DEPENDSFILE=$TMPDIR/depends
export DEPENDSFILE
SUPPORTSFILE=$TMPDIR/supports
export SUPPORTSFILE
INDEXFILE=$TMPDIR/index
export SUPPORTSFILE
ORDERFILE=$TMPDIR/order
export ORDERFILE
DATABASEFILE=$TMPDIR/database
export DATABASEFILE
BINPKGFILE=$TMPDIR/binpkglist

echo "Starting README.html generation: `date`"

######################################################################
#
#  Extract key pkgsrc configuration variables
#
######################################################################

echo " "
echo "Extracting configuration variables:"
echo " "
if [ -d ${PKGSRCDIR}/pkgtools/pkglint ]; then
    cd ${PKGSRCDIR}/pkgtools/pkglint

    for v in DISTDIR PACKAGES PKGREPOSITORYSUBDIR PKG_SUFX MACHINE_ARCH PKG_URL OPSYS OS_VERSION FTP_PKG_URL_HOST FTP_PKG_URL_DIR CDROM_PKG_URL_HOST CDROM_PKG_URL_DIR
    do
	val=`${BMAKE} show-var VARNAME=${v}`
	if [ $? != 0 ]; then
	    echo "Error:  make show-var VARNAME=${v} in `pwd` "
	    echo "Failed.  This is a fatal error"
	    clean_and_exit
	fi
	echo "$v=$val"
	eval ${v}=${val}
    done
else
    echo "Error:   ${PKGSRCDIR}/pkgtools/pkglint does not seem to exist"
    exit 1
fi


######################################################################
#
#  Decide on FTP vs CDROM README.html files
#
######################################################################

if [ "$ftp_readme" = "yes" ]; then
    PKG_URL=${FTP_PKG_URL_HOST}${FTP_PKG_URL_DIR}
    echo "Will generate FTP readme files with PKG_URL=$PKG_URL"
else
    PKG_URL=${CDROM_PKG_URL_HOST}${CDROM_PKG_URL_DIR}
    echo "Will generate CD-ROM readme files with PKG_URL=$PKG_URL"
fi

######################################################################
#
#  Check for command line switch for packages directory
#
######################################################################

# we've been given the directory as a command line switch
if [ ! -z "$PKGDIR" ]; then
    PACKAGES=$PKGDIR
    echo "PACKAGES specified on command line to be $PKGDIR"
fi

######################################################################
#
#  Decide in binary package directory layout
#
######################################################################


# If PACKAGES is set to the default (../../pkgsrc/packages), the current
# ${MACHINE_ARCH} and "release" (uname -r) will be used. Otherwise a directory
# structure of ...pkgsrc/packages/`uname -r`/${MACHINE_ARCH} is assumed.
#
# This is the logic from bsd.pkg.mk, but I think I'd like to change it to
# come from a command line switch

if [ -z "$multiarch" -a -e ${PACKAGES} ]; then
    cd ${PACKAGES}
    case `pwd` in
	*/pkgsrc/packages)
	    multiarch=no
	    ;;

	*)
	    multiarch=yes
	    ;;
    esac
fi

if [ "x$multiarch" = "xyes" ]; then
    echo "Will generate multi-release, multi-arch readme files"
else
    echo "Will generate single-release, single-arch readme files"
fi


######################################################################
#
#  Extract Database for All Packages (longest step)
#
######################################################################

if [ "x$restart" = "xno" ] ; then
    echo " "
    echo "Extracting data.  This could take a while"
    echo " "
    npkg=1

    # make sure we don't have an old database lying around
    rm -fr $DATABASEFILE

    cd ${PKGSRCDIR}
    list=`grep '^[[:space:]]*'SUBDIR */Makefile | sed 's,/Makefile.*=[[:space:]]*,/,'`
    for pkgdir in $list
    do
	    if [ ! -d $pkgdir ]; then
		    echo " "
		    echo "WARNING:  the package directory $pkgdir is listed in" > /dev/stderr
		    echo $pkgdir | sed 's;/.*;/Makefile;g' > /dev/stderr
		    echo "but the directory does not exist.  Please fix this!" > /dev/stderr
	    else
		    cd $pkgdir
		    l=`${BMAKE} print-summary-data`
		    if [ $? != 0 ]; then
			    echo "WARNING (printdepends):  the package in $pkgdir had problem with" \
				> /dev/stderr
			    echo "    ${BMAKE} print-summary-data" > /dev/stderr
			    echo "    database information for this package" > /dev/stderr
			    echo "    will be dropped." > /dev/stderr
			    ${BMAKE} print-summary-data  2>&1 > /dev/stderr
		    else
			    echo "$l" >> $DATABASEFILE
		    fi
	    fi
	    echo -n "."
	    if [ `${EXPR} $npkg % 100 = 0` -eq 1 ]; then
		echo " "
		echo "$npkg"
	    fi
	    npkg=`${EXPR} $npkg + 1`
	    cd $PKGSRCDIR
    done
else
    echo " "
    echo "Using existing database (are you sure you wanted the -r/--restart flag?)"
    echo " "
    if [ ! -f $DATABASEFILE ]; then
	echo " "
	echo "ERROR:  You have use the -r/--restart flag but the database "
	echo "        file $DATABASEFILE does not exist"
	echo " "
	exit 1
    fi
fi

######################################################################
#
#  Generate the package and category README.html files
#
######################################################################

echo " "
echo "Generating package README.html files"
echo " "
if [ "x$DEBUG" = "xyes" ]; then
    debug=1;
else
    debug=0;
fi

${AWK} -f ${PKGSRCDIR}/mk/scripts/genreadme.awk  \
    builddependsfile=${TMPDIR}/pkgsrc.builddepends.debug \
    debug=$debug \
    dependsfile=${TMPDIR}/pkgsrc.depends.debug \
    DISTDIR=$DISTDIR \
    MACHINE_ARCH=$MACHINE_ARCH \
    MULTIARCH=$multiarch \
    OPSYS=$OPSYS \
    OS_VERSION=$OS_VERSION \
    PACKAGES=$PACKAGES \
    PKG_SUFX=$PKG_SUFX \
    PKG_URL=$PKG_URL \
    PKGREPOSITORYSUBDIR=$PKGREPOSITORYSUBDIR \
    PKGSRCDIR=$PKGSRCDIR \
    TMPDIR=$TMPDIR \
	${DATABASEFILE}

if [ $? != 0 ]; then
    echo "Error:  genreadme.awk failed to create README.html files"
    clean_and_exit
fi

######################################################################
#
#  Generate the README-IPv6.html file
#
######################################################################

echo " "
echo "Generating the README-IPv6.html file"
echo " "
cd ${PKGSRCDIR}
ipv6=${TMPDIR}/ipv6pkgs
ipv6_entries=${TMPDIR}/ipv6_entries
grep -l '^BUILD_DEFS.*=.*USE_INET6' */*/Makefile  | sed 's;Makefile;;g' > $ipv6
fgrep -f $ipv6 README-all.html | sort -t/ +1 > $ipv6_entries
sed \
    -e "/%%TRS%%/r${ipv6_entries}" \
    -e '/%%TRS%%/d' \
    templates/README.ipv6 > ${TMPDIR}/README-IPv6.html
if [ $? != 0 ]; then
    echo "Error:  README-IPv6.html generation failed (on sed script)"
    clean_and_exit
fi
if [ ! -f README-IPv6.html ] || ! cmp -s ${TMPDIR}/README-IPv6.html README-IPv6.html ; then
	mv -f ${TMPDIR}/README-IPv6.html README-IPv6.html
else
	echo "README-IPv6.html is unchanged (no changes were needed)"
fi

######################################################################
#
#  Prune README.html files which are no longer needed
#
######################################################################
if [ "x$prune" = "xyes" ]; then
    echo " "
    echo "Pruning unused README.html files"
    echo " "
    cd ${PKGSRCDIR}
    for d in `ls -d */*` ; do
	if [ -d $d -a ! -f ${d}/Makefile -a -f ${d}/README.html ]; then
	    echo "Pruning ${d}/README.html which is no longer used"
	    rm -f ${d}/README.html
	fi
    done
fi

######################################################################
#
# All done.  Clean (if needed) and exit
#
######################################################################
echo " "
echo "README.html generation finished:  `date`"
echo " "
if [ "x$DEBUG" = "xno" -a "x$restart" = "xno" -a "x$save" = "xno" ]; then
    rm -fr $TMPDIR
else
    echo "Debugging output left in $TMPDIR"
fi