#!/usr/bin/perl
#####################################################################
### script som automatisk lager en index.html der det ikke finnes ###
#####################################################################
#noen variable foerst:
#use Cwd;
   #hvor staar jeg?
#$DIR = cwd();
#@dir_array = split /\//, $DIR;
#$antall = @dir_array;
#$MAIN_DIR = @dir_array[4];
$count = 0;
&makeDirlist;
open (MAINFILE, "/home/almash/temp/dirlist.redigert");
while ( $linje = <MAINFILE> )
{
    $DIR = $linje;
    $DIR =~ s/\n//;
    @dir_array = split /\//, $DIR;
    $antall = @dir_array;
    $MAIN_DIR = @dir_array[4];
       #leter opp navn pa Dewey-katalog:
    $PARENTDIR = @dir_array[$antall-1];
#    print "parentdir = $PARENTDIR\n";
       #Her finner vi dewey-katalognavnet:
    $DEWEY_DIR = &findDewey($PARENTDIR);

    if ($DEWEY_DIR eq "000")
    {
#	print "naa er deweydir satt til 000\n";
	   #Setter DEWEY_DIR til main om det ikke er noen dewey-katalog dvs star oeverst!
	$DEWEY_DIR = "000";
    }
#    elsif ($DEWEY_DIR == " ")
#    {
#	   #Setter DEWEY_DIR til main om det ikke er noen dewey-katalog dvs star oeverst!
#	$DEWEY_DIR = "Main";
#    }
    $status = 0;#for sikkerhetskyld!:)
     $status =  &makeList($DIR);
    if ($status == 1)
    {
#	print "status er na: $status\n";
	$FILES = 1;#ja, det er filer i katalogen
	   #Her kan vi fortsett, for alt gikk helt greitt!!!
	&makeFile($MAIN_DIR, $DIR, $DEWEY_DIR, $FILES);
    }
    elsif ($status == 2)
    {
#	print "status er na: $status\n";
	$FILES = 0;#Nei, det er ikke noen filer i katalogen
	   #Katalogen inneholdt ingen ting, dvs. ingen filer i katalogen 	
	&makeFile($MAIN_DIR, $DIR, $DEWEY_DIR, $FILES);
    }
    elsif ($status == 3)
    {
	   #Her maa vi sende en eller annen fornuftig melding tilbake at det gikk dritt!!!
	#system("mail -s 'Etellerannetgikkfeil!' trondif\@svada.hiof.no < /home/almash/trondif/perl/tyll.txt ");
    }
    elsif ($status == 4)
    {
	print "YIHAAAAAAAAAAAAAAAAAAAAAAAAA!\n";
    }
}
close (MAINFILE);
####################################################################################
####################################################################################
####                           Funksjoner:                                      ####
####################################################################################
####################################################################################

#Funksjon som lager en fil med alle kataloger pa /www/almashriq/htdocs/
#Usage: &makeDirlist

sub makeDirlist
{
       #Lager en tempfil som inneholder alle underkataloger:
#MA GJORES OM TIL /www/almashriq/htdocs
    system("du /home2/almash/indexfiler >/home/almash/temp/dirlist.txt.$$");
    open (FILE, "/home/almash/temp/dirlist.txt.$$");
    open (FILE2, ">/home/almash/temp/dirlist.redigert");
    while ( $line = <FILE> )
    {
	$line =~ s/^\d*.//;
	$line =~ s/\n//;
	print FILE2 "$line\n";
    }
    close (FILE);
    close (FILE2);
       #Sletter tempfilen, trenger ikke den lengre
    unlink("/home/almash/temp/dirlist.txt.$$");
}

###################################################################################

#Funksjon som finner fram Dewey-katalognavnet til et tall
#Usage: &findDewey

sub findDewey
{
    my ($parentdir) = @_;
    $PARENT_DIR = $parentdir;
    open (FILE, "/home/almash/trondif/perl/dewey.ver21") or die "dette gjekk dritt LINJE 77\n";
    while ( $line = <FILE> )
    {
	if ($line =~ /^$PARENT_DIR/i)
	{
	       #ta alt fra tegn nr5 og ut linjen!
	    close (FILE);
	    return $line;
	}
    }
}

##################################################################################

#Funksjon som lager en liste over innholdet i katalogen:
#Usage: &makeList(katalog)

sub makeList
{
    my ($workingdir) = @_;
    $teller = 0;
    system("ls $workingdir > /home/almash/temp/katalog_innhold.txt");
    if ( -s "/home/almash/temp/katalog_innhold.txt")
    {#det ble laget en fil med kataloginnholdet!
	 open(TESTFILE, "/home/almash/temp/katalog_innhold.txt");
	 while ($lines = <TESTFILE> )
	 {
	     if ( $lines =~ /^index.html/ )
	     {
	     $teller ++;
	     }
	 }
	 close (TESTFILE);
	 open(TESTFILE, "/home/almash/temp/katalog_innhold.txt");
	 while ( $lines = <TESTFILE> )
	 {
#	     print "\nteller = $teller\n";
	     if ( $teller == 1)# & $lines =~ /^index.html/ )
	  { 
#	      print "Inside teller=1\n";
	      if ($lines =~ /^index.html/ )
	      {
#		  print "det var bare en fil og det var indexfila!\n";
		  close (TESTFILE);
		  return 4;
	      }	
	  }
	     close (TESTFILE);
	     return 1;
	 }
     }
    else
    {#det er ikke noe innhold i katalogen:
	 close (TESTFILE);
	 return 2;
     }

    close (TESTFILE);
    return 3;
    
#HUSK AA SLETTE "KATALOG_INNHOLD.TXT" ETTER BRUK!!!!!!!!!!!!
}

#################################################################################

#Funksjon som lager en index-fil med liste over underkataloger m/html-tags:
#Usage: &makeFile(hovedkatalog, katalog, deweykatalog, erdetnoenfiler)
#hovedkatalog = hvilket land
#katalog = pwd

sub makeFile
{
    my ($mainDir, $workingdir, $deweyDir, $Files) = @_;
    $dir = $workingdir;
    $maindir = $mainDir;
    $dewey = $deweyDir;
    $maindirURL = $maindir;
    $files = $Files;
    local $katalognavn = " ";
#Her blir landet skrevet med stor forbokstav:
    $maindir =~ /(.)(.*)/;
    $first = $1;
    $first =~ tr/a-z/A-Z/;
    $maindir = $first . $2;
#det finnes ingen filer i katalogen:
    open (FILE, "/home/almash/temp/katalog_innhold.txt") or die;
    $INDEXFILE = $dir . "\/" . "index.html";
    $INDEXFILE =~ s/\n//;
#    print "indeks=$INDEXFILE\n";
#    print "files = $files\n";
#Tester om det eksisterer en index-fil fra foer
    $testoperator = 1;
    if ( -e $INDEXFILE )
    {
	open (TESTFILE, "$INDEXFILE");
	while ( $line1 = <TESTFILE>)
	{
	    if ( $line1 =~ /<\!--auto_index-->/ )
	    {#sjekker om filen inneholder taggen "<!--auto_index-->"
		 $testoperator = 1;
#		 print "$line1\n";
		 last;
	     }
	    else
	    {
		$testoperator = 0;
	    }
	}
	close (TESTFILE);
    }
#    print "testoperatoren:  $testoperator\n";
#    print "files er na: $files\n";
    if ( $files == 1 )
    {
#	print "rett for if return: $testoperator\n";
	if ($testoperator == 0)
	{#det er en index-fil, som ikke er blitt laget av auto_index,  fra for av:
	     return 2;
	 }
	elsif ($testoperator == 1)
	{#det finnes ikke en index-fil, eller det er blitt laget av auto_index:
	     
	     open (OUTFILE, ">$INDEXFILE") or die "DEBUG! Far ikke laget en indexfil\n";
#Her blir masse HTML-tagger laget:
	     print OUTFILE "<HTML>\n";
	     print OUTFILE "<HEAD>\n";
	     print OUTFILE "<TITLE>$maindir - $dewey<\/TITLE>\n";
	     print OUTFILE "<META NAME=\"keywords\" contents=\"$maindir \">\n";
	     print OUTFILE "<!--auto_index-->\n";
	     print OUTFILE "<\/HEAD>\n";
	     print OUTFILE "<BODY BGCOLOR=\"#ffffff\" BACKGROUND=\"/sys/ornament-back-small.gif\">\n";
	     print OUTFILE "<CENTER>\n";
	     print OUTFILE "<TABLE WIDTH=528 COLS=1 BORDER=0 CELLPADDING=5>\n";
	     print OUTFILE "<TR VALIGN=TOP>\n";
	     print OUTFILE "<TD COLSPAN=2>\n";
	     print OUTFILE "<a href=\"../\"><img src=\"/sys/almashriq-levant-small.gif\" BORDER = 0></a>\n";
	     print OUTFILE "<TR VALIGN=TOP>\n";
	     print OUTFILE "<TD COLSPAN=2>\n";
	     print OUTFILE "<H3><a href=\"/base/$maindirURL.html\">$maindir<\/A><\/H3>\n";
	     print OUTFILE "<H4>$dewey<\/H4>\n";
	     print OUTFILE "<UL>\n\n";
	     
	     while ($line = <FILE>)
	     {
		 if ($line =~ /^index.html/i)
		 {
		     next;
		 }
		 else
		 {
		     if ($line =~ /\d$/i)
		     {
			 #	print "this isn't true!!!!???\n";
			 next;
		     }
		     #printer ut innholdet av katalogen:
		     $lineURL = $line;
		     $line =~ /^(\d*)/;
		     $lineURL = $1;
#		     print "LINJEURL: $lineURL\n";
		     $line =~ s/_/ /g;
		     $line1 = $line;
		     if ( $line =~ /^(\d*\.\d*\.)(.*)/ )
		     {
			 $linje = $line;
			 $line1 = $1;
			 $test = $2;
			 print "linje = $linje";
			 print "test = $test\n";
			 $lineURL2 = $line1;
			 $lineURL2 =~ s/ /_/g;
			 $lineURL2 =~ s/\.$/ /g;
			 $linje =~ s/_/ /g;
			 $test =~ /(.)(.*)/;
			 $first = $1;
			 print "first : $first\n";
			 $first =~ tr/a-z/A-Z/;
			 print "first :: $first\n";
			 $linje = $line1 . $first . $2;
			 $katalognavn = $linje;
			 print "katalognavn!!! == $katalognavn\n";
			 print "line == $linje\n";
			 print OUTFILE "<a href=\"$lineURL2\">$linje<\/a><br>\n";
		     }
		     elsif ( $line =~ /^(\d*\.)(.*)/ )
		     {#lager stor bokstav etter dewey-tallet:
			  $line1 = $1;
			  $line2 = $2;
			  $line2 =~ /(.)(.*)/;
			  $first = $1;
			  $first =~ tr/a-z/A-Z/;
			  $line2 = $lineURL . ". " . $first . $2;
			  print OUTFILE "<a href=\"$lineURL\">$line2<\/a><br>\n";
		      }
		 }
	     }
	     close (FILE);
	     close (FILE2);
	     print OUTFILE "<\/UL>\n";
	     print OUTFILE "<TR VALIGN=TOP>\n";
	     print OUTFILE "<TD COLSPAN=2 ALIGN=CENTER>\n";
	     print OUTFILE "<P>\n";
	     print OUTFILE "<!--Linker-->\n";
	     print OUTFILE "<a href=\"/\"><img src=\"/sys/almashriq-bottom-line.gif\" alt=\"----------------\" BORDER = 0></a><p>\n";
	     print OUTFILE "<a href=\"/base/mailpage.html\">al\@mashriq</a><p>\n";
	     
	     print OUTFILE "<\/TABLE>\n";    
	     print OUTFILE "<\/CENTER>\n";
	     print OUTFILE "<\/BODY>\n";
	     print OUTFILE "<\/HTML>\n";
	     close (OUTFILE);
	 }
    }
    elsif ( $files == 0 )
    {
	print "testoperator= $testoperator\n";
	if ($testoperator == 0)
	{#det er en index-fil, som ikke er blitt laget av auto_index,  fra foer av:
	     return 2;
	 }
	elsif ($testoperator == 1)
	{#det finnes ikke en index-fil, eller det er blitt laget av auto_index:
	     
	     open (OUTFILE, ">$INDEXFILE") or die "DEBUG! Far ikke laget en indexfil\n";
#Her blir masse HTML-tagger laget:
	     print OUTFILE "<HTML>\n";
	     print OUTFILE "<HEAD>\n";
	     print OUTFILE "<TITLE>$maindir - $dewey<\/TITLE>\n";
	     print OUTFILE "<META NAME=\"keywords\" contents=\"$maindir \">\n";
	     print OUTFILE "<META HTTP-EQUIV=\"refresh\" content=\"3\; URL=http://svada.hiof.no/base/$maindirURL.html\">\n";
	     print OUTFILE "<!--auto_index-->\n";
	     print OUTFILE "<\/HEAD>\n";
	     print OUTFILE "<BODY BGCOLOR=\"#ffffff\" BACKGROUND=\"/sys/ornament-back-small.gif\">\n";
	     print OUTFILE "<CENTER>\n";
	     print OUTFILE "<TABLE WIDTH=528 COLS=1 BORDER=0 CELLPADDING=5>\n";
	     print OUTFILE "<TR VALIGN=TOP>\n";
	     print OUTFILE "<TD COLSPAN=2>\n";
	     print OUTFILE "<a href=\"../\"><img src=\"/sys/almashriq-levant-small.gif\" BORDER = 0></a>\n";
	     print OUTFILE "<TR VALIGN=TOP>\n";
	     print OUTFILE "<TD COLSPAN=2>\n";
	     print OUTFILE "<H3><a href=\"/base/$maindirURL.html\">$maindir<\/A><\/H3>\n";
	     print OUTFILE "<H4>$katalognavn is empty<\/H4>\n";
	     print "katalognavn = $katalognavn\n";
	     print OUTFILE "<H3>Redirect you in 3 seconds<\/H3>\n";
	     print OUTFILE "<TR VALIGN=TOP>\n";
	     print OUTFILE "<TD COLSPAN=2 ALIGN=CENTER>\n";
	     print OUTFILE "<P>\n";
	     print OUTFILE "<!--Linker-->\n";
	     print OUTFILE "<a href=\"/\"><img src=\"/sys/almashriq-bottom-line.gif\" alt=\"----------------\" BORDER = 0></a><p>\n";
	     print OUTFILE "<a href=\"/base/mailpage.html\">al\@mashriq</a><p>\n";
	     
	     print OUTFILE "<\/TABLE>\n";    
	     print OUTFILE "<\/CENTER>\n";
	     print OUTFILE "<\/BODY>\n";
	     print OUTFILE "<\/HTML>\n";
	     close (OUTFILE);
	 }
	
    }
}
    
