#!/usr/bin/perl -w
#
# A script to extract detailed command descriptions from the user help
# files. This way, Ian does not have to re-write all the information ny
# hand.
#
# DL6RAI, Last Change: Thu Oct  8 21:35:31 GMT 1998

require "getopts.pl";

$title = "CLX Commands Reference";
$help = <<NNNN;

For this little script to work sucessfully, the following prerequisites
must be fulfilled:

1. There must be a file help.overview in the language directory.
2. This file must consist of 
   a) a headline (optionally underlined with "---" characters
   b) one introductory paragraph
   c) one paragraph explaining each command (optionally using follow-up
      lines which must start with 20 or more blank characters
   d) one or more closing paragraphs.
3. Commands and their explanation must be separated by at least two
   space characters, to separate command arguments from the explaining text.
   There may be leading space characters.

NNNN



$home = (getpwnam('clx_us'))[7];
chomp($cwd = `pwd`);
$lang = (split(/\//,$cwd))[-1]; # depends on current working directory
$helpdir = "$home/box/info/help.$lang";
$overview = "$helpdir/help.overview";
$mode = "check";

##### Parse the command line for options

$opt_h="";
$opt_c="";
$opt_p="";

&Getopts('hcp');

if (length($opt_h) > 0) {
    print $help;
    exit;
}

if (length($opt_c) > 0) {
    $mode = "check";
    print STDERR "$0 running in development mode\n";
}

if (length($opt_p) > 0) {
    $mode = "production";
    print STDERR "$0 running in production mode\n";
}

$title = $ARGV[0] if ($#ARGV == 0); # Title of the Commands overview Section

##### Analyze the overview file

$/ = "";
open(IN,"< $overview") || die "Cannot open $overview\n";

$headline = <IN>;
$intro = <IN>;
$body = <IN>;
undef $/;
$rest = <IN>;


$/ = "";
foreach ($headline,$intro,$body,$rest) {
	s/---+\n//g; 		# remove underlines (three "-" or more)
	s/\n {20,}/ /g;		# join follow-up lines
	s/^ +//g;		# remove leading blanks at the beginning
	s/\n +/\n/g;		# remove leading blanks on every line
	s/</&lt;/g;		# replace < and > characters
	s/>/&gt;/g;
	chop;
}

foreach ($intro,$rest) {
	# uppercase all keywords
	s|(\b)([A-Z/_\*]{3,})(\b)|<bf>$2</bf>|g;
}

foreach (split(/\n/,$body)) {
	($cmd,$text) = split(/\s{2,}/); # split commands and explanation
	$cmd =~ s/\s.*//;		# remove anything after first blank
	$cmd = uc($cmd);		# we want only the command name in 
					# upper case
	$brief{$cmd}=$text;		# now assign it
}

$brief{$cmd}=$text;

close(IN);

opendir(DIR,$helpdir);
@files = (sort readdir(DIR));
closedir(DIR);
@files = grep (!/^[.]+/,@files); # ignore dot files
@files = grep (!/help.overview/,@files); # and these two...
@files = grep (!/help.packclus/,@files);

##### Now spit out the complete manual on stdout

print <<NNNN;
<sect>$title
<p>

NNNN

foreach (@files) {

	$cmd = $_;
	$cmd =~ s|help\.||;	# remove the "help." portion
	$cmd =~ s|\.|/|g;	# replace '.' with '/'
	$cmd =~ tr /a-z/A-Z/;	# uppercase the word

	undef $link;		# special treatment for links
	if ( -l "$helpdir/$_" ) {
		$link = readlink "$helpdir/$_";
		$link =~ s|help\.||;
		$link =~ s|\.|/|g;
		$link =~ tr /a-z/A-Z/;
	} 

	# if its a major command (without '/') make it a <sect1> else <sect2>
	if ( $cmd =~ /\// ) { 
		$sect = "<sect2>" 
	} else { 
		$sect = "<sect1>" 
	}
	print "\n\n\n$sect$cmd\n<p>\n";

	push (@cmd_list,$cmd);
	if ( ($mode eq "check") && (! defined($brief{$cmd})) ) {
		$brief{$cmd} = "\?\?\?";
		print(STDERR "No entry for $cmd in $overview\n");
	}

	if ( $link ) {
		print "<bf>$cmd</bf>: &rarr; <bf>$link</bf>.\n";
		next;
	}

	$p = 0;
	open(IN,"< $helpdir/$_");
	$/ = "\n\n";
	while (<IN>) {
		$p++;
		s/</&lt;/g;
		s/>/&gt;/g;
		if ( /^ / ) {
			print "<tscreen><verb>\n$_</verb></tscreen>\n" ;
		} else {
			if ( $p eq 1 ) {
				s|\n|<newline>\n|g;
				s|<newline>$|\n|;
				s|^|<tt>\n|;
				s|$|\n</tt>\n<p>\n|;
			} else {
				s|(E[jx][ae]mpl[eo]s*:)|<bf>$1</bf>|;
				s|(Beispiele*:)|<bf>$1</bf>|;
#				s|([\s^\.,;]+)([A-Z0-9/\*\s_]{3,}\b)([\s\,\.\-]+)|$1<bf>$2</bf>$3|g;
				s|(\b)([A-Z/_\*]{3,})(\b)|<bf>$2</bf>|g;
			}
			print "$_";
		}
	}
	close(IN);
}

##### Finally the commands overview

print <<NNNN;


<sect>$headline
<p>
$intro
<p>
<tscreen><verb>
NNNN

foreach (sort @cmd_list) {
	if (defined $brief{$_}) {
		printf"%-20s %s\n",$_,$brief{$_};
	}
}

print <<NNNN;
</verb></tscreen>

$rest

</article>
NNNN

