#!/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:	select.pl
### Versions:
###	1.6	bam:2009/06/04	Bugler
###	1.5	bam:2004/06/23	Rename:  select.pl - display list of files; click to modify
###	1.4a	bam:2004	(Remove title, etc.)
###	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 by John Bugler </title></head>\n";
print "<body bgcolor=#CCFFFF>\n";

my $x;


### 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};
if ($DIRNAME =~ /^$/) { $DIRNAME="."; }
my $dirname = $path . "./";

#if ($DIRNAME) { $dirname = $path  . $DIRNAME . "/"; }
if ($DIRNAME) { $dirname = $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 "<TABLE CELLPADDING=20 BORDER=4><TR><TD BGCOLOR=#99CCFF>\n";
	my $s='';
	$s .= '<FORM ACTION="#">';
	$s .= "<INPUT NAME=${Q}DIRNAME$Q VALUE=$Q$DIRNAME$Q> Directory.\n";
	$s .= '<BR><INPUT TYPE=SUBMIT VALUE="VIEW ANOTHER DIRECTORY" ';
my $d = $dirname;
$d =~ s:/$::;
	$s .= "\n	onClick=${Q}if(DIRNAME.value=='$d'){alert('Specify a different dir!');DIRNAME.focus()}$Q>\n";
	$s .= '</FORM>';
	print $s;

	print "</TABLE>\n";

        &show( $tail );

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];
	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 "<FONT SIZE=+2 COLOR=#000066> Files in directory: $d  </FONT>\n";
  #print "<BR><I>(Click on file name, to modify it.)</I>\n";


  print "<TABLE BGCOLOR=#FFFFCC BORDER=1 CELLPADDING=6 XALIGN=CENTER>";

  my $f;
  my $max=$maxlines;
  my $next;
  foreach $name ( sort @allfiles ) {
	my $f = $path . $dirname . $name;
print "<LI> $f\n";

	#if ( $f =~ /^\./ ) { next; }			## Hide the hidden files.
	#if ( $f =~ /^\// ) { next; }
	#if ( $f =~ /^~/ ) { next; }
	#if ( $f =~ /\.pl/i ) { next; }			## Hide the .pl files.
	#++++ Others to hide or skip??

	my $ff=$f;
	$ff =~ s/^\.\///g;
	$ff =~ s/^[.\/~ 	]*//g;		# Delete leading dots and/or slashes (or blanks).


	print "<TR>";
	print "<TD>\n	";

	## Special cases for html, gif, etc.
	if ( $f =~ /\.s*html*$/i 
	or $f =~ /\.php$/i 
	or $f =~ /\.txt$/i 
	) 
	{
		print "<A HREF=$Q$ff$Q TARGET=_blank>";
		print "<FONT FACE=${Q}sans-serif$Q COLOR=#660000>";
		print "$ff";
		print "</FONT>\n";
		print "<TD>";
		if ( $f =~ /^index\./i ) {next;}		## Skip the index file.
		else {
			print "\n	";
			print "<A HREF=${Q}upload.pl?FILENAME=$f$Q TARGET=_blank>\n";
			#print "<I>Modify?</I></A>";
			print "<IMG SRC=author.gif BORDER=0></A>\n";
		}
	}
	elsif ( $f =~ /\.gif$/i 
	  or $f =~ /\.png$/i 
	  or $f =~ /\.jpe*g$/i )
	{
		print "<A HREF=$Q$ff$Q TARGET=_blank>";
		print "<FONT FACE=${Q}sans-serif$Q COLOR=#006600>";
		print "$ff";
		print "</FONT>\n";
		print "<TD>";
		print "<IMG SRC=$f BORDER=0 WIDTH=32>\n";
	} else {
		print "<A HREF=$Q$ff$Q TARGET=_blank>";
		print "<FONT FACE=${Q}sans-serif$Q COLOR=#000000>";
		print "$ff";
		print "</FONT>\n";
		print "<TD>";
	}

	print "</TD>";

  }
  print "<\UL>\n";
}




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

