--- /dev/null
+#! /bin/sh
+# -*-perl-*-
+
+exec perl -x $0 "$@";
+
+#! perl
+
+# --- The idea ---
+#
+# Each manual page contains lines of the form
+#
+# .\" @function
+#
+# for each function and macro documented in it. This program sifts through
+# all of the `toplevel' manual pages and creates little manpages which
+# include the main text for each of the functions.
+#
+# The file `links' contains a generated list of little link manpages. This
+# list is used for tidying (on a `make clean'), installing (for `make
+# install') and for pruning out old links when they're not needed any more.
+
+%top = ();
+%so = ();
+if (open SO, "links") { while (<SO>) { chomp; $so{$_} = -1; } }
+if (open TOP, "toplevel") { while (<TOP>) { chomp; $top{$_} = -1; } }
+
+foreach $f (@ARGV) {
+ ($ext = $f) =~ s/^[^.]*\.//;
+ next unless $ext =~ /^\d/;
+ $sec = $&;
+ open FILE, $f or die "open($f): $!";
+ $top = 0;
+ while (<FILE>) {
+ chomp;
+ next unless /^\.\\\"\s+\@/;
+ $top = 1;
+ $link = $';
+ $full = "$link.$ext";
+ next if $full eq $f;
+ open OUT, ">$full" or die "open(>$full): $!";
+ print OUT ".so man$sec/$f\n";
+ close OUT;
+ $so{$full}++;
+ }
+ $top{$f}++ if $top;
+ close FILE;
+}
+
+$write = 0;
+foreach $i (keys(%so)) {
+ next if $so{$i} == 0;
+ unlink $i if $so{$i} < 0;
+ $write = 1;
+}
+if ($write) {
+ open LINKS, ">links" or die "open(>links): $!";
+ foreach $i (sort(keys(%so))) {
+ print LINKS "$i\n" if $so{$i} >= 0;
+ }
+ close LINKS;
+}
+
+$write = 0;
+foreach $i (keys(%top)) {
+ $write = 1 unless $top{$i} == 0;
+}
+if ($write) {
+ open TOP, ">toplevel" or die "open(>toplevel): $!";
+ foreach $i (sort(keys(%top))) {
+ print TOP "$i\n" if $top{$i} >= 0;
+ }
+ close TOP;
+}