#! /usr/local/bin/perl -w 

$0 =~ s,.*/,,;
$version="v1.1";
$program=$0;
$debug=0;
$mailto='mailto:skunkware@sco.com';
$baseref='http://www.sco.com/skunkware';
$homeref='http://www.sco.com/skunkware';
$isweb=0;

sub usage {
  warn "@_\n" if @_;
  $! = 0;
  die <<EOF;
Usage: $0 -w | -c file name prev next
  -w        - Generate pages for the Web
  -c        - Generate pages for the CD-ROM
  file      - name of the template (.tmpl) file
  name      - the name of the package
  prev      - the name of the previous link
  next      - the name of the next link
  base      - the base href prefix to use

  Use the word NONE for the prev and next parameters if the page doesnt
  have a previous or next page in the document chain.

EOF
}

if (@ARGV<1 || $#ARGV != 5) {
  usage();
}

$mode="0";

if($ARGV[0] eq "-w" || $ARGV[0] eq "web") {
  $mode="SKUNK_WEB";
  $isweb=1;
}
elsif($ARGV[0] eq "-c" || $ARGV[0] eq "cd") {
  $mode="SKUNK_CD";
}

if ($mode eq "0") {
  usage();
}

$now=`date '+%A %b %d, %Y at %T %Z'`; chop $now;
$basefile=$ARGV[1];
$shortname=$ARGV[2];
$previous=$ARGV[3];
$next=$ARGV[4];
$baseref=$ARGV[5];
$realbase=`basename $basefile`; chop $realbase;
#if ($isweb) {
#  if (not -d $shortname) {
#    mkdir($shortname,0755) or die "$program error: Could not create directory $shortname\n";
#  }
#  $output="$shortname/index.html";
#} else {
  $output="${realbase}.html";
#}
$template="${basefile}.tmpl";
$templflt="/lib/cpp -I. -I.. -I../src -D$mode $template |";

-f $template or die "$program error: $template not found\n";

if ($debug) {
 print "basefile : $basefile\n";
 print "now      : $now\n";
 print "shortname: $shortname\n";
 print "previous : $previous\n";
 print "next     : $next\n";
 print "output   : $output\n";
 print "realbase : $realbase\n";
 print "template : $template\n";
}

if ($previous eq "none" or $previous eq "NONE")
{
    $prevref="";
} else {
    $prevref="<AREA SHAPE=\"rect\" COORDS=\"112,2,236,26\" HREF=\"$baseref/$previous.html\">";
}

if ($next eq "none" or $next eq "NONE") {
    $nextref="";
} else {
    $nextref="<AREA SHAPE=\"rect\" COORDS=\"252,1,331,26\" HREF=\"$baseref/$next.html\">";
}

if ($debug) {
 print "nextref=$nextref\n";
 print "prevref=$prevref\n";
}

open (TEMPLATE,$templflt) or die "$program error: cannot open $template for reading\n";
open (OUTPUT,">$output") or die "$program error: cannot open $output for output\n";

sub skunkheader{
    my($foo) = @_;
    my($title,$plink,$nlink) = split(/;/,$foo);
    my($oplink,$onlink);
    if (not $plink) {
        $oplink = $prevref;
    } else {
        $oplink="<AREA SHAPE=\"rect\" COORDS=\"112,2,236,26\" HREF=\"$plink.html\">";
    }
    if (not $nlink) {
        $onlink = $nextref;
    } else {
        $onlink="<AREA SHAPE=\"rect\" COORDS=\"252,1,331,26\" HREF=\"$nlink.html\">";
    }

    if ($debug) {
        print "skunkheader: title='$title'\n";
        print "             oplink='$oplink'\n";
        print "             onlink='$onlink'\n";
    }

    if (not $title) {
        warn "$program: No TITLE specified in SKUNKHEADER\n";
    }

    if (not $isweb) {
#
# This is the header information for the Skunkware CD-ROM
#
        print OUTPUT <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!-- DO NOT EDIT -->
<!-- Automatically generated from $template by $program $version -->
<!-- DO NOT EDIT -->
<HTML>
<HEAD>
  <TITLE>$title</TITLE>
  <LINK REV="made" HREF="$mailto">
</HEAD>
<BODY BGCOLOR="#ffffff">

<MAP NAME="skunknav">
  <AREA SHAPE="rect" COORDS="347,1,460,26" HREF="$homeref/$shortname">
  $oplink
  $onlink
  <AREA SHAPE="rect" COORDS="3,1,93,26" HREF="$baseref/index.html">
</MAP>

<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%" BORDER="0">
  <TR ALIGN="LEFT" VALIGN="TOP">
    <TD ROWSPAN="2" WIDTH="20%">
<A HREF="http://www.sco.com/skunkware">
      <IMG BORDER=0 SRC="/skunkware/pics/gif/skunkware.gif" HEIGHT="80" WIDTH="108" ALIGN="BOTTOM">
</A>
    </TD>
    <TD VALIGN="BOTTOM" WIDTH="79%">
      <IMG ISMAP USEMAP="#skunknav" SRC="/skunkware/pics/gif/skunknav.gif" HEIGHT="26"
          WIDTH="466" ALIGN="BOTTOM" BORDER="0">
    </TD>
  </TR>
  <TR>
    <TD VALIGN="TOP">
      <IMG SRC="/skunkware/pics/gif/${shortname}.gif" ALT="$title" ALIGN="BOTTOM">
    </TD>
  </TR>
</TABLE>

EOF
  } else {
#
# This is the header information for the Skunkware On-line pages
#
    print OUTPUT <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!-- DO NOT EDIT -->
<!-- Automatically generated from $template by $program $version -->
<!-- DO NOT EDIT -->
<HTML>
<HEAD>
  <TITLE>$title</TITLE>
  <LINK REV="made" HREF="$mailto">
</HEAD>
<BODY BGCOLOR="#ffffff">

<MAP NAME="skunknav">
  <AREA SHAPE="rect" COORDS="0,0,132,25" HREF="http://www.sco.com/products">
  <AREA SHAPE="rect" COORDS="133,0,214,25" HREF="http://www.sco.com/">
  <AREA SHAPE="rect" COORDS="215,0,350,25" HREF="http://www.sco.com/comments.html">
  <AREA SHAPE="rect" COORDS="351,0,465,25" HREF="http://www3.sco.com/cgi-bin/webinator">
</MAP>

<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%" BORDER="0">
  <TR ALIGN="LEFT" VALIGN="TOP">
    <TD ROWSPAN="2" WIDTH="20%">
<A HREF="http://www.sco.com/skunkware">
      <IMG BORDER=0 SRC="/skunkware/pics/gif/skunkware.gif" HEIGHT="80" WIDTH="108" ALIGN="BOTTOM">
</A>
    </TD>
    <TD VALIGN="TOP" WIDTH="79%">
      <IMG ISMAP USEMAP="#skunknav" SRC="/graphics1/navbar-head.gif" HEIGHT="26"
          WIDTH="466" ALIGN="BOTTOM" BORDER="0">
    </TD>
  </TR>
  <TR>
    <TD VALIGN="TOP">
      <IMG SRC="/skunkware/pics/gif/${shortname}.gif" ALT="$title" ALIGN="BOTTOM">
    </TD>
  </TR>
</TABLE>

EOF
  }
}

sub skunkfooter {
    my($foo) = @_;

    if ($isweb) {
#
# Print out the footer of a page for the On-line version of the page.
# This should conform to the corporate guidelines.
#
        print OUTPUT <<EOF;
<P><HR ALIGN=LEFT>
<P ALIGN="CENTER">Last Updated: $now</P>
<P><HR ALIGN=LEFT>
<P>&nbsp;<FONT SIZE=-2>
&COPY; Copyright 1997 The Santa Cruz Operation, Inc. All rights reserved.
</FONT>
</BODY>
</HTML>
EOF
    } else {
#
# Print out the fotter for the CD-ROM version.
#
        print OUTPUT <<EOF;
<P><HR ALIGN=LEFT>
<P ALIGN="CENTER">Last Updated: $now</P>
<P><HR ALIGN=LEFT>
<P>&nbsp;<FONT SIZE=-2>
&COPY; Copyright 1997 The Santa Cruz Operation, Inc. All rights reserved.
</FONT>
</BODY>
</HTML>
EOF
    }
}

sub start_packagelist {
    my($foo) = @_;

    print OUTPUT <<EOF;
<H2 ALIGN="CENTER">Package List</H2>
<CENTER><TABLE WIDTH="100%" BORDER="1" CELLSPACING="0" CELLPADDING="2">
  <TR>
    <TH WIDTH="11%" HEIGHT="23">Name</TH>
    <TH WIDTH="62%">Description</TH>
    <TH WIDTH="9%">Version</TH>
    <TH WIDTH="7%">OSR5</TH>
    <TH WIDTH="11%">UnixWare</TH>
  </TR>
EOF
}

sub end_packagelist {
    my($foo) = @_;

    print OUTPUT <<EOF;
</TABLE></CENTER>
<H4><HR ALIGN="LEFT"></H4>
EOF
}

sub startpack {
    my($linkref,$desc) = @_;
    print OUTPUT <<EOF;
<H4><A NAME="$linkref"></A>$desc (package name <I>$linkref</I>)</H4>
<BLOCKQUOTE>
EOF
}

sub endpack {
    my($foo) = @_;

    print OUTPUT <<EOF;
</BLOCKQUOTE>
<P>&nbsp;</P>
EOF
}

sub source {
    my($site) = @_;

    print OUTPUT <<EOF;
<P>
<A HREF="$site">
  <IMG SRC="/skunkware/pics/gif/origsrc.gif" ALT="Original source code" HSPACE="20" BORDER="0"
            HEIGHT="32" WIDTH="160" ALIGN="CENTER"></A>
<A HREF="$site">$site</A></P>
EOF
}

sub homepage {
    my($page) = @_;

    print OUTPUT <<EOF;
<P>
<A HREF="$page">
  <IMG SRC="/skunkware/pics/gif/homepage.gif" ALT="Package Home page" HSPACE="20" BORDER="0"
            HEIGHT="32" WIDTH="160" ALIGN="CENTER"></A>
<A HREF="$page">$page</A></P>
EOF
}

sub osr5dist {
    my($location) = @_;
    my($volfile);
    if ($location =~ /http\:\/\// || $location =~ /ftp\:\/\//) {
        print OUTPUT <<EOF;
<P><A HREF="$location">
  <IMG SRC="/skunkware/pics/gif/osr5dist.gif" HSPACE="20" BORDER="0" HEIGHT="32" WIDTH="160"
  ALIGN="CENTER" ALT="OpenServer Distribution">
$location
</A></P>
EOF
    } else {
    if ($location =~ /osr5\//) {
        if ($location =~ /\.gz$/) {
            $volfile = "$location";
        } else {
          if (not $location =~ /\/$/) {
            $volfile="${location}/VOLS.tar";
          } else {
            $volfile="${location}VOLS.tar";
          }
        }
        print OUTPUT <<EOF;
<P><A HREF="$baseref/$volfile">
  <IMG SRC="/skunkware/pics/gif/osr5dist.gif" HSPACE="20" BORDER="0" HEIGHT="32" WIDTH="160"
  ALIGN="CENTER" ALT="OpenServer Distribution"></A>
<A HREF="$baseref/$location">$location</A></P>
EOF
    } else {
        print OUTPUT <<EOF;
<P><A HREF="$location">
  <IMG SRC="/skunkware/pics/gif/osr5dist.gif" HSPACE="20" BORDER="0" HEIGHT="32" WIDTH="160"
  ALIGN="CENTER" ALT="OpenServer Distribution">
$location
</A></P>
EOF
    }
    }
}

sub uw2dist {
    my($location) = @_;
    my($volfile);
    if ($location =~ /http\:\/\// || $location =~ /ftp\:\/\//) {
        print OUTPUT <<EOF;
<P><A HREF="$location">
  <IMG SRC="/skunkware/pics/gif/uw2dist.gif" HSPACE="20" BORDER="0" HEIGHT="32" WIDTH="160"
  ALIGN="CENTER" ALT="UnixWare Distribution">
$location
</A></P>
EOF
    } else {
        print OUTPUT <<EOF;
<P><A HREF="$baseref/$location">
  <IMG SRC="/skunkware/pics/gif/uw2dist.gif" HSPACE="20" BORDER="0" HEIGHT="32" WIDTH="160"
  ALIGN="CENTER" ALT="UnixWare Distribution"></A>
<A HREF="$baseref/$location">$location</A></P>
EOF
    }
}

sub packlist{
    my($foo) = @_;
    my($linkref,$desc,$packver,$isosr5,$isuw2) = split(/;/, $foo);

    print OUTPUT <<EOF;
<TR>
  <TD><A HREF="#$linkref">$linkref</A></TD>
  <TD>$desc</TD>
  <TD>$packver</TD>
  <TD ALIGN="CENTER">$isosr5</TD>
  <TD ALIGN="CENTER">$isuw2</TD>
</TR>
EOF
}

LINE: while (defined($line = <TEMPLATE>)) {
    next LINE if $line =~ /^# [0-9]* \"/;
    chop $line;
    if ($line =~ s/@@\@SKUNKHEADER\((.*)\)//) { skunkheader($1);}
    elsif ($line =~ s/@@\@SKUNKFOOTER//) { skunkfooter();}
    elsif ($line =~ s/@@\@START_PACKAGELIST//) {start_packagelist();}
    elsif ($line =~ s/@@\@END_PACKAGELIST//) {end_packagelist();}
    elsif ($line =~ s/@@\@STARTPACK\((.*);(.*)\)//) {startpack($1,$2);}
    elsif ($line =~ s/@@\@ENDPACK//) {endpack();}
    elsif ($line =~ s/@@\@OSR5DIST\((.*)\)//) {osr5dist($1);}
    elsif ($line =~ s/@@\@UW2DIST\((.*)\)//) {uw2dist($1);}
    elsif ($line =~ s/@@\@SOURCE\((.*)\)//) {source($1);}
    elsif ($line =~ s/@@\@HOMEPAGE\((.*)\)//) {homepage($1);}
    elsif ($line =~ s/@@\@PACKLIST\((.*)\)//) {packlist($1);}
    else {print OUTPUT "$line\n";}
}
