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
#
#    Copyright (c) 1997-2019 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/src/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
#
#
#
#--------------------------------------------------------------------
#
# 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/*.html`;
`/bin/rm -f ../ps/*.ps`;

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

# Get the MB-System version and release date from ../mbio/mb_config.h
# This header file is generated by the configure script
`grep "#define VERSION " ../mbio/mb_config.h | awk '{print "MB-System Version: ",\$3}' > mbsystem_version.txt`;
`grep "#define VERSION_DATE " ../mbio/mb_config.h | awk '{print "MB-System Release Date: ",\$3,\$4,\$5}' >> mbsystem_version.txt`;

# Copy necessary files
`cp mbsystem_home.html index.html`;
@preformatted_files = `/bin/ls -1 index.html mbsystem*.html gpl.html mbsystem_version.txt MBTeamJul2003.jpg caress_chayes_ferreira_Feb2014.jpg Christina_d_S_Ferreira_2012_crop.jpg caress2001.gif ChayesJul2003.gif Christina_d_S_Ferreira_crop.jpg Paduan_IMG_6172.jpg KrystleAnderson2013.gif ValJul2003.gif mbsystem*.gif`;
foreach $file (@preformatted_files) {
    chop($file);
    print "cp $file ../html\n";
    `cp $file ../html`;
    chmod 0664, "../html/$file";
    }
`cp ../../ChangeLog ../html/ChangeLog.txt`;

# 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;

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