#!/usr/bin/perl
#
# Copyright (c) 2001,2002 Florent Villard (warly@laposte.net)
# Published under the GPL license.
#

use strict;
use vars qw($file $dir $version $WIDTH $LEFT $TOP $P2LEFT $HEADTOP $idx_page $idx_jour);

$ARGV[0] or usage();
$file = $ARGV[0];
$dir = $ARGV[1] || './';
$version = '0.0.1';

my $hit = qq{<script language="JavaScript" SRC="../hit.js"></script>
<script language="JavaScript">
<!--
hit('warly','patriarche','main');
//-->
</script>};

open my $A, "$file" or die "FATAL, could not open $file\n";
open my $INDEX, ">$dir/index.html";

while (<$A>){
    /\/style/ and last
}

($WIDTH,$LEFT,$TOP,$P2LEFT,$HEADTOP) = (350,20,30,410,10);

my $header = qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Le Patriarche</title>
<link rel="stylesheet" type="text/css" href="index.css">
<style type="text/css">	
      A:link { color:#F73 }
      A:active { color:#000 }
      A:visited { color:#F73 }
      #p1 {
      font-size: 20px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      text-align: center;
      position: absolute; 
      left: ${LEFT}px; 
      top: ${TOP}px; 
      width: ${WIDTH}px;
      z-index: 1; 
       }
      #p2 {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      position: relative; 
      left: ${LEFT}px; 
      top: 400px; 
      width: ${WIDTH}px;
      }
      #div_toc {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      position: absolute;
      left: ${P2LEFT}px; 
      top: 50px; 
      width: ${WIDTH}px;
      }
      #toc {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      position: relative;
      left: 0px; 
      top: 50px; 
      width: ${WIDTH}px;
      }
      #toc_header {
      font-size: 16px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      text-align: center;
      position: absolute; 
      left: ${P2LEFT}px; 
      top: 10px; 
      width: ${WIDTH}px;
      }
      #part {
      font-size: 14px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      text-align: center;
      position: relative; 
      left: ${P2LEFT}px; 
      top: 10px; 
      width: ${WIDTH}px;
      }
      #p6 {
      color: #000;
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      position: absolute;
      left: 900px; 
      top: 0px;
      width: 0px;
      z-index: 2; 
      }   </style>
</head>
<body bgcolor="#ffffff">
<p id=p6>
</p>};
my $header_page = qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Le Patriarche</title>
<link rel="stylesheet" type="text/css" href="index.css">
<style type="text/css">	
      A:link { color:#F73 }
      A:active { color:#000 }
      A:visited { color:#F73 }
      #p1 {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      position: absolute; 
      left: 20px; 
      top: ${TOP}px; 
      width: ${WIDTH}px;
      z-index: 1; 
       }
      #p2 {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      position: absolute; 
      left: ${P2LEFT}px; 
      top: ${TOP}px; 
      width: ${WIDTH}px;
      }
      #chapter {
      font-size: 14px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      text-align: left;
      }
      #section {
      font-size: 12px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      text-align: left;
      }
      #day {
      font-size: 12px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      text-align: left;
      }
      #para {
      font-size: 12px;
      font-family: helvetica;
      font-style: normal;
      text-align: justify;
      }
      #page_left {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      position: absolute; 
      left: 10px; 
      top: ${HEADTOP}px; 
      width: ${WIDTH}px;
      }
      #page_right {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      position: absolute; 
      left: 420px; 
      top: ${HEADTOP}px; 
      width: ${WIDTH}px;
      }
    </style>
</head>
<body bgcolor="#ffffff">
};
my $header_part = qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Le Patriarche</title>
<link rel="stylesheet" type="text/css" href="index.css">
<style type="text/css">	
      A:link { color:#F73 }
      A:active { color:#000 }
      A:visited { color:#F73 }
      #p1 {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      text-align: left;
      position: absolute; 
      left: 20px; 
      top: 50px; 
      width: ${WIDTH}px;
      z-index: 1; 
       }
      #p2 {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      text-align: left;
      position: absolute; 
      left: ${P2LEFT}px; 
      top: 50px; 
      width: ${WIDTH}px;
      }
      #part {
      font-size: 28px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      text-align: center;
      }
      #page_left {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      position: absolute; 
      left: 10px; 
      top: ${HEADTOP}px; 
      width: ${WIDTH}px;
      }
      #page_right {
      font-size: 10px;
      font-family: helvetica;
      font-style: normal;
      font-weight: bold;
      position: absolute; 
      left: 410px; 
      top: ${HEADTOP}px; 
      width: ${WIDTH}px;
      }
   </style>
</head>
<body bgcolor="#ffffff">
};
print $INDEX $header;

while (<$A>){
    /div id="d2"/ and last;
    s,Le Patriarche,<a href="../index.html">Le Patriarche</a>,;
    print $INDEX $_
}
open my $PAGE, ">$dir/vie-1.html";

print_head($PAGE, 1, $header_page);
#print $PAGE qq{$header_page
#<p id=page_left><a href="index.html">1</a></p>
#<p id=head><a href="index.html">Le Patriarche</a></p>
#<p id=page_right><a href="vie-3.html">2</a></p>
#<p id=page_right><font align=center><a href="index.html">Le Patriarche</a></font></p>
#<div id=p1>
#};
$idx_page = qq{<p id=toc_header>Index</p><div id="div_toc"><p id=toc>Pages<br></br>\n<a href="vie-1.html">1</a>
<a href="vie-1.html"> 2</a>\n};
$idx_jour = qq{<p id=toc_header></p><p id=toc>Jours<br></br>\n};
while (<$A>){
    /<p id="(part|chapter)">/ and last;
    /-- Ancien texte --/ and $_ = 0 and last;
    print $PAGE $_
}
close $PAGE;

my $page = 3;
if ($_) { 
    print_part(3, $_, m,</p>, ? '' : get_subtitle($A));
    $page += 2
}
open my $PAGE, ">$dir/vie-$page.html";
$idx_page .= qq{<a href="vie-$page.html">$page</a>
<a href="vie-$page.html">} . ($page+1) . " </a>\n";
my ($line,$new_page);
print_head($PAGE, $page,$header_page);
$line = 10;
print $PAGE "<br>"x10;
while (<$A>) {
    if ($line > 50 && !$new_page){
	print $PAGE "</p></div><div id=p2><p id=para>";
	$new_page = 1
    }
    if ($line > 100){
	print $PAGE "</p></div>$hit</body>\n";
	new_page()
    }
    if (/<p id="day">(.+)<\/p>/){
	$idx_jour .= qq{<a href="vie-$page.html"> $1</a>}
    }
    if (/<p id="(part|chapter)"> Table des matires/){
	<$A>
    } elsif (/<p id="(part|chapter)">.*<\/a>(.*)/){
	$page += 2;
	print_part($page, $2, get_subtitle($A));
	new_page();
	$line = 10;
	print $PAGE "<br>"x10
    } else {
	$line += 1.3 if /<\/p>/;
	$line += length() / 53;
	print $PAGE $_
    }
}

close $PAGE;

print $INDEX "$idx_page</p>$idx_jour</p></div>$hit</body>\n";
exit;

sub new_page {
    $page += 2;
    open $PAGE, ">$dir/vie-$page.html";
    print_head($PAGE, $page, $header_page);
    $idx_page .= qq{<a href="vie-$page.html">$page</a>
    <a href="vie-$page.html">} . ($page + 1) . "</a>\n";
    $line = 0;
    $new_page = 0
}

# replaced because some links where not usable with mozilla
sub print_head_old {
    my ($page, $header) = @_;
    print $page qq{$header};
    print $page qq{<p id=page_left align=left><a href="vie-} . ($page-2) . qq{.html">page } . ($page-1) . qq{</a></p>} if $page > 1;
    print $page qq{<p id=page_left align=right>} . ($page) . qq{</p>
<p id=page_left align=center><a href="index.html">le patriarche</a></p>
<p id=page_right align=right><a href="vie-} . ($page+2) . qq{.html">page } . ($page+2) . qq{</a></p>
<p id=page_right align=left>} . ($page+1) . qq{</p>
<p id=page_right align=center><a href="index.html">le patriarche</a></p><div id=p1><p id=para>
};
}

sub print_head {
    my ($PAGE, $page, $header) = @_;
    print $PAGE qq{$header\n};
    print $PAGE qq{<div id=page_left><p align=left>
    <table width=100%><tr>};
    if ($page > 1) {
	print $PAGE qq{      <td><a href="vie-} . ($page-2) . qq{.html">page } . ($page-1) . qq{</a></td>\n}
    } else {
	print $PAGE qq{      <td> </td>\n}
    }
    print $PAGE qq{      <td align=center><a href="index.html">le patriarche</a></td>
      <td align=right>} . ($page) . qq{</td>
    </tr></table></div>
  <div id=page_right>
  <p align=right>
    <table width=100%><tr>
      <td>} . ($page+1) . qq{</td>
      <td align=center><a href="index.html">le patriarche</a></td>
      <td align=right><a href="vie-} . ($page+2) . qq{.html">page } . ($page+2) . qq{</a></td>
    </tr></table></div>
    <div id=p1><p id=para>
};
}

sub print_part {
    my ($page, $title, $subtitle) = @_;
    open $PAGE, ">$dir/vie-$page.html";
    $title =~ s,<a.*/a>,,;
    $title =~ s/<.?br>//g;
    $title =~ s/<.?p[^>]*>//g;
    print_head($PAGE, $page, $header_part);
    print $PAGE "</p></div><div id=p2><p id=part>$title<br>$subtitle</p></div>$hit</body>";
    $idx_page .= qq{<br></br><br></br>$title } . (($subtitle !~ /^\s*$/) ? "($subtitle) " : "") . qq{ : <a href="vie-$page.html">$page</a>
<a href="vie-$page.html">} . ($page+1) . qq{</a>\n};
    close $PAGE
}

sub get_subtitle {
    my ($A) = @_;
    my $sub;
    while (my $t = <$A>) {
	$sub .= $t;
	$t =~ m,</p>, and last
    }
    $sub =~ s,</p>,,g;
    $sub
}

sub usage {
    print "
    viediff version $version
	
	usage:
		viediff <html file> [<output dir name>]
\n";
    exit
}
