summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/lintpkgsrc.pl
blob: 7f03aeec21b8f849cb72b0d434c165d190337720 (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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
#!@PREFIX@/bin/perl

# $NetBSD: lintpkgsrc.pl,v 1.68 2002/07/11 10:22:26 abs Exp $

# Written by David Brownlee <abs@netbsd.org>.
#
# Caveats:
#	The 'Makefile parsing' algorithym used to obtain package versions
#	and DEPENDS information is geared towards speed rather than perfection,
#	though it has got somewhat better over time, it only parses the
#	simplest Makefile conditionals. (a == b, no && etc).
#
# TODO: Handle fun DEPENDS like avifile-devel with
#			{qt2-designer>=2.2.4,qt2-designer-kde>=2.3.1nb1} 

$^W = 1;
use locale;
use strict;
use Getopt::Std;
use File::Find;
use Cwd;
my(	$pkglist,		# list of Pkg packages
	$default_vars,		# Set for Makefiles, inc PACKAGES & PKGSRCDIR
	%opt,			# Command line options
	%vuln,			# vulnerability data
	@matched_prebuiltpackages,# List of obsolete prebuilt package paths
	@prebuilt_pkgdirs,	# Use to follow symlinks in prebuilt pkgdirs
	%prebuilt_pkgdir_cache,	# To avoid symlink loops in prebuilt_pkgdirs
	);

$ENV{PATH} .= ':/usr/sbin';

if (! getopts('BDK:LM:OP:RSVdg:hilmopru', \%opt) || $opt{h} ||
	! ( defined($opt{d}) || defined($opt{g}) || defined($opt{i}) ||
	    defined($opt{l}) || defined($opt{m}) || defined($opt{o}) ||
	    defined($opt{p}) || defined($opt{r}) || defined($opt{u}) ||
	    defined($opt{B}) || defined($opt{D}) || defined($opt{R}) ||
	    defined($opt{O}) || defined($opt{S}) || defined($opt{V}) ))
    { usage_and_exit(); }
$| = 1;

get_default_makefile_vars(); # $default_vars

if ($opt{D} && @ARGV)
    {
    foreach my $file (@ARGV)
	{
	if ( -d $file)
	    { $file .= "/Makefile"; }
	if (! -f $file)
	    { fail("No such file: $file"); }
	my($pkgname, $vars);
	($pkgname, $vars) = parse_makefile_pkgsrc($file);
	$pkgname ||= 'UNDEFINED';
	print "$file -> $pkgname\n";
	foreach my $varname (sort keys %{$vars})
	    { print "\t$varname = $vars->{$varname}\n"; }
	if ($opt{d})
	    { pkgsrc_check_depends(); }
	}
    exit;
    }

# main
    {
    my($pkglint_flags, $pkgsrcdir, $pkgdistdir);

    $pkgsrcdir = $default_vars->{PKGSRCDIR};
    $pkgdistdir = $default_vars->{DISTDIR};
    $pkglint_flags = '-v';

    if ($opt{r} && !$opt{o} && !$opt{m} && !$opt{p})
	{ $opt{o} = $opt{m} = $opt{p} = 1; }
    if ($opt{o} || $opt{m})
	{
	my(@baddist);

	@baddist = scan_pkgsrc_distfiles_vs_distinfo($pkgsrcdir, $pkgdistdir,
							$opt{o}, $opt{m});
	if ($opt{r})
	    {
	    safe_chdir("$pkgdistdir");
	    verbose("Unlinking 'bad' distfiles\n");
	    foreach my $distfile (@baddist)
		{ unlink($distfile); }
	    }
	}

    # List BROKEN packages
    if ($opt{B})
	{
	scan_pkgsrc_makefiles($pkgsrcdir);
	foreach my $pkgver ($pkglist->pkgver)
	    {
	    $pkgver->var('BROKEN') || next;
	    print $pkgver->pkgname.': '.$pkgver->var('BROKEN')."\n";
	    }
	}

    # List obsolete or NO_BIN_ON_FTP/RESTRICTED prebuilt packages
    #
    if ($opt{p} || $opt{O} || $opt{R} || $opt{V})
	{
	if ($opt{V})
	    {
	    my($vuln) = "$pkgdistdir/vulnerabilities";
	    if (! open(VULN, $vuln))
		{ fail("Unable to open '$vuln': $!"); }
	    while (<VULN>)
		{
		s/#.*//;
		if ( /([^*?[]+)(<|>|<=|>=)(\d\S+)/ ) 
		    {
		    my($pkg, $cmp, $ver) = ($1, $2, $3);
		    push(@{$vuln{$pkg}},"$cmp $ver");
		    }
		}
	    close(VULN);
	    }
	if ($opt{p} || $opt{O} || $opt{R} || $opt{V})
	    { scan_pkgsrc_makefiles($pkgsrcdir); }
	@prebuilt_pkgdirs = ($default_vars->{PACKAGES});
	%prebuilt_pkgdir_cache = ();
	while (@prebuilt_pkgdirs)
	    { find(\&check_prebuilt_packages, shift @prebuilt_pkgdirs); }
	if ($opt{r})
	    {
	    verbose("Unlinking listed prebuiltpackages\n");
	    foreach my $pkgfile (@matched_prebuiltpackages)
		{ unlink($pkgfile); }
	    }
	}

    if ($opt{S})
	{
	my(%in_subdir);

	foreach my $cat (list_pkgsrc_categories($pkgsrcdir))
	    {
	    my($vars) = parse_makefile_vars("$pkgsrcdir/$cat/Makefile");
	    if (! $vars->{SUBDIR})
		{ print "Warning - no SUBDIR for $cat\n"; next; }
	    foreach my $pkgdir (split(/\s+/, $vars->{SUBDIR}))
		{ $in_subdir{"$cat/$pkgdir"} = 1; }
	    }

	scan_pkgsrc_makefiles($pkgsrcdir);
	foreach my $pkgver ($pkglist->pkgver)
	    {
	    if (!defined $in_subdir{$pkgver->var('dir')})
		{ print $pkgver->var('dir').": Not in SUBDIR\n"; }
	    }
	}

    if ($opt{g})
	{
	my($tmpfile);

	$tmpfile = "$opt{g}.tmp.$$";

	scan_pkgsrc_makefiles($pkgsrcdir);
	if (!open(TABLE, ">$tmpfile"))
	    { fail("Unable to write '$tmpfile': $!"); }
	foreach my $pkgver ($pkglist->pkgver)
	    {
	    print TABLE $pkgver->pkg."\t".$pkgver->var('dir')."\t".
							$pkgver->ver."\n";
	    }
	if (!close(TABLE))
	    { fail("Error while writing '$tmpfile': $!"); }
	if (!rename($tmpfile, $opt{g}))
	    { fail("Error in rename('$tmpfile','$opt{g}'): $!"); }
	}

    if ($opt{d})
	{
	scan_pkgsrc_makefiles($pkgsrcdir);
	pkgsrc_check_depends();
	}
    if ($opt{i} || $opt{u})
	{
	my(@pkgs, @update);

	@pkgs = list_installed_packages();
	scan_pkgsrc_makefiles($pkgsrcdir);

	foreach my $pkgname (sort @pkgs)
	    {
	    if ($_ = invalid_version($pkgname))
		{
		print $_;
		if ($pkgname =~ /^([^*?[]+)-([\d*?[].*)/) 
		    {
		    foreach my $pkgver ($pkglist->pkgver($1))
			{
			$pkgver->var('dir') =~ /-current/ && next;
			push(@update, $pkgver);
			last;
			}
		    }
		}
	    }

	if ($opt{u})
	    {
	    print "\nREQUIRED details for packages that could be updated:\n";
	    foreach my $pkgver (@update)
		{
		print $pkgver->pkg.':';
		if (open(PKGINFO, 'pkg_info -R '.$pkgver->pkg.'|'))
		    {
		    my($list);
		    while (<PKGINFO>)
			{
			if (/Required by:/)
			    { $list = 1; }
			elsif ($list)
			    {
			    chomp;
			    s/-\d.*//;
			    print " $_";
			    }
			}
		    close(PKGINFO);
		    }
		print "\n";
		}
	    print "\nRunning 'make fetch-list | sh' for each package:\n";
	    foreach my $pkgver (@update)
		{
		my($pkgdir);

		$pkgdir = $pkgver->var('dir');
		if (!defined($pkgdir))
		    { fail('Unable to determine '.$pkgver->pkg.' directory'); }
		print "$pkgsrcdir/$pkgdir\n";
		safe_chdir("$pkgsrcdir/$pkgdir");
		system('make fetch-list | sh');
		}
	    }
	}
    if ($opt{l})
	{ pkglint_all_pkgsrc($pkgsrcdir, $pkglint_flags); }
    }
exit;

# Could speed up by building a cache of package names to paths, then processing
# each package name once against the tests.
sub check_prebuilt_packages
    {
    if ($_ eq 'distfiles' || $_ eq 'pkgsrc') # Skip these subdirs if present
	{ $File::Find::prune = 1; }
    elsif (/(.+)-(\d.*)\.tgz$/)
	{
	my($pkg, $ver);
	($pkg, $ver) = ($1, $2);

	if ($opt{V} && $vuln{$pkg})
	    {
	    foreach my $chk (@{$vuln{$pkg}})
		{
		my($test, $matchver) = split(' ',$chk);
		if (deweycmp($ver, $test, $matchver))
		    {
		    print "$File::Find::dir/$_\n";
		    push(@matched_prebuiltpackages, "$File::Find::dir/$_");
		    last;
		    }
		}
	    }

	my($pkgs);
	if ($pkgs = $pkglist->pkgs($pkg))
	    {
	    my($pkgver) = $pkgs->pkgver($ver);
	    if (!defined $pkgver)
		{
		if ($opt{p})
		    {
		    print "$File::Find::dir/$_\n";
		    push(@matched_prebuiltpackages, "$File::Find::dir/$_");
		    }
		# Pick probably the last version
		$pkgver = $pkgs->latestver;
		}
	    if ($opt{R} && $pkgver->var('RESTRICTED'))
		{
		print "$File::Find::dir/$_\n";
		push(@matched_prebuiltpackages, "$File::Find::dir/$_");
		}
	    if ($opt{O} && $pkgver->var('OSVERSION_SPECIFIC'))
		{
		print "$File::Find::dir/$_\n";
		push(@matched_prebuiltpackages, "$File::Find::dir/$_");
		}
	    }

	}
    elsif (-d $_)
	{
	if ($prebuilt_pkgdir_cache{"$File::Find::dir/$_"})
	    { $File::Find::prune = 1; return; }
	$prebuilt_pkgdir_cache{"$File::Find::dir/$_"} = 1;
	if (-l $_)
	    {
	    my($dest) = readlink($_);
	    if (substr($dest, 0, 1) ne '/')
		{ $dest = "$File::Find::dir/$dest"; }
	    if (!$prebuilt_pkgdir_cache{$dest})
		{ push(@prebuilt_pkgdirs, $dest); }
	    }
	}
    }

# Dewey decimal verson number matching - or thereabouts
# Also handles 'nb<N>' suffix (checked iff values otherwise identical)
#
sub deweycmp
    {
    my($match, $test, $val) = @_;
    my($cmp, $match_nb, $val_nb);

    $match_nb = $val_nb = 0;
    if ($match =~ /(.*)nb(.*)/)	# Handle nb<N> suffix
	{
	$match = $1;
	$match_nb = $2;
	}

    if ($val =~ /(.*)nb(.*)/)		# Handle nb<N> suffix 
	{
	$val = $1;
	$val_nb = $2;
	}

    $cmp = deweycmp_extract($match, $val);

    if (!$cmp)			# Iff otherwise identical, check nb suffix
	{ $cmp = deweycmp_extract($match_nb, $val_nb); }

    eval "$cmp $test 0";
    }

sub convert_to_standard_dewey
    {
    # According to the current implementation in pkg_install/lib/str.c
    # as of 2002/06/02, '_' before a number, '.', and 'pl' get treated as 0,
    # while 'rc' gets treated as -1; other characters are converted to lower
    # case and then to a number: a->1, b->2, c->3, etc. Numbers stay the same.
    # 'nb' is a special case that's already been handled when we are here.
    my($elem, $underscore, @temp);
    foreach $elem (@_) {
	if ($elem =~ /\d+/) {
	    push(@temp, $elem);
	}
	elsif ($elem =~ /^pl$/ or $elem =~ /^\.$/) {
	    push(@temp, 0);
	}
	elsif ($elem =~ /^_$/) {
	    push(@temp, 0);
	}
	elsif ($elem =~ /^rc$/) {
	    push(@temp, -1);
	}
	else {
	    push(@temp, 0);
	    push(@temp, ord($elem)-ord("a")+1);
	}
    }
    @temp;
}

sub deweycmp_extract
    {
    my($match, $val) = @_;
    my($cmp, @matchlist, @vallist);

    @matchlist = convert_to_standard_dewey(split(/(\D+)/, lc($match)));
    @vallist = convert_to_standard_dewey(split(/(\D+)/, lc($val)));
    $cmp = 0;
    while( ! $cmp && (@matchlist || @vallist))
	{
	if (!@matchlist)
	    { $cmp = -1; }
	elsif (!@vallist)
	    { $cmp = 1; }
	else
	    { $cmp = (shift @matchlist <=> shift @vallist) }
	}
    $cmp;
    }

sub fail
    { print STDERR @_, "\n"; exit(3); }

sub get_default_makefile_vars
    {
    chomp($_ = `uname -srm`);
    ( $default_vars->{OPSYS},
	$default_vars->{OS_VERSION},
	$default_vars->{MACHINE} ) = (split);

    # Handle systems without uname -p  (NetBSD pre 1.4)
    chomp($default_vars->{MACHINE_ARCH} = `uname -p 2>/dev/null`);
    if (! $default_vars->{MACHINE_ARCH} &&
				$default_vars->{OS_VERSION} eq 'NetBSD')
	{ chomp($default_vars->{MACHINE_ARCH} = `sysctl -n hw.machine_arch`);}
    if (! $default_vars->{MACHINE_ARCH})
	{ $default_vars->{MACHINE_ARCH} = $default_vars->{MACHINE}; }

    $default_vars->{OBJECT_FMT} = 'x';
    $default_vars->{LOWER_OPSYS} = lc($default_vars->{OPSYS});

    if ($opt{P})
	{ $default_vars->{PKGSRCDIR} = $opt{P}; }
    else
	{ $default_vars->{PKGSRCDIR} = '/usr/pkgsrc'; }

    my($vars);
    if (-f '/etc/mk.conf' && ($vars = parse_makefile_vars('/etc/mk.conf')))
	{
	foreach my $var (keys %{$vars})
	    { $default_vars->{$var} = $vars->{$var}; }
	}

    if ($opt{P})
	{ $default_vars->{PKGSRCDIR} = $opt{P}; }

    if ($opt{M})
	{ $default_vars->{DISTDIR} = $opt{M}; }
    $default_vars->{DISTDIR} ||= $default_vars->{PKGSRCDIR}.'/distfiles';

    if ($opt{K})
	{ $default_vars->{PACKAGES} = $opt{K}; }

    # Extract some variables from bsd.pkg.mk
    my($mkvars);
    $mkvars = parse_makefile_vars("$default_vars->{PKGSRCDIR}/mk/bsd.pkg.mk");
    foreach my $varname (keys %{$mkvars})
	{
	if ($varname =~ /_REQD$/ || $varname eq 'EXTRACT_SUFX')
	    { $default_vars->{$varname} = $mkvars->{$varname}; }
	}

    $default_vars->{PACKAGES} ||= $default_vars->{PKGSRCDIR}.'/packages';
    }

# Determine if a package version is current. If not, report correct version
# if found
#
sub invalid_version
    {
    my($pkgmatch) = @_;
    my($fail, $ok);
    my(@pkgmatches, @todo);

    @todo = ($pkgmatch);

    # We handle {} here, everything else in package_globmatch
    while ($pkgmatch = shift @todo)
	{
	if ($pkgmatch =~ /(.*){([^{}]+)}(.*)/)
	    {
	    foreach (split(',', $2))
		{ push(@todo, "$1$_$3"); }
	    }
	else
	    { push (@pkgmatches, $pkgmatch); }
	}

    foreach $pkgmatch (@pkgmatches)
	{
	my($pkg, $badver) = package_globmatch($pkgmatch);

	if (defined($badver))
	    {
	    my($pkgs);
	    if ($pkgs = $pkglist->pkgs($pkg))
		{
		$fail .= "Version mismatch: '$pkg' $badver vs ".
				    join(',', $pkgs->versions)."\n";
		}
	    else
		{ $fail .= "Unknown package: '$pkg' version $badver\n"; }
	    }
	else
	    { $ok = 1; } # If we find one match, don't bitch about others
	}
    $ok && ($fail = undef);
    $fail;
    }

# List (recursive) non directory contents of specified directory
#
sub listdir
    {
    my($base, $dir) = @_;
    my($thisdir);
    my(@list, @thislist);

    $thisdir = $base;
    if (defined($dir))
	{
	$thisdir .= "/$dir";
	$dir .= '/';
	}
    else
	{ $dir = ''; }
    opendir(DIR, $thisdir) || fail("Unable to opendir($thisdir): $!");
    @thislist = grep(substr($_, 0, 1) ne '.' && $_ ne 'CVS', readdir(DIR));
    closedir(DIR);
    foreach my $entry (@thislist)
	{
	if (-d "$thisdir/$entry")
	    { push(@list, listdir($base, "$dir$entry")); }
	else
	    { push(@list, "$dir$entry"); }
	}
    @list;
    }

# Use pkg_info to list installed packages
#
sub list_installed_packages
    {
    my(@pkgs);
    my $pkgver;

    open(PKG_INFO, 'pkg_info -a|') || fail("Unable to run pkg_info: $!");
    while ( <PKG_INFO> )
	{ push(@pkgs, (split)[0]); }
    close(PKG_INFO);

    # pkg_install is not in the pkg_info -a output, add it manually
    $pkgver = `pkg_info -V 2>/dev/null || echo 20010302`;
    chomp($pkgver);
    push(@pkgs, "pkg_install-$pkgver");
    @pkgs;
    }

# List top level pkgsrc categories
#
sub list_pkgsrc_categories
    {
    my($pkgsrcdir) = @_;
    my(@categories);

    opendir(BASE, $pkgsrcdir) || die("Unable to opendir($pkgsrcdir): $!");
    @categories = grep(substr($_, 0, 1) ne '.' && $_ ne 'CVS' &&
				-f "$pkgsrcdir/$_/Makefile", readdir(BASE));
    closedir(BASE);
    @categories;
    }

# For a given category, list potentially valid pkgdirs
#
sub list_pkgsrc_pkgdirs
    {
    my($pkgsrcdir, $cat) = @_;
    my(@pkgdirs);

    if (! opendir(CAT, "$pkgsrcdir/$cat"))
	{ die("Unable to opendir($pkgsrcdir/cat): $!"); }
    @pkgdirs = sort grep($_ ne 'Makefile' && $_ ne 'pkg' && $_ ne 'CVS' &&
					substr($_, 0, 1) ne '.', readdir(CAT));
    close(CAT);
    @pkgdirs;
    }

sub glob2regex
    {
    my($glob) = @_;
    my(@chars, $in_alt);
    my($regex);

    @chars = split(//, $glob);
    while (defined($_ = shift @chars))
	{
	if ($_ eq '*')
	    { $regex .= '.*'; }
	elsif ($_ eq '?')
	    { $regex .= '.'; }
	elsif ($_ eq '+')
	    { $regex .= '.'; }
	elsif ($_ eq '\\+')
	    { $regex .= $_ . shift @chars; }
	elsif ($_ eq '.' || $_ eq '|' )
	    { $regex .= quotemeta; }
	elsif ($_ eq '{' )
	    { $regex .= '('; ++$in_alt; }
	elsif ($_ eq '}' )
	    {
	    if (!$in_alt)		# Error
		{ return undef; }
	    $regex .= ')';
	    --$in_alt;
	    }
	elsif ($_ eq ','  && $in_alt)
	    { $regex .= '|'; }
	else
	    { $regex .= $_; }
	}
    if ($in_alt)			# Error
	{ return undef; }
    if ($regex eq $glob)
	{ return(''); }
    if ($opt{D})
	{ print "glob2regex: $glob -> $regex\n"; }
    '^'.$regex.'$';
    }

# Perform some (reasonable) subset of 'pkg_info -e' / glob(3)
# Returns (sometimes best guess at) package name,
# and either 'problem version' or undef if all OK
#
sub package_globmatch
    {
    my($pkgmatch) = @_;
    my($matchpkgname, $matchver, $regex);

    if ($pkgmatch =~ /^([^*?[]+)(<|>|<=|>=|-)(\d[^*?[{]*)$/) 
	{						# (package)(cmp)(dewey)
	my($test, @pkgvers);

	($matchpkgname, $test, $matchver) = ($1, $2, $3);

	if ($test ne '-' && $matchver !~ /^[\d.]+(pl\d+|p\d+|rc\d+|nb\d+|)*$/ )
	    { $matchver = "invalid-dewey($test$matchver)"; }
	elsif (@pkgvers = $pkglist->pkgver($matchpkgname))
	    {
	    foreach my $pkgver (@pkgvers)
		{
		if ($test eq '-')
		    {
		    if ($pkgver->ver eq $matchver)
			{ $matchver = undef; last }
		    }
		else
		    {
		    if (deweycmp($pkgver->ver, $test, $matchver))
			{ $matchver = undef; last }
		    }
		}
	    if ($matchver && $test ne '-')
		{ $matchver = "$test$matchver"; }
	    }
	}
    elsif ( $pkgmatch =~ /^([^[]+)-([\d*?{[].*)$/ )	# }
	{					 	# (package)-(globver)
	my(@pkgnames);

	($matchpkgname, $matchver) = ($1, $2);

	if (defined $pkglist->pkgs($matchpkgname))
	    { push(@pkgnames, $matchpkgname); }
	elsif ($regex = glob2regex($matchpkgname))
	    {
	    foreach my $pkg ($pkglist->pkgs)
		{ ($pkg->pkg() =~ /$regex/) && push(@pkgnames, $pkg->pkg()); }
	    }

	# Try to convert $matchver into regex version
	#
	$regex = glob2regex($matchver);

	foreach my $pkg (@pkgnames)
	    {
	    if (defined $pkglist->pkgver($pkg, $matchver))
		{ return($matchver); }

	    if ($regex)
		{
		foreach my $ver ($pkglist->pkgs($pkg)->versions)
		    {
		    if( $ver =~ /$regex/ )
			{ $matchver = undef; last }
		    }
		}
	    $matchver || last;
	    }

	# last ditch attempt to handle the whole DEPENDS as a glob
	#
	if ($matchver && ($regex = glob2regex($pkgmatch)))	# (large-glob)
	    {
	    foreach my $pkgver ($pkglist->pkgver)
		{
		if( $pkgver->pkgname =~ /$regex/ )
		    { $matchver = undef; last }
		}
	    }
	}
    else
	{ ($matchpkgname, $matchver) = ($pkgmatch, 'missing'); }
    ($matchpkgname, $matchver);
    }

# Parse a pkgsrc package makefile and return the pkgname and set variables
#
sub parse_makefile_pkgsrc
    {
    my($file) = @_;
    my($pkgname, $vars);

    $vars = parse_makefile_vars($file);

    if (!$vars) # Missing Makefile
	{ return(undef); }

    if (defined $vars->{PKGNAME})
	{ $pkgname = $vars->{PKGNAME}; }
    elsif (defined $vars->{DISTNAME})
	{ $pkgname = $vars->{DISTNAME}; }
    if (defined $pkgname)
	{
	if (defined $vars->{PKGREVISION}
	    and not $vars->{PKGREVISION} =~ /^\s*$/ )
	    {
	    $pkgname .= "nb";
	    $pkgname .= $vars->{PKGREVISION};
	    }
	if ( $pkgname =~ /\$/ )
	    { print "\rBogus: $pkgname (from $file)\n"; }
	elsif ($pkgname =~ /(.*)-(\d.*)/)
	    {
	    if ($pkglist)
		{
		my($pkgver) = $pkglist->add($1, $2);

		foreach my $var (qw(DEPENDS RESTRICTED OSVERSION_SPECIFIC BROKEN))
		    { $pkgver->var($var, $vars->{$var}); }
		if (defined $vars->{NO_BIN_ON_FTP})
		    { $pkgver->var('RESTRICTED', 'NO_BIN_ON_FTP'); }
		if ($file =~ m:([^/]+/[^/]+)/Makefile$:)
		    { $pkgver->var('dir', $1); }
		else
		    { $pkgver->var('dir', 'unknown'); }
		}
	    }
	else
	    { print "Cannot extract $pkgname version ($file)\n"; }
	return($pkgname, $vars);
	}
    else
	{ return(undef); }
    }

# Extract variable assignments from Makefile
# Much unpalatable magic to avoid having to use make (all for speed)
#
sub parse_makefile_vars
    {
    my($file) = @_;
    my($pkgname, %vars, $plus, $value, @data,
       %incfiles,
       @if_false); # 0:true 1:false 2:nested-false&nomore-elsif


    if (! open(FILE, $file))
	{ return(undef); }
    @data = map {chomp; $_} <FILE>;
    close(FILE);

    # Some Makefiles depend on these being set
    if ($file eq '/etc/mk.conf')
	{ $vars{LINTPKGSRC} = 'YES'; }
    else
	{ %vars = %{$default_vars}; }
    $vars{BSD_PKG_MK} = 'YES';

    if ($file =~ m#(.*)/#)
	{ $vars{'.CURDIR'} = $1; }
    else
	{ $vars{'.CURDIR'} = getcwd; }
    if ($opt{L})
	{ print "$file\n"; }

    while( defined($_ = shift(@data)) )
	{
	s/\s*#.*//;

	# Continuation lines
	#
	while ( substr($_, -1) eq "\\" )
	    { substr($_, -2) = shift @data; }

	# Conditionals
	#
	if (m#^\.if(|def|ndef)\s+(.*)#)
	    {
	    my($type, $false);

	    $type = $1;
	    if ($if_false[$#if_false])
		{ push(@if_false, 2); }
	    elsif( $type eq '')	# Straight if
		{ push(@if_false, parse_eval_make_false($2, \%vars)); }
	    else
		{
		$false = ! defined($vars{parse_expand_vars($2, \%vars)});
		if ( $type eq 'ndef' )
		    { $false = ! $false ; }
		push(@if_false, $false ?1 :0);
		}
	    debug("$file: .if$type (@if_false)\n");
	    next;
	    }
	if (m#^\.elif\s+(.*)# && @if_false)
	    {
	    if ($if_false[$#if_false] == 0)
		{ $if_false[$#if_false] = 2; }
	    elsif ($if_false[$#if_false] == 1 &&
		    ! parse_eval_make_false($1, \%vars) )
		{ $if_false[$#if_false] = 0; }
	    debug("$file: .elif (@if_false)\n");
	    next;
	    }
	if (m#^\.else\b# && @if_false)
	    {
	    $if_false[$#if_false] = $if_false[$#if_false] == 1?0:1;
	    debug("$file: .else (@if_false)\n");
	    next;
	    }
	if (m#^\.endif\b#)
	    {
	    pop(@if_false);
	    debug("$file: .endif (@if_false)\n");
	    next;
	    }

        $if_false[$#if_false] && next;

	# Included files (just unshift onto @data)
	#
	if (m#^\.include\s+"([^"]+)"#)
	    {
	    $_ = $1;
	    if (! m#/mk/#)
		{
		my($incfile) = ($_);

		# Expand any simple vars in $incfile
		#
		$incfile = parse_expand_vars($incfile, \%vars);

		# Handle relative path incfile
		#
		if (substr($incfile, 0, 1) ne '/')
		    { $incfile = "$vars{'.CURDIR'}/$incfile"; }
		if (!$incfiles{$incfile})
		    {
		    $incfiles{$incfile} = 1;
		    if (!open(FILE, $incfile))
			{ verbose("Cannot open '$incfile' (from $file): $!\n");}
		    else
			{
			unshift(@data, map {chomp; $_} <FILE>);
			close(FILE);
			}
		    }
		}
	    next;
	    }

	if (/^ *([-\w\.]+)\s*([:+?]?)=\s*(.*)/)
	    {
	    my($key);
	    $key = $1;
	    $plus = $2;
	    $value = $3;
	    if ($plus eq ':')
		{ $vars{$key} = parse_expand_vars($value, \%vars); }
	    elsif ($plus eq '+' && defined $vars{$key} )
		{ $vars{$key} .= " $value"; }
	    elsif ($plus ne '?' || !defined $vars{$key} )
		{ $vars{$key} = $value; }
	    } 
	}

    debug("$file: expand\n");

    # Handle variable substitutions  FRED = a-${JIM:S/-/-b-/}
    #
    my($loop);
    for ($loop = 1 ; $loop ;)
	{
	$loop = 0;
	foreach my $key (keys %vars)
	    {
	    if ( index($vars{$key}, '$') == -1 )
		{ next; }
	    $_ = parse_expand_vars($vars{$key}, \%vars);
	    if ($_ ne $vars{$key})
		{
		$vars{$key} = $_;
		$loop = 1;
		}
	    elsif ($vars{$key} =~ m#\${(\w+):([CS]([^{}])[^{}\3]+\3[^{}\3]*\3[g1]*(|:[^{}]+))}#)
		{
		my($left, $subvar, $right) = ($`, $1, $');
		my(@patterns) = split(':', $2);
		my($result);

		$result = $vars{$subvar};
		$result ||= '';

		# If $vars{$subvar} contains a $ skip it on this pass.
		# Hopefully it will get substituted and we can catch it
		# next time around.
		if (index($result, '${') != -1)
		    { next; }

		debug("$file: substitutelist $key ($result) $subvar (@patterns)\n");
		foreach (@patterns)
		    {
		    if (! m#([CS])/([^/]+)/([^/]*)/([1g]*)#)
			{ next; }

		    my($how, $from, $to, $global) = ($1, $2, $3, $4);

		    debug("$file: substituteglob $subvar, $how, $from, $to, $global\n");
		    if ($how eq 'S') # Limited substitution - keep ^ and $
			{ $from =~ s/([?.{}\]\[*+])/\\$1/g; }
		    $to =~ s/\\(\d)/\$$1/g; # Change \1 etc to $1

		    my($notfirst);
		    if ($global =~ s/1//)
			{ ($from, $notfirst) = split('\s', $from, 2); }

		    debug("$file: substituteperl $subvar, $how, $from, $to\n");
		    eval "\$result =~ s/$from/$to/$global";
		    if (defined $notfirst)
			{ $result .= " $notfirst"; }
		    }
		$vars{$key} = $left . $result . $right;
		$loop = 1;
		}
	    }
	}
    \%vars;
    }

sub parse_expand_vars
    {
    my($line, $vars) = @_;

    while ( $line =~ /\$\{([-\w.]+)\}/ )
	{
	if (defined(${$vars}{$1}))
	    { $line = $`.${$vars}{$1}.$'; }
	else
	    { $line = $`.'UNDEFINED'.$'; }
	}
    $line;
    }

sub parse_expand_vars_dumb
    {
    my($line, $vars) = @_;

    while ( $line =~ /\$\{([-\w.]+)\}/ )
	{
	if (defined(${$vars}{$1}))
	    { $line = $`.${$vars}{$1}.$'; }
	else
	    { $line = $`.'UNDEFINED'.$'; }
	}
    $line;
    }

sub parse_eval_make_false
    {
    my($line, $vars) = @_;
    my($false, $test);

    $false = 0;
    $test = parse_expand_vars_dumb($line, $vars);
    # XXX This is _so_ wrong - need to parse this correctly
    $test =~ s/""/\r/g;
    $test =~ s/"//g;
    $test =~ s/\r/""/g;

    debug("conditional: $test\n");
    # XXX Could do something with target and empty
    while ( $test =~ /(target|empty|make|defined|exists)\s*\(([^()]+)\)/ )
	{
	if ($1 eq 'exists')
	    { $_ = (-e $2) ?1 :0; }
	elsif( $1 eq 'defined')
	    { $_ = (defined($${vars}{$2}) ?1 :0); }
	else
	    { $_ = 0; }
	$test =~ s/$1\s*\([^()]+\)/$_/;
	}
    while ( $test =~ /([^\s()]+)\s+(!=|==)\s+([^\s()]+)/ )
	{
	if ($2 eq '==')
	    { $_ = ($1 eq $3) ?1 :0; }
	else
	    { $_ = ($1 ne $3) ?1 :0; }
	$test =~ s/[^\s()]+\s+(!=|==)\s+[^\s()]+/$_/;
	}
    if ($test !~ /[^<>\d()\s&|.]/ )
	{
	$false = eval "($test)?0:1";
	if (!defined $false)
	    { fail("Eval failed $line - $test"); }
	debug("conditional: evaluated to ".($false?0:1)."\n");
	}
    else
	{
	$false = 0;
	debug("conditional: defaulting to 0\n");
	}
    $false;
    }

# Run pkglint on every pkgsrc entry
#
sub pkglint_all_pkgsrc
    {
    my($pkgsrcdir, $pkglint_flags) = @_;
    my(@categories, @output);

    @categories = list_pkgsrc_categories($pkgsrcdir);
    foreach my $cat ( sort @categories )
	{
	safe_chdir("$pkgsrcdir/$cat");
	foreach my $pkgdir (list_pkgsrc_pkgdirs($pkgsrcdir, $cat))
	    {
	    if (-f "$pkgdir/Makefile")
		{
		if (!open(PKGLINT, "cd $pkgdir ; pkglint $pkglint_flags|"))
		    { fail("Unable to run pkglint: $!"); }
		@output = grep(!/^OK:/ &&
			     !/^WARN: be sure to cleanup .*work.* before/ &&
			     !/^WARN: is it a new package/ &&
			     !/^\d+ fatal errors and \d+ warnings found/
			     , <PKGLINT> );
		close(PKGLINT);
		if (@output)
		    { print "===> $cat/$pkgdir\n", @output, "\n"; }
		}
	    }
	}
    }

# chdir() || fail()
#
sub safe_chdir
    {
    my($dir) = @_;

    if (! chdir($dir))
	{ fail("Unable to chdir($dir): $!"); }
    }

# Generate pkgname->category/pkg mapping, optionally check DEPENDS
#
sub scan_pkgsrc_makefiles
    {
    my($pkgsrcdir, $check_depends) = @_;
    my(@categories);

    if ($pkglist) # Already done
	{ return; }
    $pkglist = new PkgList;
    @categories = list_pkgsrc_categories($pkgsrcdir);
    verbose("Scanning pkgsrc Makefiles: ");
    if (!$opt{L})
	{ verbose('_'x@categories."\b"x@categories); }
    else
	{ verbose("\n"); }

    foreach my $cat ( sort @categories )
	{
	foreach my $pkgdir (list_pkgsrc_pkgdirs($pkgsrcdir, $cat))
	    {
	    my($pkg, $vars) =
		    parse_makefile_pkgsrc("$pkgsrcdir/$cat/$pkgdir/Makefile");
	    }
	if (!$opt{L})
	    { verbose('.'); }
	}

    if (!$opt{L})
	{
	my ($len);
	$_ = $pkglist->numpkgver().' packages';
	$len = @categories - length($_);
	verbose("\b"x@categories, $_, ' 'x$len, "\b"x$len, "\n");
	}
    }

# Cross reference all depends
#
sub pkgsrc_check_depends
    {
    foreach my $pkgver ($pkglist->pkgver)
	{
	my($err, $msg);
	defined $pkgver->var('DEPENDS') || next;
	foreach my $depend (split(" ", $pkgver->var('DEPENDS')))
	    {
	    $depend =~ s/:.*// || next;
	    if (($msg = invalid_version($depend)))
		{
		if (!defined($err))
		    { print $pkgver->pkgname." DEPENDS errors:\n"; }
		$err = 1;
		$msg =~ s/(\n)(.)/$1\t$2/g;
		print "\t$msg";
		}
	    }
	}
    }

# Extract all distinfo entries, then verify contents of distfiles
#
sub scan_pkgsrc_distfiles_vs_distinfo
    {
    my($pkgsrcdir, $pkgdistdir, $check_unref, $check_distinfo) = @_;
    my(@categories);
    my(%distfiles, %sumfiles, @distwarn, $numpkg);
    my(%bad_distfiles);

    @categories = list_pkgsrc_categories($pkgsrcdir);

    verbose("Scanning pkgsrc distinfo: ".'_'x@categories."\b"x@categories);
    $numpkg = 0;
    foreach my $cat (sort @categories)
	{
	foreach my $pkgdir (list_pkgsrc_pkgdirs($pkgsrcdir, $cat))
	    {
	    if (open(DISTINFO, "$pkgsrcdir/$cat/$pkgdir/distinfo"))
		{
		++$numpkg;
		while( <DISTINFO> )
		    {
		    if (m/^(\w+) ?\(([^\)]+)\) = (\S+)/)
			{
			if ($2 =~ /^patch-[a-z0-9]+$/)
			    { next; }
			if (!defined $distfiles{$2})
			    {
			    $distfiles{$2}{sumtype} = $1;
			    $distfiles{$2}{sum} = $3;
			    $distfiles{$2}{path} = "$cat/$pkgdir";
			    }
			elsif ($distfiles{$2}{sumtype} eq $1 &&
				$distfiles{$2}{sum} ne $3)
			    {
			    push(@distwarn, "checksum mismatch between '$1' ".
			    "in $cat/$pkgdir and $distfiles{$2}{path}\n");
			    }
			}
		    }
		close(DISTINFO);
		}
	    }
	verbose('.');
	}
    verbose(" ($numpkg packages)\n");

    # Do not mark the vulnerabilities file as unknown
    $distfiles{vulnerabilities} = { path => 'vulnerabilities',
				      sum => 'IGNORE'};

    foreach my $file (listdir("$pkgdistdir"))
	{
	my($dist);
	if (!defined($dist = $distfiles{$file}))
	    { $bad_distfiles{$file} = 1; }
	else
	    {
	    if ($dist->{sum} ne 'IGNORE')
		{ push(@{$sumfiles{$dist->{sumtype}}}, $file); }
	    }
	}

    if ($check_unref && %bad_distfiles)
	{
	verbose(scalar(keys %bad_distfiles),
			" unreferenced file(s) in '$pkgdistdir':\n");
	print join("\n", sort keys %bad_distfiles), "\n";
	}

    if ($check_distinfo)
	{
	if (@distwarn)
	    { verbose(@distwarn); }
	verbose("checksum mismatches\n");
	safe_chdir("$pkgdistdir");
	foreach my $sum (keys %sumfiles)
	    {
	    if ($sum eq 'Size')
		{
		foreach (@{$sumfiles{$sum}})
		    {
		    if (! -f $_ || -S $_ != $distfiles{$_}{sum})
			{
			print $_, " (Size)\n";
			$bad_distfiles{$_} = 1;
			}
		    }
		next;
		}
	    open(DIGEST, "digest $sum @{$sumfiles{$sum}}|") ||
						fail("Run digest: $!");
	    while (<DIGEST>)
		{
		if (m/^$sum ?\(([^\)]+)\) = (\S+)/)
		    {
		    if ($distfiles{$1}{sum} ne $2)
			{
			print $1, " ($sum)\n";
			$bad_distfiles{$1} = 1;
			}
		    }
		}
	    close(DIGEST);
	    }
	}
    (sort keys %bad_distfiles);
    }

# Remember to update manual page when modifying option list
#
sub usage_and_exit
    {
    print "Usage: lintpkgsrc [opts] [makefiles]
opts:
  -h : This help.	 [see lintpkgsrc(1) for more information]

Installed package options:		Distfile options:
  -i : Check version against pkgsrc	  -m : List distinfo mismatches
  -u : As -i + fetch dist (may change)	  -o : List obsolete (no distinfo)

Prebuilt package options:		Makefile options:
  -p : List old/obsolete		  -B : List packages marked as 'BROKEN'
  -O : List OSVERSION_SPECIFIC		  -d : Check 'DEPENDS' up to date
  -R : List NO_BIN_ON_FTP/RESTRICTED	  -S : List packages not in 'SUBDIRS'
  -V : List known vulnerabilities

Misc:
  -g file : Generate 'pkgname pkgdir pkgver' map in file
  -l	  : Pkglint all packages
  -r	  : Remove bad files (Without -m -o -p or -V implies all, can use -R)

Modifiers:
  -K path : Set PACKAGES basedir (default PKGSRCDIR/packages)
  -M path : Set DISTDIR		 (default PKGSRCDIR/distfiles)
  -P path : Set PKGSRCDIR	 (default /usr/pkgsrc)
  -D      : Debug makefile and glob parsing
  -L      : List each Makefile when scanned
";
    exit;
    }

sub verbose
    {
    if (-t STDERR)
	{ print STDERR @_; }
    }

sub debug
    {
    ($opt{D}) && print STDERR 'DEBUG: ', @_;
    }

# PkgList is the master list of all packages in pkgsrc.
#
package PkgList;

sub add
    {
    my $self = shift;

    if (!$self->pkgs($_[0]))
	{ $self->{_pkgs}{$_[0]} = new Pkgs $_[0]; }
    $self->pkgs($_[0])->add(@_);
    }

sub new
    {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
    }

sub numpkgver
    {
    my $self = shift;
    scalar($self->pkgver);
    }

sub pkgver
    {
    my $self = shift;

    if (@_ == 0)
	{
	my(@list);
	foreach my $pkg ($self->pkgs)
	    { push(@list, $pkg->pkgver); }
	return (@list);
	}
    if (defined $self->{_pkgs}{$_[0]})
	{
	return (@_>1)?$self->{_pkgs}{$_[0]}->pkgver($_[1])
		     :$self->{_pkgs}{$_[0]}->pkgver();
	}
    return;
    }

sub pkgs
    {
    my $self = shift;
    if(@_)
	{ return $self->{_pkgs}{$_[0]} }
    else
	{ return (sort {$a->pkg cmp $b->pkg} values %{$self->{_pkgs}}); }
    return;
    }

# Pkgs is all versions of a given package (eg: apache-1.x and apache-2.x)
#
package Pkgs;

sub add
    {
    my $self = shift;
    $self->{_pkgver}{$_[1]} = new PkgVer @_;
    }

sub new
    {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    $self->{_pkg} = $_[0];
    return $self;
    }

sub versions
    {
    my $self = shift;
    return sort {$b cmp $a} keys %{$self->{_pkgver}};
    }

sub pkg
    {
    my $self = shift;
    $self->{_pkg};
    }

sub pkgver
    {
    my $self = shift;
    if (@_)
	{
	if ($self->{_pkgver}{$_[0]})
	    { return ($self->{_pkgver}{$_[0]}) }
	return;
	}
    return sort {$b->ver() cmp $a->ver()} values %{$self->{_pkgver}};
    }

sub latestver
    {
    my $self = shift;
    ($self->pkgver())[0];
    }

# PkgVer is a unique package+version
#
package PkgVer;

sub new
    {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    $self->{_pkg} = $1;
    $self->{_ver} = $2;
    return $self;
    }

sub pkgname
    {
    my $self = shift;
    $self->pkg.'-'.$self->ver;
    }

sub pkg
    {
    my $self = shift;
    $self->{_pkg};
    }

sub var
    {
    my $self = shift;
    my($key, $val) = @_;
    (defined $val)  ? ($self->{$key} = $val)
		    : $self->{$key};
    }

sub ver
    {
    my $self = shift;
    $self->{_ver};
    }