#!/usr/sbin/perl

## Copyright 1995, Silicon Graphics, Inc.
## All Rights Reserved.
##
## This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
## the contents of this file may not be disclosed to third parties, copied or
## duplicated in any form, in whole or in part, without the prior written
## permission of Silicon Graphics, Inc.
##
## RESTRICTED RIGHTS LEGEND:
## Use, duplication or disclosure by the Government is subject to restrictions
## as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
## and Computer Software clause at DFARS 252.227-7013, and/or in similar or
## successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
## rights reserved under the Copyright Laws of the United States.

#
#	webdist
#
#	Generates a Web Software Distribution page for a Inst
#	distribution directory.
#
#	Usage:
#
#	    webdist distdir 
#
#	    distdir	Distribution directory path
#

$scriptpath = "/cgi-bin/webdist.install.cgi";
chop($disthost = `hostname`);
chop($disthost = `/usr/sbin/canonhost $disthost`);

#
# Check argument count and input parameters.  
#

if ($#ARGV != 0) {
    print "Usage: $0 distdir\n";
    exit 1;
}

$distdir = @ARGV[0];
if (!-r $distdir) {
    print STDERR "Error: Cannot read from distribution directory $distdir!\n";
    exit 1;
}
if (!-d $distdir) {
    print STDERR "Error: $distdir is not a directory!\n";
    exit 1;
}


#
# Check the distribution directory for product files and product descriptions
# and set up the following arrays:
#
#    idbfiles contains the paths for the idb files 
#    prodfiles contains the paths to the product spec files.
#    prodnames contains the product names
#    proddescs contains the product descriptions
#

@idbfiles = <$distdir/*.idb>;

foreach $i (@idbfiles) {

    # Figure out the product file name
    chop($dirname = `dirname $i`);
    chop($prodname = `basename $i .idb`);
    $prodfile = $dirname . "/" . $prodname;

    @showprodout = `/usr/sbin/showprods -v -f $distdir $prodname 2>&1`;

    if ($? != 0) {
	print STDERR 
	      "Warning: Error getting description for product file $prodfile:@showprodout\n";

    } elsif ($#showprodout < 1) {

	print STDERR 
	      "Warning: Could not get description for product file $prodfile\n";

    } elsif ($showprodout[1] =~ /^ERROR/) {
	      "Warning: Error getting description for product file $prodfile:@showprodout\n";
    } else {

	# successfully ran showprod on the product file
	# Save the product name and product file path

	push(@prodnames, $prodname);
	push(@prodfiles, $prodfile);

	# Get the product description

	$descline = $showprodout[2];

	$where = index($descline, "\"") + 1;
	$descline = substr($descline, $where);

	$where = index($descline, "\"");
	$descline = substr($descline, 0, $where);

	push(@proddescs, $descline);
    }
}


if ($#prodnames < 0) {
    print STDERR "No products found at $distdir.\n";
    exit 1;
}


# 
# Write the HTML for the Web Installation Page
#

print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>$disthost:$distdir</TITLE>\n";
print "</HEAD>\n";

print "<BODY BACKGROUND=\"/webdist/images/background.jpeg\">\n";
print "<P ALIGN=\"CENTER\">";
print "<IMG SRC=\"/webdist/images/dist.gif\">";
print "</P>";

print "<H3 ALIGN=\"CENTER\">$disthost:$distdir</H3>\n";

print "<P ALIGN=\"CENTER\">";
print "<IMG SRC=\"/webdist/images/bar.gif\">";
print "</P>";

print "<H2>Installation Instructions</H2>\n";
print "Select the products you want to install and press \"Install Software\" to start the installation.\n";
print "<FORM METHOD=\"POST\" ACTION=\"$scriptpath\">\n";
print "<H2>Software Products</H2>\n";
print "<UL>\n";

for ($i = 0; $i <= $#prodnames; $i++) {
    $prodname = $prodnames[$i];
    $proddesc = $proddescs[$i];
    if ($proddesc eq "") {
	$proddesc = $prodname;
    }
    print "<LI> <input type=\"checkbox\"  name =\"$prodname\"> $proddesc\n";
}

print "</UL>\n";
print "<H2>Installation Options</H2>\n";
print "<UL>\n";
print "<LI> <input type=\"radio\" name=\"options\" value=\"immediate\" checked> Start the installation immediately.\n";
print "<LI> <input type=\"radio\" name=\"options\" value=\"delay\"> Let me look at my selections before installing.\n";
print "</UL>\n";
print "<input type=\"submit\" value=\"Install Software\">\n";
print "<input type=\"reset\" value=\"Clear Selections\">\n";
print "<input type=\"hidden\" name=\"products\" value=\"";
foreach $i (@prodnames) {
    print "$i ";
}
print "\">\n";
print "<input type=\"hidden\" name=\"distdir\" value=\"$disthost:$distdir\">\n";
print "</FORM>\n";
print "</BODY>\n";

exit 0
