#!/usr/bin/perl

### Display all (or selected) files in directory.  (+++ subdirs, too?)
### Display TITLE/ABSTRACT info.
### head.html and tail.html are also displayed.

########################################################################
### Copyright (C)  Tripodics.com 
### Author:	Bruce A. Martin
### File Name:	abstract.pl
### Versions:
###	1.9	bam:2009/06/04	Bugler
###	1.6	bam:2007/07/23	Remove heading.  (Use head.html)
###	1.5	bam:2006/01/20	Move head & tail to format/
###	1.4	bam:2004/02/27	PIX
###	1.3	bam:2004/02/22	HTML only.
###	1.2	bam:2004/02/22	Use tighter Regexp control.  (Rename "abstract.pl")
###	1.1	bam:2004/02/22	Extract descriptions from HTML 
###	0.0	bam:2003/11/20	
#########################################################################

use strict;
use CGI qw/:standard :newtag/;

#??	use "_getargs.txt";

my $NL = "\n"; 
my $Q = '"'; 

print header;				# html format is permitted to output.

#### Try some tricks, from 
html();
#--	head( title( "Abstracts" ) ); 
print "<head><title> Essays </title></head>\n";
print "<body bgcolor=#CCFFFF>\n";

my $x;
#print h1( {-align=>'right'}, "Goodbye" );

#print "<UL>";
#foreach $x ( @INC )
#{
	#print "<LI>$x: &nbsp;&nbsp;";
	#my $s = `ls $x`;
	#print "<FONT SIZE=-1 COLOR=#336600>";
	#print "$s</FONT>";
#}
#print "</UL>\n";

### Get parameters from html form.
#
my %in_para;
foreach (param()){
       	$in_para{$_} = join " ", param($_);
	# Multiple-values (checkbox) joined with blanks.
} 


#### Set prefix to path. ####
my $name = $ENV { 'COMPUTERNAME' } ;
my $path = "";

#### Set file names. ####
my $DIRNAME = $in_para{DIRNAME};
my $dirname = $path . "./";
if ($DIRNAME) { $dirname = $path  . $DIRNAME . "/"; }

my $MAXLINES = $in_para{MAXLINES};
my $maxlines = $MAXLINES;
if ($maxlines < 1) { $maxlines = 10; }


my $head = $path . "format/head.html";
my $tail = $path . "format/tail.html";


### Main processing
# 

        &show( $head );

	&showdir( $dirname );

        &show( $tail );


	my $s='';
	$s .= '<FORM ACTION="#">';
	$s .= '<BR><INPUT NAME="DIRNAME"> Directory.';
	$s .= '<BR><INPUT NAME="MAXLINES" SIZE=20> lines.';
	$s .= '<BR><INPUT TYPE=SUBMIT>';
	$s .= '</FORM>';

	#+++++	print $s;

exit;


########  The following subroutines are from the Tripodics Library. ########
#### Copyright 1996-2002 by Tripodics, All Rights Reserved.             ####
#### Tripodics Computing Service, POBox 456 Middle Island, NY 11953     ####
#+++   (Nonexclusive usage granted to PhDsigns, Bay Shore, NY.)     +++#
############################################################################

################################### show.pl
# Print contents of an HTML file.
#

sub show( $f )
{
	my $f = $path . @_[0];
        open(F,"$f") or return 0;
        while(<F>){
			print; 
        }
        close(F);
}

sub showdir( $d )
{
  my $d = $path . @_[0];
  my @allfiles;

        opendir(D,$d) or return 0;
	@allfiles = grep !/^\./, readdir D;
	closedir D;

#print "<LI>#### allfiles= @allfiles <BR>";

	print "<UL>\n";
#--print "<H2 ALIGN=CENTER> <FONT COLOR=#000066> Essays by <I>???? ????</I> </FONT></H2>\n";
print "<TABLE BGCOLOR=#FFFFCC BACKGROUND=images/parchment.jpg BORDER=1 CELLPADDING=6 ALIGN=CENTER>";

	my $f;
	my $max=$maxlines;
	my $next;
        foreach $name ( sort @allfiles ) {
		my $f = $path . $dirname . $name;
		#-- my $fn=  fileno $f ;

		#print '<LI>';
		#print '<A HREF="' . $f . '" TARGET=_blank>';
		#print "$f";
		##--print "<U>$f</U> \n";
		#print '</A>';
		### Display file name and info ##
		#if ( -z $f ) { print "ZERO SIZE"; }

if ( $f =~ /index.htm/i ) {next;}
		## Special cases for html, gif, etc.
		if ( $f =~ /\.html*$/i ) {
			#print '<P><LI>';
print "<TR>";
print "<TD>";
			print '<A HREF="' . $f . '" TARGET=_blank>';
my $ff=$f;
$ff =~ s/^\.\///;
print '<FONT FACE="sans-serif" SIZE=-2 COLOR=#660000>';
print "$ff";
print "</FONT>";
			#print "$f";
			#--print "<U>$f</U> \n";
			print '</A>';
			## Display file name and info ##
print "<TD>&nbsp;";
			print '<FONT COLOR=#660000>';
			&shownotags( $f, $max );
			print '</FONT>';
print "</TD>";
		}

#		elsif ( $f =~ /\.pl*$/i ) {
#			print '<FONT COLOR=#006600>';
#			&showtext( $f, $max );
#			print '</FONT>';
##		}
#		elsif ( $f =~ /\.gif$/i ) {
#			print "GIF: "; 
#			print '<A HREF="' . $f . '" TARGET=_blank>';
#			print '<IMG SRC="' . $f . '" HSPACE=6 ALIGN=TOP>';
#			print '</A>';
#		}
#		elsif ( $f =~ /\.jp[e]*g$/i ) {
#			print "JPEG: "; 
#			print '<A HREF="' . $f . '" TARGET=_blank>';
#			print '<IMG SRC="' . $f . '" HSPACE=6 ALIGN=TOP>';
#			print '</A>';
#		}
#		## Check file type; display if text.
#		elsif ( -T $f ) {
#			&showtext( $f, $max );
#		}
#		elsif ( -d $f ) {
#			print " -- DIRECTORY"; 
#			my $dd = $path . $f;
#			my @ddfiles; 
#			opendir(DD,$dd) or print ' [[[[ Cannot open DIR. ]]]]';
#			@ddfiles = grep !/^\./, readdir DD;
#			closedir DD;
#			print "<BR> @ddfiles <BR>";
##++ font.
#		}
#		elsif ( -l $f ) { print " -- SYMLINK"; }
#		else { 
#			print "<BR>&nbsp;&nbsp;&nbsp;&nbsp;[[[[ $f is not a text file ]]]]\n"; 
#			print "++++  $f<BR>\n"; &showtext( $f, $max ); 
#			#--	print "<HR>\n";
#		}
	}
	print "<\UL>\n";
}

sub showtext( $f, $max )
{
  #--	my $f = $path . $dirname . @_[0];
  my $f = @_[0];
  my $maxlines = @_[1];
			## Display text file contents ##
        		open(F,$f) or print "Cannot open $f";
			print "<OL><PRE>";
			while(<F>){
				s/</\&lt;/g;	# Suppress tags.
				print "<LI>$_";
				if ($. >= $maxlines)
				{
					print ". . .";
					last;
				}
        		}
			print "</PRE></OL>\n";
        		close(F);
}

sub shownotags( $f, $max )
{
  #--	my $f = $path . $dirname . @_[0];
  my $f = @_[0];
  my $maxlines = @_[1];
  my $lines = $maxlines;


	## Display without tags ##
	
	my $TAG = "(ABSTRACT|DISPLAY|INDEX)";		# Tags to search for.	+++ fix this!
	my $mode =0;					# Display mode is OFF.
	#
        open(F,$f) or print "Cannot open $f";
	#print "<OL><PRE>";
	while(<F>){

	    if ( /<PIX/i ) 
	    { 
		s/<[^>]*>//g;	# Suppress tags.
		s/^\w*//;
		s/\w*#//;
		print '<A HREF="' . $f . '">';
		print '<IMG BORDER=0 ' . $_ . '</IMG ></A>';
		print "\n";
	    }
	    if ( /<TITLE/i ) 
	    { 
		s/<[^>]*>//g;	# Suppress tags.
		s/^\w*//;			#???? BUG, HERE:  Must add a blank before title!!!
		s/\w*#//;
		print " &nbsp;&nbsp; &nbsp;&nbsp;";
		print '<A HREF="' . $f . '" TARGET=_blank>';
		print "$_";
		print '</A>';
		print " &nbsp;&nbsp; &nbsp;&nbsp;";
	    }
	    

	    if ( /<META.*TYPE=[\*]*BAM/i )
	    {
		print "<BR> +++++ <BR>";
	    }

		#### Display whatever (HTML) is found 
		# within <ABSTRACT> ... </ABSTRACT> tags.
		# (also DISPLAY, INDEX, etc.
		#
		if (! $mode) {
		  #--if ( /^.*<(ABSTRACT|DISPLAY|INDEX)[^>]*>/i ) {	# Tag found
		  $TAG = "ABSTRACT";
		  if ( /^.*<\Q$TAG\E[^>]*>/i ) {	# Tag found
			s///i;						# Discard tag.
			print "<UL><FONT COLOR=#${Q}006600$Q>";
			$mode = 1;					# Display mode ON.
		  }
		  $TAG = "DISPLAY";
		  if ( /^.*<\Q$TAG\E[^>]*>/i ) {	# Tag found
			s///i;						# Discard tag.
			print "<UL><FONT COLOR=#${Q}003333$Q>";
			$mode = 1;					# Display mode ON.
		  }
		  $TAG = "INDEX";
		  if ( /^.*<\Q$TAG\E[^>]*>/i ) {	# Tag found
			s///i;						# Discard tag.
			print "<UL><FONT COLOR=#${Q}333333$Q>";
			$mode = 1;					# Display mode ON.
		  }

		}
		if ($mode) {		# (NOTE:  Not elsif !!) #
		  #--if ( /<\/(ABSTRACT|DISPLAY|INDEX)[^>]*>.*$/i ) {	# Closing tag.
		  $TAG = "ABSTRACT";
		  if ( /<\/\Q$TAG\E[^>]*>.*$/i ) {	# Closing tag.
			s///i;						# Discard.
			$_ .= "</FONT></UL>\n";					# (Add BR at the end.)
			$mode = 0;					# Display mode OFF.
		  }
		  $TAG = "DISPLAY";
		  if ( /<\/\Q$TAG\E[^>]*>.*$/i ) {	# Closing tag.
			s///i;						# Discard.
			$_ .= "</FONT></UL>\n";					# (Add BR at the end.)
			$mode = 0;					# Display mode OFF.
		  }
		  $TAG = "INDEX";
		  if ( /<\/\Q$TAG\E[^>]*>.*$/i ) {	# Closing tag.
			s///i;						# Discard.
			$_ .= "</FONT></UL>\n";					# (Add BR at the end.)
			$mode = 0;					# Display mode OFF.
		  }
		  print;						# Now, print what's left.
		}
		  
		

#	    else {
#		s/<[^>]*>*//g;	# Suppress tags.
#		next if ( /^\s*$/ );
#		print "$_";
#		$lines--;
#		if ($lines < 0 )
#		{
#			print ". . .";
#			last;
#		}
#	    }
        }
	print "</PRE></OL>\n";
        close(F);
}

##############################  End of bamlib.inc  #############################

