1#!/usr/local/bin/perl
2#
3# Public domain.
4#
5# Create shadow directories of symbolic links to include files in the
6# source directory (i.e., for ./configure --include=link).
7#
8
9my $outdir = '';
10
11sub Scan ($$)
12{
13	my $dir = shift;
14	my $outdir = shift;
15
16	if (! -e $outdir) {
17		mkdir($outdir, 0755) || die "$outdir: $!";
18	}
19	unless (opendir(CWD, $dir)) {
20		print STDERR "$dir: $!; ignored\n";
21		return;
22	}
23	foreach my $ent (readdir(CWD)) {
24		my $file = $dir.'/'.$ent;
25		my $outfile = $outdir.'/'.$ent;
26
27		if ($ent =~ /^\./ || -l $outfile || -l $file) {
28			next;
29		}
30		if (-d $ent) {
31			if (-e $file.'/.generated') { next; }
32			Scan($file, $outfile);
33			next;
34		}
35		if ($ent =~ /\.h$/) {
36			unless (symlink($srcdir.'/'.$file, $outfile)) {
37				print STDERR "$file to $outfile: $!\n";
38			}
39		}
40	}
41	closedir(CWD);
42}
43
44sub CleanEmptyDirs ($)
45{
46	my $dir = shift;
47
48	unless (opendir(CWD, $dir)) {
49		print STDERR "$dir: $!; ignored\n";
50		return;
51	}
52	foreach my $ent (readdir(CWD)) {
53		my $subdir = $dir.'/'.$ent;
54
55		if ($ent =~ /^\./ || -l $subdir || !-d $subdir) {
56			next;
57		}
58		CleanEmptyDirs($subdir);
59		rmdir($subdir);
60	}
61	closedir(CWD);
62}
63
64if (@ARGV < 2) {
65	print STDERR "Usage: gen-includelinks.pl [source-dir] [target-dir]\n";
66	exit(1);
67}
68$srcdir = $ARGV[0];
69$outdir = $ARGV[1];
70
71if (! -e $outdir) {
72	my $now = localtime;
73	mkdir($outdir, 0755) || die "$outdir: $!";
74	open(STAMP, ">$outdir/.generated") || die "$outdir/.generated: $!";
75	print STAMP $now,"\n";
76	close(STAMP);
77}
78Scan('.', $outdir);
79CleanEmptyDirs($outdir);
80