perl
#
# genertoc - generate Table of Contents for linuxdoc text.
# Stick this before the call to sgmlsasp.
#
# Thomas Koenig, 1995-12-01
# This code is in the public domain.
#
# Choi Jun Ho, for Korean environment, 1996-10-14
# add -k option to genertoc and change standard stream to stdin
#

# build toc header
push(@toc,"(HLINE\n");
push(@toc,")HLINE\n");
push(@toc,"(P\n");
if ($ARGV[0] eq '-k'){
  push(@toc,"-  \n");
}else{
  push(@toc,"-Table of Contents:\n");
}
push(@toc,")P\n");

while (<STDIN>) {
    push(@lines,$_);
    if (/^\(SECT(.*)/) {
	@header = @header[0..$1];
	$header[$1]++;
    }
    if (/^\(HEADING/) {
	$_ = <STDIN>;
	push(@lines,$_);
	s/^-//;
	$_ = "-" . join(".",@header) . " " . $_;
	s/\\n/ /g;
	s/\(\\[0-9][0-9][0-9]\)/\\\1/g;
	# put a . and a tab after each number
	s/ /.\t/;
	push(@toc,"(P\n" , $_, ")P\n");
    }
}

push(@toc,"(HLINE\n");
push(@toc,")HLINE\n");

for (@lines) {
    print;
    if (/^\(TOC/) {
	print @toc;
    }
}
