summaryrefslogtreecommitdiffstats
path: root/doincludes.pl
blob: 5da650a15e4fb064a300b73b6716af7f74d16a60 (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
#!/usr/bin/perl
#
# doincludes directory
#
# Expands #include directives in files in the directory. This is used
# to let task package pull in the contents of metapackages, keeping the
# contents up-to-date, w/o actually pulling in the metapackages themselves,
# since some metapackages are rather prone to breakage near release time.

my $dir=shift or die "no directory specified\n";

my %depends;
{
	local $/="\n\n";
	if (! open (AVAIL, "apt-cache dumpavail |")) {
		warn "cannot real available info, so not exanding includes\n";
		exit;
	}
	while (<AVAIL>) {
		my ($package)=/Package:\s*(.*?)\n/;
		my ($depends)=/Depends:\s*(.*?)\n/;
		$depends{$package}=$depends;
	}
	close AVAIL;
}

use File::Find;
find(\&processfile, $dir);

sub processfile {
	my $file=$_; # File::Find craziness.
	$file eq 'po' && -d $file && ($File::Find::prune = 1);
	return if $File::Find::dir=~/\.(svn|git)/;
	return unless $file =~ /^[-+_.a-z0-9]+$/ and -f $file;
	my @lines;
	open (IN, $file) or die "$file: $!";
	while (<IN>) {
		if (/#\s*endinclude/) {
			if ($skipping == 0) {
				die "$file: #endinclude without #include\n";
			}
			$skipping=0;
		}
		
		push @lines, $_ unless $skipping == 1;

		if (/^#\s*include\s+(\w+)/) {
			my $pkg=$1;
			if ($skipping) {
				die "$file: nested includes near $_\n";
			}
			if (! exists $depends{$pkg}) {
				warn "$file: #include $1 skipped; no such package. Leaving what was there alone.\n";
				$skipping=-1;
			}
			else {
				push @lines, "#Automatically added by doincludes.pl; do not edit.\n";
				# Split deps and remove alternates and versioned
				# deps. Include the metapackage on the list.
				push @lines, map { s/[|(].*//; "  $_\n" }
				             split(/,\s+/, $depends{$pkg}), $pkg;
				$skipping=1;
			}
		}
	}
	close IN;
	if ($skipping == 1) {
		die "$file: #include without #endinclude";
	}
	
	open (OUT, ">$file") or die "$file: $!";
	print OUT @lines;
	close OUT;
}