#!/usr/bin/perl -w
use strict;
use POSIX qw(getcwd strftime);

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use CGI qw(:standard);
print header();
warningsToBrowser(1);

####
# Fehlerausgabe auskommentieren, wenn das Script luft und eingesetzt wird!!
####

### Variablen konfigurieren ###
# relativer Pfad vom cgi-bin Verz. zum htdocs-Verzeichnis
my $htdocs_rel = "http://texnik.dante.de/";
my $htdocs_abs = "/serv/www/texnik/";
my @file_suffixes = ();
my @includefiles = ('tex/*', 'doc/*', 'dvips/*', 'source/*', 'scripts/*', 'config/*');
my @excludefiles = ('robots.txt', 'tree-template.html', 'includeTDS');

my $date_format = "%Y-%m-%d";
# Time format. %H = hour, %M = minute, %S = second.
my $time_format = "%H:%M";

my $q;
$q = new CGI;
my $path = $q->param('path');
chomp ($path);

print <<BACK;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>Directory of $path</title>
    <link rel="stylesheet" href="texnik.css" type="text/css">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>

<body>
<h1>PSTricks in a Tex Directory Structure</h2>
The main sub directories are 
<a href="/cgi-bin/dir.cgi?path=config/">config</a>, 
<a href="/cgi-bin/dir.cgi?path=dvips/">dvips</a>, 
<a href="/cgi-bin/dir.cgi?path=tex/generic/">tex/generic</a>, 
<a href="/cgi-bin/dir.cgi?path=tex/latex/">tex/latex</a>, 
<a href="/cgi-bin/dir.cgi?path=doc/">doc</a>, and
<a href="/cgi-bin/dir.cgi?path=source/">source</a> 
<p></p>

You can download a complete <a href="http://archiv.dante.de/~herbert/PSTricks-TDS.tgz">tarzip of all files</a>, which is
about 120 MByte !!

<h2>Directory of $path</h2>
BACK

init();

my $currentPath = "";
&scan_files($htdocs_abs,$path);

print "\n";
exit(0);


print "</body> \n </html>";

#--------------------------------------------------------------------------
sub scan_files { 
  my (@scandirs,$htdocs,$scandir,@files,$file,$list);
  $currentPath = "$currentPath$scandir";
  $htdocs = $_[0];
  $scandir = $_[1];
  my $absdir="$htdocs$scandir";
  my $reldir="$htdocs_rel$scandir";
  opendir(DIR,"$absdir") || print "can't open the directory $scandir: $!\n";
  @scandirs = grep {!(/^\./) && -d "$absdir/$_"} readdir(DIR);
  rewinddir(DIR);
  @files = grep {!(/^\./) && -f "$absdir/$_"} readdir(DIR);
  closedir (DIR);
  my $currentPath = "";
  for $list(0..$#scandirs) { 
    print "<a href=\"/cgi-bin/dir.cgi?path=$currentPath$scandirs[$list]\">$scandirs[$list]/</a><br />\n";
    $currentPath = $absdir."/".$scandirs[$list];
    &scan_files($absdir."/".$scandirs[$list]); 
  }
  if ($#files >= 0) {
    foreach $file(@files){ 
      next if ( ! include_file($file, $currentPath) );
      print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$scandir<a href=\"";
      print $reldir;
      print $currentPath;
      print "/";
      print $file;
      print "\">$file";
      print "</a>&nbsp;&nbsp;<font size=\"-1\">"; 
      print get_file_date("$htdocs_abs$scandir$file");
      print "&nbsp;&nbsp;";
      print get_file_time("$htdocs_abs$scandir$file");
#      print "$htdocs_abs$scandir$file &nbsp;&nbsp;$htdocs_abs$scandir$file";
      print "</font><br />\n";
    }
  }
  $currentPath = "";
  return 1;
}

sub init()
{
	# Enable '*' as wildcard in @conf::excludefiles and @conf::includefiles
	foreach my $pat (@conf::excludefiles, @conf::includefiles) {
		$pat =~ s#\*#.*?#g;
	}
}



# Return 1 if a file/dir should be included in the generated tree,
# return 0 otherwise.
sub include_file($$){
  my $f = shift;
  my $dir = shift;
  my $check_path = $f;
  $check_path = $dir.$f;
  # Add slash so that '/dirname/*' can match:
  if( -d $check_path ) { $check_path = $check_path."/"; }
	$check_path =~ s/$conf::basedir//;
	# pretend that $conf::basedir is the root directory:
	if( $check_path !~ m/^\// ) {
		$check_path = "/".$check_path;
	}

  my $include_file = 1;
  # possibly exclude certain files:
  if( $include_file && scalar(@excludefiles) >= 1 ) {
		foreach my $inc_pat (@excludefiles) {
			if( $check_path =~ m/^$inc_pat$/ ) {
				$include_file = 0;
				last;
			}
		}
	}
	# debug: warn "$include_file <= $dir, $f\n";
	return $include_file;
}

sub get_file_date($){
	my $filename = shift;
	my $date_time = (stat($filename))[8];
	return POSIX::strftime($date_format, localtime($date_time));
}

# Get formatted time of a file's last modification.
sub get_file_time($){
	my $filename = shift;
	my $date_time = (stat($filename))[8];
	return POSIX::strftime($time_format, localtime($date_time));
}

