#!/usr/bin/perl
use File::Find;
use Cwd;
$topdir=cwd();
print STDOUT "$topdir\n";
@itransextns = ("txt", "itx");
@htmlextns = ("htm", "html");
@pixextns = ("gif","jpg","jpeg","jpe");
@dirs =(); #all the directories below
@reldirs=();
$thumbstr = "thumb";
$charstr = "";
$topref=""; #string contains links from the top of the page to things on the page itself
#$/ = ""; # Enable paragraph mode.
#$* = 1; # Enable multi-line patterns.
foreach $extn (@htmlextns){
$charstr = $charstr . "\\". $extn. "|"; #building an array of extensions to match
}
$charstr =~ s/(.*)\|$/$1/; #get rid of the last vertical line.
#open(INDEX, ">index.html") || die "cannot open index.html $!\n"; #The top index.html
#open(INDEX, ">MasterFile.html") || die "cannot open MasterFile.html $!\n"; #The masterfile
#select INDEX;
#&printhead;
find(\&mkdirlist,".");
#print STDOUT @dirs;
$smref = "
["; #sentence month reference header
foreach $dir (@dirs){ #build header for the sentence month-index files
($onlydir = $dir ) =~ s/(.*)\/(.*)?/$2/;
if ("$topdir" eq "$dir") {
$reldir = "\.";
}
else{
($reldir = $dir) =~ s/$topdir\/(.*)/$1/;
}
if ($onlydir =~ m/(\d\d\d\d)-(\d\d)-(\d\d)/){
$smyear = $1;
&getmonth($2);
$smref = "$smref" . " $smmonth $smyear |";
#print STDOUT "$smref/n";
}
}
$smref =~ s/(.*)\|$/$1/; #get rid of the last vertical line.
$smref = "$smref" . "]\n"; #printed in the month-index file
#print STDOUT "$smref";
#
foreach $dir (@dirs){
($onlydir = $dir ) =~ s/(.*)\/(.*)?/$2/;
if ("$topdir" eq "$dir") {
$reldir = "\.";
}
else{
($reldir = $dir) =~ s/$topdir\/(.*)/$1/;
}
if ($topdir =~ m/$onlydir/){ #do not create a folder index for the top directory
next; #don't do anything for the top directory
}
if ($onlydir =~ /^Sentences$/){ #don't do anything in the top sentence directory
next;
}
if ($onlydir =~ /^sarani$/){ #don't do anything in the sarani directory
next;
}
#print STDOUT "Only Dir: $onlydir\n";
#print STDOUT "Dir: $dir\n";
#print STDOUT "Rel Dir: $reldir\n";
chdir("$dir");
@lindexstat = stat "$onlydir\.html"; #local index file statistics
#system("pwd");
@filelist = (); #a list of all the files (with full path) in a directory
@onlyfilelist = (); #only file list without directory information
$winIndex = 0;
while (<*>) {
$file = $_;
if ($file =~ m/^thumb/i){next;}
if ($file =~ m/($charstr)/i){ #
$extn = $1; #the match above
#print STDOUT "$extn";
($onlyfile = $file) =~ s/(.*)$\.$extn/$1/; #filename without the extension
push(@filelist,$file);
push(@onlyfilelist,$onlyfile);
}
}
#$topref = " ["; #this string is put on the top
#print STDOUT @filelist, "\n";
$numoffiles = @filelist;
if ($numoffiles == 0){next;}
$lindflag = 0; #localindex file flag
for ($ctr=0; $ctr<$numoffiles;$ctr++){#check if localindex is more recent or not
$file = $filelist[$ctr];
$onlyfile = $onlyfilelist[$ctr];
@fstat = stat "$file";
#print STDOUT "stats: $lindexstat[9]\, $fstat[9]\n";
if ($lindexstat[9] < $fstat[9]){$lindflag = 1;}
}
if ($lindflag == 0){next;} #if $onlydir.html is more recent than all other html files then don't do anything
#print STDOUT "lindflag=$lindflag\n";
#build topref
if (2 > 3){
for ($ctr=0; $ctr<$numoffiles ;$ctr++){
$file = $filelist[$ctr];
$onlyfile = $onlyfilelist[$ctr];
$topref = "$topref" . " $onlyfile |";
#print STDOUT "$topref/n";
}
$topref =~ s/(.*)\|$/$1/; #get rid of the last vertical line.
$topref = "$topref" . "]"; #printed via printhead subroutine.
}
#
open(LOCALINDEX, ">$onlydir\.html") or die "cannot open $onlydir\.html $!\n"; #">$onlydir\.html"
#print INDEX ""; #$onlydir\.html
#print INDEX "$onlydir
\n";
select LOCALINDEX;
&printhead; #$topref string is printed via printhead
if ($onlydir =~ m/(\d\d\d\d)-(\d\d)-(\d\d)/){ #print month-year for sentence subdirectories
print LOCALINDEX $smref;
}
open(FINFO, "){
print LOCALINDEX $_;
}
#
for ($ctr=0; $ctr<$numoffiles ;$ctr++){
$file = $filelist[$ctr];
$onlyfile = $onlyfilelist[$ctr];
if ($onlyfile =~ m/$onlydir/){ #m/$onlydir/){ #do not create link for the localindex file
#print STDOUT $onlydir;
next;
}
if ($onlyfile =~ m/(\d\d\d\d)-(\d\d)-(\d\d)/){ #if it's a file with date then print the day
print LOCALINDEX "$3 - ";
}
print LOCALINDEX "html | "; #$file | ";
#print STDOUT "html - $onlyfile\n";
unless ($onlyfile =~ /index/){ #don't link the index.txt file
if (-e "$onlyfile\.txt"){ #link only if a txt file exists
#print STDOUT "txt - $onlyfile\n";
print LOCALINDEX " txt \ "; #$onlyfile\.txt ";
}
}
open(LFILE, "<$file") or die "cannot open $file $!\n";
$keepskipping = 0;
while(){
$line = $_; $line =~ s/\r//g;
#print STDOUT "Beginning: $line \n";
if ($line =~ m/^$/){
next;
}
if ($line =~ m//i){
$keepskipping = 1;
#print STDOUT "$file: $line \n";
next;
}
if ($line =~ m|(.*)
|i & $keepskipping == 1){ #pick up the first paragraph
print LOCALINDEX "$1
\n"; #print what is between the paragraph marks
#print STDOUT "$file: $line \n";
last;
}
}
close(LFILE);
#the following code is to insert links in the html files.
#It checks if the links already exist or not. If they do it simply copies the file.
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime, $mtime,$ctime,$blksize,$blocks)
= stat "$file";
#print STDOUT "$file: $mtime, $lindexstat[9]\n";
if ($mtime > $lindexstat[9]){ #enter the loop only if the localindex file is older than the html file
print STDOUT "Updating $file...";
$tmpfile = "temp$file";
open(INFILE, "<$file");
open(TMPFILE, ">$tmpfile");
while (){ #first stores the contents in a temp file
print TMPFILE;
}
close(INFILE);
close(TMPFILE);
open(TMPFILE, "<$tmpfile");
open(INFILE, ">$file");
$linecount=0;
$linksappended=0;
while (){
$line = $_;
$linecount++;
if ($linecount == 2){
#print STDOUT "$linecount: $line \n";
unless ($line =~ m/links\-appended\-\(HRP\)/){
#print STDOUT "In links-appended\n";
print INFILE "\<\!\-\-links\-appended\-(HRP).\-\-\> \n";
$linksappended = 1;
}
}
unless ($linksappended == 1){ #if the links have already been appended just copy the file
print INFILE $line;
next;
}
if ($line =~ m/font-family\: \"Sanskrit 2003\"\;/){
print INFILE " font-family\:\"Sanskrit 2003\"\,\"Sanskrit 99\"\,\"URW Palladio ITU\",Uttara\,Chandas\,Code2000\;";
next;
}
if ($line =~ m/^/i){
select INFILE;
print <
[ Up |
Sentences |
Dhaatus |
Vibhakti |
Misc |
Site Index |
Learn Sanskrit |
Home Personal |
Home ]
EOF
next;
}
if ($line =~ m+<\/BODY>+i){
select INFILE;
print <
[ Up |
Sentences |
Dhaatus |
Vibhakti |
Misc |
Site Index |
Learn Sanskrit |
Home Personal |
Home ]
EOF
}
print INFILE $_;
}
close(INFILE);
close(TMPFILE);
print STDOUT "Done.\n";} # matches with if the htm files was created after the localindex file
unlink $tmpfile;
#end of link copying code
#$topref = "$topref" . " $onlyfile |";
#print STDOUT "$topref/n";
}
if (2 > 3){
$numofcols = 4;
$ctr=0;
for ($ctr=0; $ctr < $numoffiles; $ctr=$ctr+$numofcols){
$tr1str="";
$tr2str="
";
for ($imgctr=0; $imgctr < $numofcols; $imgctr++) {
if ($filelist[$ctr+$imgctr]) {
$file = $filelist[$ctr+$imgctr];
$onlyfile = $onlyfilelist[$ctr+$imgctr];
$thumbfile = "$thumbstr" . "$file";
$clonlyfile = "cl" . "$onlyfile";
#$fsize = -s "$dir/$file"; #file size
#$ksize = int($fsize/1000) . "k";
$tr1str = "$tr1str" . "  | ";
$tr2str = "$tr2str" . " $onlyfile | \n";
$winIndex = $winIndex + 1;
}
}
print LOCALINDEX "$tr1str Top |
\n";
print LOCALINDEX "$tr2str \n";
}
}
select LOCALINDEX;
&printtail;
close(LOCALINDEX);
chdir("$topdir");
}
#select INDEX;
#&printtail;
#close(INDEX);
sub mkdirlist {
if (-d $_) {
($dirname = $File::Find::name )=~ s/./$topdir/; #replace the first dot with $topdir
push(@dirs, "$dirname");
}
}
sub printhead {
print <