#!/usr/local/bin/perl
eval "exec perl -S  $0 $*"
                         if $running_under_some_shell;

# Joel Schroeder April 1996
#
# keywords web html report tex redoc 

$usage = "Usage: $0 srcdir urlpth [-b] [-a] [-s] [-p] [-t] [-P] [-T] > file.html\n";
$selfdoc = "
Convert a SEP report (or book) to html format.
  -b or --book      assume book format (default is report)
  -a or --no-author suppresses the author listings (good for books)
You probably won't need the following four options:
  -s or --skip      do not create files which already exist
  -p or --no-ps     suppresses creation of the .ps.gz files
  -t or --no-tar    suppresses creation of the .tar.gz files
  -P		    as -p but also suppresses the hyperlink
  -T		    as -t but also suppresses the hyperlink

$0 is invoked from the Adm directory.
WARNING: run $0 in the foreground
         (otherwise it may stop unexpectedly)
srcdir:
   A directory is created: srcdir/title (from file \"document.name\").
   .ps.gz and .tar.gz files go into srcdir/title
   file.html contains the titles and abstracts and hyperlinks:
      hyperlinks to the papers (.ps.gz).
      hyperlinks to reproducible packages (.tar.gz).
urlpth:
   urlpth should specify srcdir in url format.
   (Note: url paths may not be identical to Unix paths.)
   urlpth can be relative or absolute.
   The typical absolute urlpth is /sepwww/pub/private/docs
   (This is currently a sponsor area.)

EXAMPLES:

    Make a web page for the book tdf:

example% pwd
/homes/sep/prof/tdf/Adm
example% book2html /public/docs . -b -a > /public/docs/tdf/index.html

    Make a web page for the report sep89:

example% pwd
/wrk3/sep89/Adm
example% book2html /private/docs /private/docs/sep89 > /public/docs/sep89.html\n";


if ((@ARGV < 2) || (@ARGV > 6)) {
    die "$usage$selfdoc";
}

if (`pwd` ne `cd ../Adm; pwd`) {
    die "\n*** $0 must be invoked from dir \"Adm\" ***\n\n$usage$selfdoc";
}

$srcdir      = $ARGV[0];
$urlpth      = $ARGV[1];
$no_tar      = "";
$no_ps       = "";
$no_tar_link = "";
$no_ps_link  = "";
$no_author   = "";
$book_form   = "";
$skip        = 0;
while($ARGV[0]) {
  $_ = shift;
  SWITCH: {
    if (/^-b/)            { $book_form 	 = $_; last SWITCH; }
    if (/^--book/)        { $book_form 	 = $_; last SWITCH; }
    if (/^-t/)            { $no_tar    	 = $_; last SWITCH; }
    if (/^--no-tar/)      { $no_tar    	 = $_; last SWITCH; }
    if (/^-p/)            { $no_ps     	 = $_; last SWITCH; }
    if (/^--no-ps/)       { $no_ps     	 = $_; last SWITCH; }
    if (/^-s/)            { $skip        = $_; last SWITCH; }
    if (/^--skip/)        { $skip        = $_; last SWITCH; }
    if (/^-T/)            { $no_tar_link = $_; last SWITCH; }
    if (/^-P/)            { $no_ps_link  = $_; last SWITCH; }
    if (/^-a/)            { $no_author   = $_; last SWITCH; }
    if (/^--no-author/)   { $no_author   = $_; last SWITCH; }
    $no_op = 1;
  }
}

&getbookname(*bookname);
chop($bookname);
@tmp = split(/\W*\s+\W*/, $bookname);
$bookdir = @tmp[0];
$bookdir =~ tr/A-Z/a-z/;
&getchapters(*chapters, *papers);
if ($#chapters == -1) {
    @chapters = split(/[ \t\n]+/, `cd ..; ls -p -d [a-z]*`);
}
$index_html = "";

`mkdir $srcdir/$bookdir`;

foreach $chapnum (0..$#chapters) {
     $chapter = $chapters[$chapnum];
     $paperopt = "";
     if ($chapnum <= $#papers) {
	 if (-f "../$chapter/$papers[$chapnum].tex") {
	     $paperopt = "tex=$papers[$chapnum].tex";
	 }
     }
     if (-d "../$chapter") {
	  print STDERR "++++++++++++++++++++++++++++++++++++++++++++ $chapter\n";
	  $paper = `cd ../$chapter; paper2html $srcdir/$bookdir $urlpth $paperopt $no_ps $no_tar $skip $no_author $book_form $no_ps_link $no_tar_link`;
          $index_html = $index_html.$paper;
	  print STDERR "-------------------------------------------- $chapter\n";
      }
 }

# check for cake or make
$make = "gmake"; # set default to gmake
if (system("gmake -n -s clean > /dev/null") == 0) {
    $make = "gmake";
}
else {
    if (system("cake -n -s clean > /dev/null") == 0) {
	$make = "cake";
    }
}

%tardirmsgs = (
	       "Lib",  " : subroutine library of this document.",
	       "Src",  " : main routines of this document.",
	       "Data", " : field data processed by this document",
	       "Adm",  " : administration directory of this document."	      
	    );
foreach $currdir (keys(%tardirmsgs)) {
    $skip_this_one = $skip && (-f "$srcdir/$bookdir/$currdir.tar.gz");
    if ((-d "../$currdir") && ($no_tar eq "") && !($skip_this_one)) {
	print STDERR "Tar-ring directory: $currdir\n";
	`cd ../$currdir; $make clean > /dev/null`;
	`cd ..; tar hcf junk.tar ./$currdir; mv junk.tar Adm/$currdir.tar`;
	`gzip $currdir.tar`;
	`cp $currdir.tar.gz $srcdir/$bookdir/$currdir.tar.gz`;
	`rm $currdir.tar.gz`;
    }
}

# create index.html
print "<HTML>\n\n<HEAD><TITLE> $bookname</TITLE>\n";
print "</HEAD>\n\n<BODY>\n<H2 ALIGN = CENTER>$bookname</H2>\n<HR>\n";
print $index_html;
print "<HR>\n";
foreach $currdir (keys(%tardirmsgs)) {
    if (-d "../$currdir") {
	$size = (stat("$srcdir/$bookdir/$currdir.tar.gz"))[7];
	$size = int($size/1024);
	print "<A HREF = $urlpth/$currdir.tar.gz>$currdir.tar.gz (${size}K)</A>$tardirmsgs{$currdir}\n<BR>\n";
    }
}
print "\n</BODY>\n\n</HTML>\n";

exit;

# get the name of the report or book based on document.name
sub getbookname {
   local(*name) = @_;
   open(NAMEFILE, "<document.name");
   $not_found = 1;
   while (<NAMEFILE>) {
       if ((s/^#define\s*BOOK\s*//) || (s/^\s*BOOK\s*:?=\s*//))
       {
	   $name = $_;
	   $not_found = 0;
	   last;
       }
   }
   if ($not_found) { # no name found, use the directory name
       $name = `cd ..; pwd`;
       $name =~ s#(/.*)*/##;
   }
}

sub getchapters{
    local(*chap, *filenames) = @_;
    open(CHAPFILE, "<all.tex");
    while (<CHAPFILE>) {
	if (/^[^%].*\\WHERE\/\s*([a-z][^}]*)\}\s*\{([^}]+)\}/) {
            push(@chap, $1);
	    push(@filenames, $2);
	}
        else {
	    if (/^[^%].*\.\/\.\.\/([a-z][^}]*)\}\s*\{([^}]+)\}/) {
                 push(@chap, $1);
                 push(@filenames, $2);
}
            else {
                if (/\\paper\{([a-z][^}]*)\}/) {
	            push(@chap, $1);
		    push(@filenames, "paper");
                }
            }
        }
    }
    close(CHAPFILE);
}

