#!/usr/local/bin/perl -I/home/users/dberansky/perl/lib

use Cwd;
use CGI qw(fatalsToBrowser);
use File::Find;

$q = new CGI;
$e = new CGI(\%ENV);
$0=~ /(.*\/)[^\/]*/;
$PWD = $1;
($baseURL = "http://".$ENV{HTTP_HOST}.$q->script_name()) =~ s/[^\/]*$//;
$loginURL = "/~dberansk/browser/login.html";

require $PWD."miscLib.pl";

$count = 0;
@colors = ("FFFFFF","Cyan");

#print $q->header;
$username = $q->cookie(-name=>'username');
if( !$username )
{
   print $q->redirect($loginURL);
}
#reset cookie timeout;
setCookies();

$currDir = $q->cookie('dir');
chdir $currDir;
$currDir = cwd;

if( $q->param('entry') )
{
   processEntry( $q->param('entry'), $currDir );
}
elsif( $q->param('action') eq "Upload..." )
{
   print $q->redirect( $uploadURL );
}

print $q->header(-expires=>"-1d", -cookie=>[$cookie1,$cookie2]);

print "<BODY BGCOLOR='FFFFFF'>\n";
print "<TABLE CELLSPACING=0 WIDTH='100%'>\n";
print "<CAPTION>$currDir ($username)</CAPTION>";
print q( <TR><TH></TH>
             <TH>File Name</TH>
             <TH>Type</TH>
             <TH>Modification Date</TH>
             <TH>Size</TH></TR> );
print $q->start_form;
find( \&processFile, $currDir );
print "</TABLE>\n";

print $q->hr;
print $q->submit( -name=>"action", -value=>'Upload...');
print $q->submit( -name=>"action", -value=>'Download...');
print $q->submit( -name=>"action", -value=>'Rename...');
print $q->submit( -name=>"action", -value=>'Delete...');

print $q->end_form;
print "</BODY>\n";

sub processFile
{
   my($file) = $_;
   my($dir) = $File::Find::dir;
   my($color) = $colors[($count++)%2];

   $File::Find::prune = 1;
   $file = '..' if $file eq ".";

   print "<TR BGCOLOR=$color>\n";
   print dirEntry($dir,$file);
   print "</TR>\n";
}

sub dirEntry
{
   my($dir, $file ) = @_;
   my(%fileInfo) = getFileInfo("$dir/$file");
   my($date) = getDate($fileInfo{mtime});
   my($size) = getSize($fileInfo{size});
   my($radio) = $q->checkbox(-value=>$file,-name=>"selected", -label=>"");
   my($fileType) = getType("$dir/$file");

   return <<"***ENDRET***";
      <TD>$radio</TD>
      <TD NOWRAP WIDTH='100%'><A HREF="browser.pl?entry=$file">$file</A></TD>
      <TD NOWRAP>$fileType</TD>
      <TD NOWRAP ALIGN="Center">$date</TD>
      <TD ALIGN=Right NOWRAP>$size</TD>
***ENDRET***
}

sub getFileInfo
{
   my( $fileName ) = @_;
   my( $size );
   my( %info ); 
   my( $dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$ctime,$atime,$mtime,$ctime,$blksize ) = stat $fileName; 

#   if( ! -e $fileName )
#   {
#      print "Can't find $fileName";
#      return;
#   }

   ( $info{dev},$info{ino},$info{mode},$info{nlink},$info{uid},$info{gid},$info{rdev},$info{size},$info{ctime},$info{atime},$info{mtime},$info{ctime},$info{blksize} ) =  
   ( $dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$ctime,$atime,$mtime,$ctime,$blksize );

   return %info;
}

sub getSize
{
   my( $sysSize ) = @_;

   return $sysSize;
}

sub getDate
{
   my( $sysTime ) = @_;
   my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($sysTime);

   return "$mon/$mday/$year $hour:$min:$sec";
}

sub getType
{
   my($file) = @_;

   return "DIR" if -d $file;
   
   $file =~ /.+\.([^\.]*)/;
   $ext = $1;

   return $ext if $ext;

   return "BIN";
}

sub processEntry
{
   my( $entry, $dir ) = @_;

   if( -d "$dir/$entry" ) 
   {
      $pathCookie = $q->cookie( -name=>'dir', -value=>"$dir/$entry",
                                -path=>$scriptPath );
      print $q->header(-cookie=>$pathCookie, -Location=>$baseURL."browser.pl");
      exit;
   }
}
