#!/usr/local/bin/perl
#
# OpenBSD Package to Web Page Converter
# -------------------------------------
#
# This script generates web pages by extracting the long and short
# definitions from OpenBSD package files (.tgz files).
# Info: Ryan Walker (walkerr@ssimicro.com)
# Mangled by Bob Beck (beck@openbsd.org)
# 
# commited as working on ftp.openbsd.org (so /usr/local/bin/perl isn't a typo)
# remember this has to work on non openbsd platforms.
#
# Define these variables to fit your config
# $pkg_root -- root directory of packages to build, should contain arch
# $web_root -- main web directory, index.html created in this directory
# $tmp      -- temporary directory, obviously must be writable
# $version  -- version number we are building for, ie "2.7" (only used for
# ftp1 & 2  -- self explanatory, used for building FTP links

$pkg_root="/afs/ualberta.ca/sunsite/ftp/pub/OpenBSD/2.8/packages";
$web_root="/usr/sunsite02/home/beck/www";
$tmp="/usr/sunsite02/home/beck/tmp";
$version="2.8";
$ftp1="ftp://ftp.openbsd.org/pub/OpenBSD/$version";
$ftp2="ftp://ftp1.usa.openbsd.org/pub/OpenBSD/$version";

# OK, let's get started.
# verifyvars just checks to make sure all the variables specified
# make sense.  (In that the directories exist, are writable, etc.)

&verifyvars;
&getdata;

system "rm -rf $web_root/*";
system "mv $tmp/build/* $web_root";
system "rm -rf $tmp/*";

sub getdata {
        opendir ROOT, "$pkg_root" or die "Could not open $pkg_root
directory!";
        @array=(readdir(ROOT));
        open(INDEXFILE,">$tmp/build/index.html");

        print INDEXFILE "<html><title>OpenBSD Packages - Architecture
Selection for OBSD $version</title>
        <body BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#23238E\">
        <img src=http://www.openbsd.org/images/smalltitle.gif><br><br>\
        <h2><font color=\"#e00000\">Packages - Architecture Selection for
OpenBSD $version</font></h2>
        Please select the architecture for which you wish to download a
package.<br><br><center><h2>";

        foreach $dir (@array) {
          if ($dir ne ".." and $dir ne "." and $dir ne "index.txt")
          {
            print INDEXFILE "<a href=$dir.html>$dir</a>\n";
            system "mkdir $tmp/build/$dir";
                  opendir PACKAGES, "$pkg_root/$dir" or die "Could not open
$dir dir.";
                  @contents=(readdir(PACKAGES));


            open (ARCHINDEX, ">$tmp/build/$dir.html");

            &header;
                  foreach $line (@contents) {
                        if ($line ne ".." and $line ne ".") {&body;}

}

#FOOTER HERE
print ARCHINDEX "</table><hr></body></html>";
}
                              }
           }

#FOOTER HERE
print INDEXFILE "<br><br><hr></table></body></html>";


sub header {print ARCHINDEX "<html><body BGCOLOR=\"#FFFFFF\"
TEXT=\"#000000\" LINK=\"#23238E\"><title>OpenBSD
Packages ($version/$dir)</title>\n
<img src=http://www.openbsd.org/images/smalltitle.gif><br><br><font
color=#e00000><h2>Packages</h2></font>
The following table is a listing of the packages currently available for
OpenBSD <font
color=#e00000><b>$version</b></font> on the <font
color=#e00000><b>$dir</b></font> platform. Make sure you've got
the right version and platform -- chaos will ensue if you are in the wrong
area.

<br><br><hr>

<table>\n\n";}

# This subroutine generates the HTML code (and yanks decription files) for
# each per/arch package page..

sub body {

    # Extracting desc. files from each .tgz package
    if ($line =~ /\.tgz$/ ) {
    print  "doing $dir/$line\n";
    system "cp $pkg_root/$dir/$line $tmp/tar";
    system "gunzip $tmp/tar/$line";

    # Hack to get proper filename for tar..
    $entrytar=$line;
    $entrytar=~s/.tgz/.tar/;

    # Extract our Description files..
    system "tar xvf $tmp/tar/$entrytar -C $tmp/tar \"+DESC\" \"+COMMENT\"";


    open(LONGDESC, ">$tmp/build/$dir/$line-long.html");
    print LONGDESC "<html><title>OpenBSD Package Details - $line</title>
    <body BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#23238E\">
    <img src=http://www.openbsd.org/images/smalltitle.gif>
    <br><br><h2><font color=#e0000>Package Information for $line
($dir)</font></h2>
    [ <a href=$ftp1/packages/$dir/$line>FTP 1</a> ]\n
    [ <a href=$ftp2/packages/$dir/$line>FTP 2</a> ] [ <a
href=$line-contents.html>Package Contents</a> ]

    <br><br><hr><br><pre>";close(LONGDESC);
    system "cat $tmp/tar/\"+DESC\" >>$tmp/build/$dir/$line-long.html";
    open(LONGDESC, ">>$tmp/build/$dir/$line-long.html");
    print LONGDESC "</pre><hr>";
    print LONGDESC "</body></html>";
    close(LONGDESC);


    system "cp $tmp/tar/\"+COMMENT\" $tmp/build/$dir/$line-short.html";

    # Generate a contents listing from the tar file, copy it to build
    open(CONT, ">$tmp/build/$dir/$line-contents.html");
    print CONT "<html><title>Package contents for $line ($dir)</title>

    <body BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#23238E\">
    <img src=http://www.openbsd.org/images/smalltitle.gif><br><br>
    <h2><font color=\"#e00000\">
    Package contents for $line ($dir)</font></h2><hr><pre>"; close(CONT);
    system "tar tf $tmp/tar/$entrytar >> $tmp/build/$dir/$line-contents.html";
    open(CONT, ">>$tmp/build/$dir/$line-contents.html"); print CONT
"</pre><hr></body></html>";

#FOOTER HERE

    close(CONT);

    # Toast the temporary tar directory for the next iteration
    system "rm -rf $tmp/tar/*";

    open(SHORTDESC, "$tmp/build/$dir/$line-short.html");
    print ARCHINDEX "<tr><td><b><a
href=$dir/$line-long.html>$line</a></b></td><td>";
    foreach $zz (<SHORTDESC>) {print ARCHINDEX "&nbsp&nbsp&nbsp
<i>$zz</i>";}
    print ARCHINDEX "</td><td>[ <a href=$ftp1/packages/$dir/$line>FTP Site
1</a> ]</td><td>
    [ <a href=$ftp2/packages/$dir/$line>FTP Site 2</a> ]</td></tr>\n";
    }
}

sub verifyvars {
        system "clear";
        print "OpenBSD PackageSucker\n*********************\n\n";
        print "starting directory checks...\n\n";
        if (-d $pkg_root) {print "ok - pkg_root exists\n";} else {die
"ERROR pkg_root does not exist!;";}
        if (-w $tmp) {print "ok - tempdir is accessible\n";} else {die
"ERROR tmp not writable!";}
        if (-w $web_root) {print "ok - webroot is accessible\n";} else {die
"ERROR web_root is not writable!"}
        print "\ngo - toasting temp directory"; system "rm -rf $tmp/*";
print "\n";
        system "mkdir $tmp/build"; system "mkdir $tmp/tar";
}





