eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
                    & eval 'exec perl -S $0 $argv:q'
                         if 0;
#--------------------------------------------------------------------
#    The MB-system:	make_mbhtml	8/19/97
#    $Id: make_mbhtml 2175 2014-03-18 23:03:39Z caress $
#
#    Copyright (c) 1997-2014 by 
#    D. W. Caress (caress@lamont.ldgo.columbia.edu)
#    and D. N. Chayes (dale@lamont.ldgo.columbia.edu)
#    Lamont-Doherty Earth Observatory
#    Palisades, NY  10964
#
#    See README file for copying and redistribution conditions.
#--------------------------------------------------------------------
#
# Command:
#   make_mbhtml
#
# Purpose:
#   Macro to automatically generate html files from
#   MB-System man pages and place them in the mbsystem/html
#   directory. This tool is not for direct inclusion in the
#   MB-System distribution.
#
# Author:
#   David W. Caress
#   Lamont-Doherty Earth Observatory
#   Palisades, NY  10964
#   August 19,  1997
#
# Version:
#   $Id: make_mbhtml 2175 2014-03-18 23:03:39Z caress $
#
# Revisions:
#   $Log: make_mbhtml,v $
#   Revision 5.12  2003/12/04 23:10:21  caress
#   Fixed problems with format 54 EM12DARW due to old code assuming how internal structure was packed. Also changed handling of beamflags for formats that don't support beamflags. Now flagged beams will always be nulled in such cases.
#
#   Revision 5.11  2003/08/07 21:21:50  caress
#   Changed to reflect use of CVS instead of raw RCS and the placement of source manual pages in mbystem/man/manl instead of src/* directories.
#
#   Revision 5.10  2003/04/17 21:43:48  caress
#   Release 5.0.beta30
#
#   Revision 5.9  2002/08/30 19:55:38  caress
#   Release 5.0.beta22.
#   Fixed script for SGI.
#
#   Revision 5.8  2002/08/28 01:36:56  caress
#   Updated documentation for 5.0.beta22.
#
#   Revision 5.7  2002/07/22 01:11:43  caress
#   Added generation of postscript files.
#
#   Revision 5.6  2002/01/02 20:20:08  caress
#   Fixed handling of operating system differences.
#
# Revision 5.5  2001/12/30  21:03:54  caress
# Fixed handling of operating system parameter.
#
#   Revision 5.4  2001/11/19 23:52:41  dcaress
#   Made it work on both SGI's and Linux.
#
#   Revision 5.3  2001-07-20 11:00:05-07  caress
#   Don't know what I did...
#
# Revision 5.2  2001/06/08  21:54:03  caress
# Version 5.0.beta01
#
# Revision 5.1  2001/04/25  05:10:56  caress
# Changed from using awf to groff.
#
#   Revision 5.0  2001/03/23 01:48:38  caress
#   Trying to get out release 5.0.beta00.
#
# Revision 4.3  1997/09/17  21:48:31  caress
# Fixed typo.
#
# Revision 4.2  1997/09/17  21:46:07  caress
# Put last update into man page html files.
#
# Revision 4.1  1997/09/17  20:07:29  caress
# Fixed details.
#
# Revision 4.0  1997/09/15  19:13:10  caress
# Real Version 4.5
#
#
#--------------------------------------------------------------------
#
# Set program name
$program_name = "make_mbhtml";

# Make html and ps directories if necessary
print "make sure ../../html and ../../ps exist and are empty\n";
`mkdir ../../html`;
`mkdir ../../ps`;
`/bin/rm -f ../../html/*`;
`/bin/rm -f ../../ps/*`;

# generate mbsystem_formats.html using special option for mbformat
print "mbformat -W > mbsystem_formats.html\n";
`mbformat -W > mbsystem_formats.html`;

# Copy necessary files
@preformatted_files = `/bin/ls -1 mbsystem*.html gpl.html MBTeamJul2003.gif caress2001.gif ChayesJul2003.gif ValJul2003.gif mbsystem*.gif`;
foreach $file (@preformatted_files) {
    chop($file);
    print "cp $file ../../html\n";
    `cp $file ../../html`;
    chmod 0664, "../../html/$file";
    }

# Make index.html
`cd ../../html ; ln -s mbsystem_home.html index.html`;

# Get list of man page files to process
@manual_pages = `/bin/ls -1 ../../man/man*/* | grep [mh][bs]`;

# Get header and footer data
@man_header_lines = `cat mbsystem_man_hdr.frag`;
@man_header2_lines = `cat mbsystem_man_hdr2.frag`;
@man_footer_lines = `cat mbsystem_man_ftr.frag`;

# Get operating system
$os = `uname -s`;
chop($os);
if ($os eq "IRIX64") 
    {
    $groff = "awf";
    $groff = "/usr/freeware/bin/groff";
    }
else
    {
    $groff = "groff";
    }
print "Operating System: $os\nFormatter: $groff\n";

# Loop over files
`cc man2html.c -o man2html`;
foreach $manpageraw (@manual_pages) {
    chop($manpageraw);
    ($man_page) = $manpageraw =~ /^\.\.\/\.\.\/man\/man.\/(\S+)\../;
    print "\nProcessing manual page for $man_page...\n";

    # first generate a postscript version for mbsystem/ps
    print "Generating ../../ps/$man_page.ps\n";
    `$groff -man -Tps $manpageraw > ../../ps/$man_page.ps`;
    
    # Generate an initial html file
    print "Generating ../../html/$man_page.html\n";
    $raw_html_file = "./$man_page.html";
    $date = `head -1 $manpageraw | awk '{print \$4,\$5,\$6}'`;
    chop($date);
    chop($date);
    $date = substr($date, 1);
#    `cat $manpageraw | ./man2html -leftm 1 -topm 5 -botm 5 -pgsize 55 > $raw_html_file`; 
#    `$groff -man -Thtml $manpageraw  > $raw_html_file`;
    `cat $manpageraw | man2html -f > $raw_html_file`;

    # Open the input file
    if (!open(IHTML,"$raw_html_file"))
	{
	print "\a";
	die "Cannot open raw html file $raw_html_file\nMacro $program_name aborted.\n";
	}

    # Open the output html file
    $html_file = "../../html/$man_page.html";
    if (!open(OHTML,">$html_file"))
	{
	print "\a";
	die "Cannot open output file $html_file\nMacro $program_name aborted.\n";
	}

    # Put header stuff into html file
    foreach $line (@man_header_lines) {
	print OHTML $line;
	}
    print OHTML "   <TITLE>MB-System Unix Manual Page: $man_page</TITLE>\n";
    foreach $line (@man_header2_lines) {
	print OHTML $line;
	}
	
    # Read and process lines from raw html file
    $line_count = 0;
    $line_total = `cat $raw_html_file | wc -l`;
    while ($line = <IHTML>) {
	$line_count++;
	if ($line_count > 3 
	    && $line_count < $line_total - 3)
	    {
	    print OHTML $line;
	    }
	}

    # Close and remove the raw html file
    close IHTML;
    `rm -f $raw_html_file`;
    
    # Put last update date line into html file
    print OHTML "</PRE>\n\n<CENTER><P><BR>\nLast Updated: $date</P></CENTER>\n\n";

    # Put footer stuff into html file
    foreach $line (@man_footer_lines) {
	print OHTML $line;
	}

    # Close the output file and set mode
    close OHTML;
    chmod 0664, $html_file;
    
    # End loop over man pages
    }
    
# Exit the program
exit 0;

#-----------------------------------------------------------------------
