#!/star/Perl/bin/perl -w
use IO::Handle;
use File::Basename;
use Cwd;
use File::Copy;

require 'vespaServer.pl';

$htdir="$SVNDIR/vespa";
$commonDir="$SVNDIR/common";
$workdir=$WORKDIR;
$installdir=$BASEWWWDIR;

system ("cd $SVNDIR; $SVNCMD  update ./");


$singleFile="";
$singleFile=$ARGV[0];
$noArgs=scalar @ARGV;
$DEFAULTTITLE="VESPA";
$DEFAULTCONTENT="VESPA";
print "::: $noArgs \n ";

    @htTypes = ('.ht'); # files of this type will be branded with nav bars
    undef %is_htType;
    for (@htTypes) { $is_htType{$_} = 1; }

@copyTypes=('.html','.htm','.jpeg','.jpg','.gif','.fud','.png','.txt','.pdf','.gz','.sql','.css');
   undef %is_copyType;
    for (@copyTypes) { $is_copyType{$_} = 1; }

if ($is_htType{'.ht'}) {
print "htype $is_htType{'.ht'} \n";
}
if ($is_copyType{'.htm'}) {
print "copytype $is_copyType{'.htm'} \n";
}
   
print $workdir,$htdir;

if ($noArgs > 0 ){
system ("cd $workdir; find $htdir/ |  grep -v \\\\.svn |  grep -v \\\\.pl |  grep -v \\\\.ht\\\\.bak | grep -v \\\\.httemp | grep -v \\\\.ht~ | grep $singleFile | grep -v end.ht | grep -v bottom | grep -v top.ht > $workdir/in.dat");
}
else {
system ("cd $workdir;  find $htdir/ | grep -v \\\\.svn | grep -v \\\\.pl | grep -v \\\\.ht\\\\.bak | grep -v \\\\.httemp | grep -v \\\\.ht~ | grep -v end.ht | grep -v bottom.ht | grep -v top.ht > $workdir/in.dat");
}


$filename="$workdir/in.dat";

#open (WORK,$filename);

#while (<WORK>){
#    chomp;
#    $possDir=$_;
#    if (-d $possDir){
#	print "directory $possDir\n";
#    }
#}

close(WORK);

open INPUT, "<$htdir/top.ht";
undef $/;
$topBasic = <INPUT>;
close INPUT;
$/ = "\n";     #Restore for normal behaviour later in script

#print "$topBasic\n";

open INPUT, "<$htdir/bottom.ht";
undef $/;
$bottomBasic = <INPUT>;
close INPUT;
$/ = "\n";     #Restore for normal behaviour later in script

#print "$bottomBasic\n";

open INPUT, "<$htdir/end.ht";
undef $/;
$endBasic = <INPUT>;
close INPUT;
$/ = "\n";     #Restore for normal behaviour later in script

#print "$endBasic\n";

$topBasic=~s/\$URL./$BASEWWWURL/g;
$topBasic=~s/\$JSP./$BASEJSPURL/g;
$topBasic=~s/\$CGI./$BASECGIURL/g;

$topHC=$topBasic;

$topHC=~s/<!-- \$TITLE -->/$DEFAULTTITLE/g;
$topHC=~s/<!-- \$CONTENT -->/$DEFAULTCONTENT/g;

$bottomBasic=~s/\$URL./$BASEWWWURL/g;
$bottomBasic=~s/\$JSP./$BASEJSPURL/g;
$bottomBasic=~s/\$CGI./$BASECGIURL/g;

$endBasic=~s/\$URL./$BASEWWWURL/g;
$endBasic=~s/\$JSP./$BASEJSPURL/g;
$endBasic=~s/\$CGI./$BASECGIURL/g;

$top="$workdir/top.html";
unlink $top;
open (TOPHTML, ">$top");
print TOPHTML $topHC;
close (TOPHTML);


$bottom="$workdir/bottom.html";
unlink $bottom;
open (BOTHTML, ">$bottom");
print BOTHTML $bottomBasic;
close (BOTHTML);


$end="$workdir/end.html";
unlink $end;
open (ENDHTML, ">$end");
print ENDHTML $endBasic;
close (ENDHTML);


#open INPUT, "<local_1.menu";
#undef $/;
#$local_1 = <INPUT>;
#close INPUT;
#$/ = "\n"; 

#open INPUT, "<local_2.menu";
#undef $/;
#$local_2 = <INPUT>;
#close INPUT;
#$/ = "\n"; 

#open INPUT, "<facilities_1.menu";
#undef $/;
#$facilities_1 = <INPUT>;
#close INPUT;
#$/ = "\n"; 

#open INPUT, "<facilities_2.menu";
#undef $/;
#$facilities_2 = <INPUT>;
#close INPUT;
#$/ = "\n";



open (WORK,$filename);

while (<WORK>){
    
    $content=$DEFAULTCONTENT;
    $title=$DEFAULTTITLE;
    $topTemp=$topBasic;
    $bottomTemp=$bottomBasic;
    chomp;
    $line=$_;
    $relativeFile=$line;
    $relativeFile=~ s/$htdir\///g;
    print "$relativeFile :::\n";
    @fields=split(/&/,$line);
    $htfile=$fields[0];
    ($pfile, $pdir, $psuffix) = fileparse($relativeFile,qr/\.[^.]*/);
    print "$relativeFile\n";
    print "dir $pdir \n";
    $cdir = getcwd;
    print "CWD $cdir \n";
    unless (-e "$workdir/$pdir"){
      print	"need to create dir $workdir/$pdir\n";
      mkdir "$workdir/$pdir";
    }


    print "file $pfile \n";
    print "suffix $psuffix \n";

 if ($is_htType{$psuffix}) {   
 #   if ($psuffix eq ".ht") {
    open( HT, "<$htfile");
    while (<HT>){
	$htline=$_;
        chomp($htline);
        if ( $htline =~ /<!-- \$TITLE/){
	    $title=$htline;
	    $title=~ s/<!-- \$TITLE//;
            $title=~ s/-->//;
	   print "$title \n";
	}
        if ( $htline =~ /<!-- \$CONTENT/){
	    $content=$htline;
	    $content=~ s/<!-- \$CONTENT//;
            $content=~ s/-->//;
	   print "$content \n";
	}
    }
    close(HT);
    open INPUT, "<$htfile";
    undef $/;
    $htcontents = <INPUT>;
    close INPUT;
    $/ = "\n"; 
 $htcontents=~s/\$URL./$BASEWWWURL/g;
$htcontents=~s/\$JSP./$BASEJSPURL/g;
$htcontents=~s/\$CGI./$BASECGIURL/g;
    $date=$fields[1];
    chomp($htfile);
    ($mdate)=(stat($htfile))[9];
    ($sec,$min,$hour,$mday,$mon,$year,$a1,$a2,$a3) = localtime ($mdate);
    $mon++;#Perl counts months 0-11, so increment for humans
    $year=$year+1900;
    $thedate = "$mday/$mon/$year";
    $date=$thedate;

    print "$htfile $date\n";
    $htmlfile="$workdir/".basename($htfile)."ml";
    $htmlfile="$workdir/".$relativeFile."ml";;
    print "$htmlfile XXXXXX\n";
    unlink $htmlfile;

$topTemp=~s/<!-- \$TITLE -->/$title/;
$topTemp=~s/<!-- \$CONTENT -->/$content/;
#if ($htfile =~ /local_org/) {
#$topTemp=~s/<!-- \$LOCAL_2 -->/$local_2/;
#}

#if ($htfile =~ /local/) {
#$topTemp=~s/<!-- \$LOCAL_1 -->/$local_1/;
#}

#if ($htfile =~ /facilities_near/) {
#$topTemp=~s/<!-- \$FACILITIES_2 -->/$facilities_2/;
#}

#if ($htfile =~ /facilities/) {
#$topTemp=~s/<!-- \$FACILITIES_1 -->/$facilities_1/;
#}

    print "htmlfile $htmlfile \n";
open (HTMLFILE, ">$htmlfile");
print HTMLFILE "$topTemp\n";
print HTMLFILE "$htcontents\n";
print HTMLFILE "$bottomTemp\n";
print HTMLFILE "<br>$date";
print HTMLFILE "$endBasic\n";

#</font></i><p>\n";
#print HTMLFILE "</center> </td>";
#print HTMLFILE "<td>&nbsp;</td>";
#print HTMLFILE "<td style=\"height: 100%; width:32px;\" height=\"100%\" valign=top background=\"side.jpg\" alt="sidebar" width=32><img alt="sidebar" src=\"side.jpg\"></td>";
#print HTMLFILE "</table> </BODY> \n";
#print HTMLFILE "</html>";


close (HTMLFILE);

}


 if ($is_copyType{$psuffix}) { 
     print "copy file:: $htfile  $relativeFile \n";
     if ($psuffix eq '.html' ||  $psuffix eq '.html') {
	 print "replacing\n";
       open REPFILE, "<$htfile";
       undef $/;
       $repContents = <REPFILE>;
       close REPFILE;
       $/ = "\n";     #Restore for normal behaviour later in script
       $repContents=~s/\$URL./$BASEWWWURL/g;
       $repContents=~s/\$JSP./$BASEJSPURL/g;
       $repContents=~s/\$CGI./$BASECGIURL/g;
	 $newRepFile=$workdir."/".$relativeFile;
       open NEWREPFILE, ">$newRepFile";
       print NEWREPFILE  $repContents;
       close NEWREPFILE;
     }
     else {
     copy ($htfile,"$workdir/$relativeFile");
 }
 }


}
close (WORK);


system ("cp -r $workdir/*  $installdir; cp -fr $commonDir $installdir");

exit;
