#!/usr/bin/perl
#
#   cvs_modules - This program knows how to read a CVS modules file and 
#                   writes that information out as the rows of an HTML
#                   table.
#
#	Copyright (C) 2002 Arizona Board of Regents on behalf of the
#   Planetary Image Research Laboratory, Lunar and Planetary Laboratory
#   at the University of Arizona
#
#   CVS $Id: cvs_modules,v 1.2 2002/02/17 00:46:53 rbeyer Exp $
#
#
#   License & Copyright Information
#   -------------------------------
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
#   Program Description
#   -------------------
#
#   This file was written alongside CVS 1.11, and may have
#   issues (but probably not) with later versions.
#
#   In order for this Perl program to work on your system, you 
#   may have to change the path to the Perl executable on the
#   first line of this program.  
#
#   This program basically just reads your CVS modules file
#   and writes out the contents as the rows of an HTML table.
#   This program is intended to be called from a Server Side
#   Include "exec" directive.  An example of an sHTML file
#   that uses this program should have been distributed with
#   this file.
#

use Getopt::Std;

# This should be the location of your $CVSROOT/CVSROOT/modules file.
my $modules_file = "/opt/local/src/ICL/CVS_Repository/CVSROOT/modules";

my $usage = qq{cvs_modules Usage:
    cvs_modules
        -m <path to the CVS modules file>

    -m If the <path to the CVS modules file> is not specified, its
        default value is $modules_file.};

# Parse command line arguments
my %option = ();
unless( getopts ('m:', \%option) ) {die "$usage\n";}

if( $option{m} )
    {
    $modules_file = $option{m}
    }

unless( -r $modules_file )
    {
    die "Your CVS modules file, $modules_file, was not readable.\n$usage\n";
    }

my @mname;
my %module_options;
my %aliases;
my %type_information;

# HTML for the various types of CVS modules.
my $alias_module = qq{<a href="http://www.cvshome.org/docs/manual/cvs_18.html#SEC156">Alias Module</a>};
my $regular_module = qq{<a href="http://www.cvshome.org/docs/manual/cvs_18.html#SEC157">Regular Module</a>};
my $ampersand_module = qq{<a href="http://www.cvshome.org/docs/manual/cvs_18.html#SEC158">Ampersand Module</a>};


# Read in the contents of the modules file.
open( MODULES, "< $modules_file" )
    or die "Can't open $modules_file for reading. !$\n";
until( eof MODULES )
    {
    my $read_line = <MODULES>;
    if( ($read_line =~ /^#/) or ($read_line =~ /^\W*$/) ) {next};
    chomp( $read_line );
    while( $read_line =~ /\\$/ )
        {
        $read_line =~ s/\\//g;
        my $continued_read_line = <MODULES>;
        chomp( $continued_read_line );
        $read_line .= " $continued_read_line";
        }

    # We're going to hijack @ARGV so that we can take advantage of getopts.
    @ARGV = ();
    my @modules_line = split(/\s+/, $read_line);
    my $key = shift @modules_line;
    push @mname, $key;

    @ARGV = @modules_line;
    my %modules_options;
    getopts('i:o:e:t:u:d:la', \%modules_options);
    my $values = join(" ", @ARGV);
    $aliases{$key} = $values;

    foreach $element (@ARGV)
        {
        pop( @modules_line );
        }
    $module_options{$key} = join(" ", @modules_line);

    # Now determine the module type;
    if( $modules_options{a} )
        {
        $type_information{$key} = $alias_module;
        next;
        }
    if( $values =~ /&/g )
        {
        $type_information{$key} = $ampersand_module;
        next;
        }
    $type_information{$key} = $regular_module;
    }

# Now that we have all the information, let's write it out as the
#   elements of an HTML table.
my $module;
foreach $module ( @mname )
    {
    print 
qq(<tr><td>$module</td>
    <td>$module_options{$module}</td>
    <td>$aliases{$module}</td>
    <td>$type_information{$module}</td></tr>
);
    }

exit(0);
