#!/usr/bin/perl

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

########################################################################
### Copyright (C)  Tripodics.com 
### Author:	Bruce A. Martin
### File Name:	display.pl
### Versions:
###	0.3	(Don't show .pl source code.)
###	0.2	bam:2004/08/03	Add PNG, also link for DIR.
###	0.1	bam:2004/02/22	Extract description from HTML 
###	0.0	bam:2003/11/20	
#########################################################################

use strict;
use CGI qw/:standard/;

print header;				# html format is permitted to output.
my $NL = "\n"; 
my $Q = '"'; 


### 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 . "head.html";
my $tail = $path . "tail.html";


### Main processing
# 

        &show( $head );

	&showdir( $dirname );

print "<BR>TAIL:<BR>";

        &show( $tail );

print "<HR>";

	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 "#### allfiles= @allfiles <BR>";

	print "<UL>\n";

	my $f;
	my $max=$maxlines;
	my $next;
        foreach $name ( @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"; }

		## Special cases for html, gif, etc.
		if ( $f =~ /\.html*$/i ) {
			print '<FONT COLOR=#660000>';
			&shownotags( $f, $max );
			print '</FONT>';
		}
		elsif ( $f =~ /\.pl*$/i ) {
			#--	print '<FONT COLOR=#006600>';
			#--	&showtext( $f, $max );
			#--	print '</FONT>';
		}
		elsif ( $f =~ /\.png$/i ) {
			print "png: "; 
			print '<A HREF="' . $f . '" TARGET=_blank>';
			print '<IMG SRC="' . $f . '" HSPACE=6 ALIGN=TOP>';
			print '</A>';
		}
		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 " <A HREF=$Q#DIRNAME=$f$Q>"; 
			print " -- DIRECTORY"; 
			print "</A>\n";
			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;
print $lines;
	## Display without tags ##
        open(F,$f) or print "Cannot open $f";
	print "<OL><PRE>";
	while(<F>){
			if ( /<META.*TYPE=["]*BAM/i )
			{
				print "<BR> +++++ <BR>\n";
			}
			if ( /<TITLE/i )
			{ 
				s/<[^>]*>*//g;	# Suppress tags.
				print "<LI> TITLE:   $_<BR>\n";
			}
		s/<[^>]*>*//g;	# Suppress tags.
		next if ( /^\s*$/ );
		print "$_";
		$lines--;
		if ($lines < 0 )
		{
			print ". . .";
			last;
		}
        }
	print "</PRE></OL>\n";
        close(F);
}

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

