#!/usr/bin/perl

# todo
# titles in cftp and explaning the ': in ibidem

# this is the time stamp for the update
$updata="Last update: ".`date --utc --rfc-822`;

# this is the version string for the packaged version
$pakvers=`date --utc +%Y.%m.%d`;

chomp $updata;
chomp $pakvers;

# format for tidy
# this is a fully indented and neat format
$format='-i -w 9999';
# this is a compact format - not good for Netscape
#$format='-o -w 9999';

# cleanup----------------------------------------------------------------------------------------------
# cleaning up garbage files
`rm -f *~ index.html f-*.html framed.html table-index.html *.tgz *.bak tcpip-slides/*~`;

# backing up files
`cp -R * ../backups/miscnotes`;

# -d parameter means we only want to clean files
exit if($ARGV[0] eq '-d');

# tidying all files------------------------------------------------------------------------------------
print "Tidying phase 1\n";
`tidy -m $format *.html *.htm &> /dev/null`;

# -t parameter means basic clean up and reformatting
exit if($ARGV[0] eq '-t');

# ChangeLog processing

# first we clean up ownership entries
open FIN,"<ChangeLog";
open FOUT,">/tmp/ChangeLog";
while(<FIN>)
{
    chomp;
    if(/^(\d\d\d\d-\d\d-\d\d)\s+<.+>$/)
    {
	$_=$1;
    }
    print FOUT "$_\n";
}
close FIN;
close FOUT;
`mv /tmp/ChangeLog ChangeLog`;

$clue="<tr><th class=gin>date</th><th class=gin>changes</th></tr>\n";
$ldate="";

open FIN,"<ChangeLog";
while(<FIN>)
{
    # we must select: date lines, entry start lines, entry continuation lines, formatting lines
    next if(/^\s*---/);
    if(/^(\d\d\d\d-\d\d-\d\d)/)
    {
	if($1 ne $ldate)
	{
	    $cl.=$clue;
	    $cl.="\n<tr valign=top><td class=dry nowrap>$1</td><td class=twist><ul>";
	    $clue="\n</ul></td></tr>\n";
	    $ldate=$1;
	    next;
	}
    }
    if(/^\s+\*(.*)/)
    {
	$cl.="\n<li>$1";
	next;
    }
    $cl.=" $1" if(/^\s+([^*].*)/);
}

$cl.=$clue;
$cl=qq(<!-- changelog start -->\n<table width=90% cellspacing=2 cellpadding=4 align=center summary="The Linux Notebook!Changelog">\n$cl</table>\n<!-- changelog end -->\n);

close FIN;

# intro.html processing--------------------------------------------------------------------------------
# fill in the version numbers in intro.html
# update the changelog

open FIN,"intro.html";
open FOUT,">/tmp/intro.html";

$incl=0;

while(<FIN>)
{
   if(/<!--\s+changelog\s+end\s+-->/)
   {
       $incl=0;
       print FOUT $cl;
       next;
   }
   elsif(/<!--\s+changelog\s+start\s+-->/)
   {
       $incl=1;
       next;
   }
   elsif($incl)
   {
       print "Skipped: $_";
       next;
   }
   elsif(/(.*?<!--\s+version\s+-->)(.*?)(<!--\s+\[(.*?)\]\s+-->.*)/)
   {
       $p1=$1;
       $comm=$4;
       $p3=$3;
       $comm =~ s/=/-/g;
       $comm=`$comm`;
       chomp($comm);
       print "Match!\n";
       $_="$p1$comm$p3";
   }
   elsif(/note="archive"/)
   {
       s!<a href=.*?\s+note="archive">.*?</a>!<a href="LNB-$pakvers.tgz" note="archive">LNB-$pakvers.tgz</a>!;
   }
   elsif(/note="bigarchive"/)
   {
       s!<a href=.*?\s+note="bigarchive">.*?</a>!<a href="LNB-big-$pakvers.tgz" note="bigarchive">LNB-big-$pakvers.tgz</a>!;
   }
   print FOUT;
}
close(FIN);
close(FOUT);

`mv /tmp/intro.html intro.html`;

# find the main files: the ones without dashes in the name and ending in html--------------------------

@mainfiles=(grep(!/-/,`ls *.html`));
chomp @mainfiles;
$mainfiles=join " ",@mainfiles;

# collect menu info -----------------------------------------------------------------------------------

@a=`grep '"menuin"' $mainfiles`;

foreach $v (@a)
  {
    # extracting filename and title
    ($file,$title)= $v =~ /(.*?):(.*)/;
    # cleaning up title
    $title =~ s/<.*?>//g;
    $title =~ s/^\s+//g;
    $title =~ s/\s+$//g;
    $ref= $title;
    $ref =~ tr/A-Z/a-z/;
    $ref =~ tr/A-Za-z0-9/_/cs;

    # each record in @menu contains the title, the filename and the reference used as destination
    push @menu,$title.'%'.$file.'%'.$ref;

    # this accumulates the titles to use as keyword in the meta tags
    $keywords{$_}=1 foreach(split " ",$title);

    # the %titulos hash contains the titles keyed by the file names
    $titulos{$file}.=" / $title";
  }

# we sort the menu by title, case insensitive
@menu=sort {uc($a) cmp uc($b)} @menu;

foreach(keys %keywords)
{ 
    push @remove,$_ if(length($_)<3);
    push @remove,$_ if(/^&/); 
}
delete $keywords{$_} foreach(@remove);
$keywords=qq(\n<meta name="keywords" content=").lc(join " ",keys %keywords).
qq(">\n<meta name="description" content="Documents various Linux or *nix programs and concepts, including qmail, djbdns, emacs, Perl, hardware, bash, TCP/IP, DNS, etc...">\n);

# now we process each file

$tabix="tab000";

foreach $file (@mainfiles)
  {
    chomp $file;

    print "Processing [$file]\n";

    open FIN,"<$file";
    open FOUT,">$file.tmp";

    $inhead=0;

    while(<FIN>)
      {
	  $inhead=1 if(m/<head/);
	  if(m!</head!)
	  {
	      $inhead=0;
	      $_.=$keywords;
	  }
	  if($inhead && m/<meta/)
	  {
	      s/<meta.*?>//g;
	  }
	  s{\s+$}{};
	  s{<a name[^>]+? note="tabix"></a>}{}g;

	  if(/<table\s[^>]*?summary="(.*?)"[^>]*>/)
	  {
	      $tabix++;
	      if(length($1)==0)
	      {
		  print "Table index problem in file $file\n";
		  exit 100;
	      }
	      print FOUT "<a name=\"$tabix\" note=\"tabix\"></a>";
	      push @tabs,$1."!".$file."#".$tabix;
	      print FOUT;
	  }
	  elsif(/note="indexin"/)
	  {
	      # this is where we put the menu, the whole line is replaced
	      $ins='<td colspan="3" align="center" note="indexin">';
	      
	      foreach $r (@menu)
	      {
		  ($title,$page,$ref)=split /%/,$r;
		  if($page eq $file)
		  {
		      $ins.="<a href=\"#$ref\" class=\"hobuts\">$title</a> ";
		  }
		  else
		  {
		      $ins.="<a href=\"$page#$ref\" class=\"tobuts\">$title</a> ";
		  }
	      }   

	      # table index entry	      
	      $ins.="<a href=\"table-index.html\" class=pobuts>Index</a> ";

	      # slides
	      $ins.="<a href=\"tcpip-slides/index.html\" class=tobuts>TCP/IP slides</a> ";
	      
	      $ins.="</td>\n";

	      print FOUT $ins;
	  }
	  elsif( m{<title>} )
	  {
	      # replacing the old title
	      print FOUT "<title>Linux notebook$titulos{$file}</title>\n";
	  }
	  elsif( m/class="time"/ )
	  {
	      # updating the time stamp
	      s!class="time">.*?</td!class="time">$updata</td!;
	      print FOUT $_;
	  }
	  elsif( m{class="menuin">(.*?)</td} )
	  {
	    # replacing the reference
	    $r=$1;
	    $r =~ s/<.*?>//g;
	    $r =~ s/^\s+//g;
	    $r =~ s/\s+$//g;

	    $r=(grep(/^$r%/,@menu))[0];
	    ($title,$page,$ref)=split /%/,$r;
	    $curtitle=$title;
	    s{class="menuin">.*?</td}{class="menuin"><a name="$ref">$title</a></td};
	    print FOUT;
	  }
	  elsif (m/<p class="topic">/)
	  {
	      ($lk,$top)= m{<a name="(\w+)">(.*)</a>};
	      push @tops,"$title§$top§$file#$lk";
	      print FOUT;
	  }
	  elsif (m/<img.*?>/)
	  {
	      ($src,$alt,$link)= m{<img src="(.+?)" alt="(.+?)" link="(.+?)">};
	      push @imgs,"$title§$alt§$file#$link";
	      print FOUT;
	  }
	  else
	  {
	      chomp;
	      s/\s+$//;
	      print FOUT "$_\n";
	  }
      }
    
    close FIN;
    close FOUT;
    
    `mv $file.tmp $file`;
    
}

# creating table index

foreach (@tabs)
{
    ($area,$title,$file)=split /!/;
    $tu=$title;
    $tu =~ s/\(|\)|\:|\.//g;
    $taba{$area}.="$tu§<dd class=minus1><a href=\"$file\">$title</a></dd>§";
    $taci{$area}++;
}

foreach (@tops)
{
    ($area,$title,$file)=split /§/;
    $tu=$title;
    $tu =~ s/<.+?>//g;
    $tu =~ s/\(|\)|\:|\.//g;
    $topba{$area}.="$tu§<dd class=minus1><a href=\"$file\">$title</a></dd>§";
    $topci{$area}++;
}

foreach (@imgs)
{
    ($area,$title,$file)=split /§/;
    $tu=$title;
    $tu =~ s/<.+?>//g;
    $tu =~ s/\(|\)|\:|\.//g;
    $tipba{$area}.="$tu§<dd class=minus1><a href=\"$file\">$title</a></dd>§";
    $tipci{$area}++;
}

open FOUT,">table-index.html";

print FOUT qq(<html><head><title>Linux notebook/Index</title><link rel="StyleSheet" type="text/css" href="style.css">$keywords</head><body><table cellpadding="0" cellspacing="0" width="100%"><tr><td align="center" note="indexin" colspan=3>);

foreach $r (@menu)
{
    ($title,$page,$ref)=split /%/,$r;
    print FOUT "<a href=\"$page#$ref\" class=\"tobuts\">$title</a> ";
}   

print FOUT '</td></tr>

<tr bgcolor="yellow"><td align="left" class="menuin">Index</td>
        <td align="center">
          <table cellspacing="5">
            <tr class="submenu">
              <td class="subuts"><a href="#topic_ent">Topics</a></td>
              <td class="subuts"><a href="#topic_ixtab">Tables</a></td>
              <td class="subuts"><a href="#topic_imgs">Images</a></td>
            </tr>
          </table>
        </td>
        <td align="right" class="toplink"><a href="#top">top</a></td>


</tr></table>';

# ------------------ the index of topics ----------------------
print FOUT '<blockquote><p class="item"><a name="topic_ent">Index of topics</a></p>';
# calculating total number of lines
$nlin=0;
$nlin+=$topci{$_}+1 foreach (keys(%topci));

$lic=0;

print FOUT "<table width=90% align=center><tr valign=top bgcolor=#f0f0f0><td width=33%><dl>";
foreach (sort {uc($a) cmp uc($b)} keys(%topba))
{
    %roro=split /§/,$topba{$_};

    $topba{$_}=();

    foreach $uiui (sort {uc($a) cmp uc($b)} keys %roro)
    {
	$topba{$_}.=$roro{$uiui};
    }

    print FOUT "<dt class=indextab>$_</dt>$topba{$_}";
    $lic+=$topci{$_}+1;
    if($lic>$nlin/3)
    {
	print FOUT "</dl></td><td width=33%><dl>";
	$lic=0;
    }
}
print FOUT "</dl></td></table></blockquote>";

# ------------------ the index of tables ----------------------
print FOUT '<blockquote><p class="item"><a name="topic_ixtab">Index of tables</a></p>';

# calculating total number of lines
$nlin=0;
$nlin+=$taci{$_}+1 foreach (keys(%taci));

$lic=0;

print FOUT "<table width=90% align=center><tr valign=top bgcolor=#f0f0f0><td width=33%><dl>";
foreach (sort {uc($a) cmp uc($b)} keys(%taba))
{
    %roro=split /§/,$taba{$_};

    $taba{$_}=();

    foreach $uiui (sort {uc($a) cmp uc($b)} keys %roro)
    {
	$taba{$_}.=$roro{$uiui};
    }

    print FOUT "<dt class=indextab>$_</dt>$taba{$_}";
    $lic+=$taci{$_}+1;
    if($lic>$nlin/3)
    {
	print FOUT "</dl></td><td width=33%><dl>";
	$lic=0;
    }
}
print FOUT "</dl></td></table></blockquote>";

# ------------------ the index of images --------------------
print FOUT '<blockquote><p class="item"><a name="topic_imgs">Index of images</a></p>';
# calculating total number of lines
$nlin=0;
$nlin+=$tipci{$_}+1 foreach (keys(%tipci));

$lic=0;

print FOUT "<table width=90% align=center><tr valign=top bgcolor=#f0f0f0><td width=33%><dl>";
foreach (sort {uc($a) cmp uc($b)} keys(%tipba))
{
    %roro=split /§/,$tipba{$_};

    $tipba{$_}=();

    foreach $uiui (sort {uc($a) cmp uc($b)} keys %roro)
    {
	$tipba{$_}.=$roro{$uiui};
    }

    print FOUT "<dt class=indextab>$_</dt>$tipba{$_}";
    $lic+=$tipci{$_}+1;
    if($lic>$nlin/3)
    {
	print FOUT "</dl></td><td width=33%><dl>";
	$lic=0;
    }
}
print FOUT "</dl></td></table></blockquote>";

print FOUT "</body></html>";

close FOUT;

# cleaning up files
print "Tidying phase 2\n";
`tidy -m -i -w 9999 *.html &> /dev/null`;

# linking do index.html
($title,$page,$ref)=split /%/,$menu[0];
`ln -s intro.html index.html`;

print "Packing\n";
$truque=time();
`mv tcpip-slides ../tcpip-slides.$truque`;
`tar czvf LNB-$pakvers.tgz *`;
`mv ../tcpip-slides.$truque tcpip-slides`;
$archivesize=int((-s "LNB-$pakvers.tgz")/1024+0.5);
`mv LNB-$pakvers.tgz ../LNB-$pakvers.tgz.$truque`;
`tar czvf LNB-big-$pakvers.tgz *`;
$bigarchivesize=int((-s "LNB-big-$pakvers.tgz")/1024+0.5);
`mv ../LNB-$pakvers.tgz.$truque LNB-$pakvers.tgz`;

# linking versioned archives
`ln -s LNB-$pakvers.tgz LNB-current.tgz`;
`ln -s LNB-big-$pakvers.tgz LNB-current-big.tgz`;

# repatching intro with archive sizes
open FIN,"intro.html";
open FOUT,">/tmp/intro.html";

while(<FIN>)
{
    s[<i note="archivesize">.*?</i>][<i note="archivesize">$archivesize kB</i>]g;
    s[<i note="bigarchivesize">.*?</i>][<i note="bigarchivesize">$bigarchivesize kB</i>]g;
    print FOUT;
}

close FIN;
close FOUT;
`mv /tmp/intro.html intro.html`;

if($ARGV[0] eq '-u')
{
    print "Uploading...\n";
    `scp LNB-big-$pakvers.tgz beer:/tmp`;
    print "Upload complete.\n";
}


