#!/usr/bin/perl

##   #! /usr/local/pkg/bin/perl

$filepath = "./";
$destpath = $filepath . "../";
$footer = $filepath . "../../UTIL/footer.txt";

while( $ARGV[0] =~ /^-/ )
{
    if ( $ARGV[0] =~ /^-h/ )
    {
	print <<EOF;
    maker.pl [-help | -free] <file list>
            File list is list of files.  If empty, it does all .html 
        files in "$filepath".
	    -help    gives this
	    -free    lists out all files which are not linked
	    -loop    lists out all files which have no loop
EOF
        exit( 0 );
    }
    elsif ( $ARGV[0] =~ /^-f/ )
    {
	print "Printing all unlinked files\n";
	$print_free = 1;
    }
    elsif ( $ARGV[0] =~ /^-l/ )
    {
	print "Printing all files not in the big loop\n";
	$print_free = 0;
	$print_loop = 1;
    }
    else 
    {
	print "Unknown command line option $ARGV[0]\n";
	exit( 1 );
    }
    shift( @ARGV );
}

$num_files = $#ARGV + 1;
if ($num_files == 0) {
    print "Doing all files\n"
}
else {
    print "Doing the following $num_files file(s):\n";
    foreach $student (@ARGV) {
	print "    $student\n";
    }
}
print "\n";

# get all files
opendir(THISDIR, "$filepath");
@allfiles = grep(/[^\., ^tmp].*.html$/, readdir(THISDIR));
closedir(THISDIR);
#@allfiles = sort @allfiles;


if ( ! $print_free )
{
    if ( ! $print_loop ) {
	print "Making html header and footers for humor maze\n";
	print "   putting new files in $destpath\n";
    } else {
	print "Detecting files without loops\n";
    }

    # Get the list/loop structures up
    &MakeLoop( "stack.list" );
}


foreach $dog (@allfiles) {
    $cur_file = $dog;
    if ($num_files > 0) {
	@search = grep(/$cur_file/, @ARGV);
    }
    
    if (($num_files == 0) || ($#search > -1)) {
	print "Working on $cur_file"  if ( ! $print_free && ! $print_loop );

	&StatFile( $cur_file );
	&FixFile( $cur_file ) if ( ! $print_free && ! $print_loop );

	# do loop detection
	&AddLoop( OUT, $cur_file ) if ( $print_loop );
    }
}

print "Done with all files\n";




#
#  Subroutines
#



#####
#
#     Get number of outgoing links
#     Find and print out all links from other files to this file
#
#####
sub StatFile
{
    $file = shift( @_ );

#    print "  Statting $file\n";
#     Get number of outgoing links
    ($title) = $file =~ /^(.*).html$/;
    #    print "got the title of $title\n";
    open( TEMP, $title . ".html" );
    @lines = <TEMP>;
    close( TEMP );
    @lines = grep( /<a href=\"/, @lines );
    #    print "\n", @lines, "\n";
    print " ( ", $#lines + 1, " )\n" if ( ! $print_free && ! $print_loop );



#     Find and print out all links from other files to this file
    # get all files
    opendir(THISDIR, "$filepath");
    @allfiles = grep(/[^\., ^tmp].*.html$/, readdir(THISDIR));
    closedir(THISDIR);
    $hook = "<a href=\"" . $title . ".html\"";
    #    print "The hook is " . $hook . "\n";
    $linked = 0;
    $linklist = "";
    foreach $pig (@allfiles) {
	open( TEMP, $pig );
	@lines = <TEMP>;
	close( TEMP );
	@lines = grep( /$hook/, @lines );
	if ( $#lines != -1 )
	{
	    ($ftitle) = $pig =~ /^(.*).html$/;
	    $linklist .= $ftitle . ", ";
            #print "     " . $ftitle . " ( ", 1 + $#lines, " )\n";
	    $linked++;
	}
    }
    if ( $linked == 0 ) {
	if ( $print_free ) 
	{
	    print "$file\n";
	}
	elsif( ! $print_loop )
	{
	    print "     ***** UNLINKED FILE *****\n";
	}   
    }
    else {
	chop( $linklist );
	chop( $linklist );
	print "     links: ", $linklist, "\n"  if ( ! $print_free && ! $print_loop );
    }
}


#####
#
#     Make the new .html file
#
#####
sub FixFile
{
    local( $ffile ) = shift( @_ );
    ($title) = $ffile =~ /^(.*).html$/;
#    print "  Fixing $file ($title)\n";
    #    print "Now creating $ffile \n";
    open( NEW, ">$destpath$ffile" );
    
    print NEW <<EOF;
<html>
<head>
<title>Humor Maze ($title)</title>
</head>
<body>
<h2 align=center>Humor Maze ($title)</h2>
EOF
    # put in loop headers
    &AddLoop( NEW, $ffile );

    print NEW "\n<pre>\n";

    # copy over the file
#    print "Continuing to copy $title to the thing\n";
    open( FROM, "$filepath$ffile" );
    while( <FROM> )
    {
	if ( !( $_ =~ /\@/ ) ) {
	    print NEW $_;
	}
    }
    close( FROM );

    print NEW "\n\n                   <a href=\"index.html\">] back to entrance [</a></pre>\n";

    open( FROM, "$footer" );
    while( <FROM> )
    {
	print NEW $_;
    }
    close( FROM );

    print NEW <<EOF;
</body>
</html>
EOF
    close( NEW );

    # done with the file
}



# Read in a loop file and make a list for the looping
sub MakeLoop
{
    local( $filename ) = @_;
    local( $prev, $first );

    open( I_FILE, $filename );

    chop( $loop_name = <I_FILE> );
    chop( $first = <I_FILE> );

    $prev = $first;
    while( <I_FILE> )
    {
	chop( $_ );
	$loop{ $prev } = $_;
	$prev = $_;
    }
    $loop{ $prev } = $first;

    close( I_FILE );
}

# print out a loop link to the file passed.
# if there is no loop link then print message to console.
sub AddLoop
{
    local( $file, $filename ) = @_;
    local( $link );
    
    $link = $loop{ $filename };
    if ( $print_loop )
    {
	print $filename, "\n" 	if ( $link eq "" );
    }
    else
    {
	if ( $link ne "" )
	{
	    print $file "<a href=\"$loop{ $filename }\">$loop_name</a>\n";
	}
	else
	{
	    print "    **** UNLOOPED FILE ****\n";
	}
    }
}

