#!/usr/bin/perl -W


use strict;
use English;

my $outdir = "gen";
my $stub;
my $RedUpdate="<em><font color=\"FF0000\" > Update</font></em>";
my $PinkUpdate="<font color=\"FF8383\" > Update</font>";

# open the outline file
open (OUTLINE,"<outline.html") || die ("can't open outline - $!");
my @outline_tmp=(<OUTLINE>);
close (OUTLINE);
my $outline_base=join('',@outline_tmp);

# create the output subdir if it doesn't already exist 
if (! ( -d $outdir )) {
  mkdir($outdir) || die ("can't make output dir - $!"); 
}

#create a sub-dir the hebrew files
if (! ( -d $outdir.'/hebrew' )) {
  mkdir($outdir.'/hebrew') || die ("can't make output dir - $!"); 
}

# process the files 
foreach $stub (@ARGV){
  open (IN,"<$stub") || die ("can't open $stub - $!");
  my @page_tmp=(<IN>);
  close(IN);
  my $page=join('',@page_tmp);
  my $html=$stub;
  ($html=~s/htm$/html/) or $html.='.html';
  #get the the first line, which is heading1 (<h1>), and use it for title.
  #we shave it off for now.
  #later on, we stick it back  inside in the proper place
  #first we shave the <h1> we expect
 
  $page=~s|^\s*\<\s*h1\s*\>||i  
    or die "no heading <h1> for $stub\n";
  #then we seek a closing </h1>. we seperate by it the title.
  # we do not use the following form:
  #  $page=~s|^\s*\<\s*h1\s*\>([^\<]+)\<\s*\/h1\s*\>||i 
  #   or die "no heading for $stub\n";
  #  in order to allow for tags within the title (for links and modifiers- for example, <div dir="rtl">)
  $page=~s|\<\s*\/h1\s*\>||i 
    or die "no heading </h1> for $stub\n";
  my $head=$`;
  $page=$';
  my $site_name = "CSL Projects";
  my $title = (($head eq $site_name) ? $site_name : "$site_name - $head");
  #title cannot have any tags inside it, but we may need tags for redirection
  $title=~s/\<[^\>]*\>//g; 
  
  print "$stub, $head\n";
  my $outline=$outline_base;

  my $is_hebrew=($html=~/hebrew\//);

  my $other_language;
  my $direct_to_other_language;
  if ($is_hebrew){
    $other_language='../'.$POSTMATCH;
    $direct_to_other_language="This page in English";
   
    $page="\<div dir=\"rtl\"\>$page\<\/div\>";
    $head="\<div dir=\"rtl\"\>$head\<\/div\>";
    }else{
    
    $other_language='hebrew/'.$html;
    $direct_to_other_language="דף זה בעברית";
  }

  my $link_to_other_language='<a href="'.$other_language.'">'.
    $direct_to_other_language.'</a>';
	#I dont want to deal with Hebrew here
	$link_to_other_language='';
  $outline=~s/LinkToThisPageInOtherLanguage/$link_to_other_language/;

  $outline=~s/InsertTextHere/$page/;

  $outline=~s/InsertHeadHere/$head/;
  $outline=~s/InsertTitleHere/$title/;
  if($is_hebrew) {
     $outline=~s/>[^<]+<!--translate(([^-]|(-[^-]))+)-->/><span dir="rtl">$1<\/span>/g;
  }

    

  $outline=~s/RedUpdate/$RedUpdate/g;
  $outline=~s/PinkUpdate/$PinkUpdate/g;
#Find examples, and convert them to text by inserting <br\> before each \n
  while ($outline=~/BeginExampleHere/){
    my $before=$PREMATCH;
    my $rest=$POSTMATCH;
    ($rest=~/EndExampleHere/) or die "example begun but not ended on $stub\n";
    my $after=$POSTMATCH;
    my $example=$PREMATCH;
    $example=~s/\n/\<br \/\>\n/g;
    $outline=$before.$example.$after;
  }
  my $outname = "$outdir/$html";
  open(OUT,">$outname") || die ("can't open output file $outname - $!");
  print OUT $outline;
  close(OUT);
}
