#!/usr/bin/perl 
#
# Generate an html version of The Linux Kernel
#
use Getopt::Long;

sub generateTitle {
    print STDERR "Generating title page (tlk-title.html)\n";

    open TITLEFILE, ">tlk-title.html" or die "Could not open tlk-title.html: $!";
    print TITLEFILE "<HTML>\n";
    print TITLEFILE "<HEAD>\n";
    print TITLEFILE "<TITLE>The Linux Kernel</TITLE>\n";
    print TITLEFILE "</HEAD>\n";
    print TITLEFILE "<BODY>\n";
    print TITLEFILE "<center><h1>The Linux Kernel</h1></center>\n";
    print TITLEFILE "<center> <img src=\"logos/sit3-bwo-tran.1.gif\"><br> </center><p>\n" ;
    print TITLEFILE "<b>This book is for Linux enthusiasts who want to know how the Linux kernel\n";
    print TITLEFILE "works. It is not an internals manual.  Rather it describes the principles\n"; 
    print TITLEFILE "and mechanisms that Linux uses; how and why the Linux kernel works the\n";
    print TITLEFILE "way that it does. <p>Linux is a moving target; this book is based upon the\n";
    print TITLEFILE "current, stable, 2.0.33 sources as those are what most individuals and companies\n";
    print TITLEFILE "are now using.</b>\n";
    print TITLEFILE "<p>\n";
    print TITLEFILE "This book is freely distributable, you may copy and redistribute it under certain conditions.\n";
    print TITLEFILE "Please refer to the <A HREF=\"misc/copyright.html\"> copyright and distribution statement</a>.\n";
    print TITLEFILE "<p>\n";
    print TITLEFILE "<center>\n";
    print TITLEFILE "Version 0.8-3<br>\n";
    print TITLEFILE "David A Rusling<br>\n";
    print TITLEFILE "<A HREF=\"mailto:david.rusling\@arm.com\">david.rusling\@arm.com</A>)\n";
    print TITLEFILE "</center>\n";
    print TITLEFILE "<hr>\n";
    print TITLEFILE "<center>\n";
    print TITLEFILE "<A HREF=\"tlk-toc.html\">Table of Contents</A>, \n"; 
    print TITLEFILE "<A href=\"tlk.html\" target=\"_top\"> Show Frames</A>, \n";
    print TITLEFILE "<A href=\"tlk-title.html\" target=\"_top\"> No Frames</A><br> \n";
    print TITLEFILE " 1996-1999 David A Rusling <A HREF=\"misc/copyright.html\">copyright notice</a><br>\n";
    print TITLEFILE "</center>\n";
    print TITLEFILE "</BODY>\n";
    print TITLEFILE "</HTML>\n";
    close TITLEFILE;
}

sub generateIndex {
    print STDERR "Generating main page (tlk.html)\n";

    open INDEXFILE, ">tlk.html" or die "Could not open tlk.html: $!";
    print INDEXFILE "<HTML>\n";
    print INDEXFILE "<HEAD>\n";
    print INDEXFILE "<TITLE>The Linux Kernel</TITLE>\n";
    print INDEXFILE "</HEAD>\n";
    print INDEXFILE "<frameset cols=\"30%,*\">\n";
    print INDEXFILE "<frame src=\"tlk-toc.html\">\n";
    print INDEXFILE "<frame src=\"tlk-title.html\" name=\"view_frame\">\n";
    print INDEXFILE "<noframes>\n";
    print INDEXFILE "<center>\n";
    print INDEXFILE "<h2><A HREF=\"tlk-title.html\"> Title Page</A></h2>\n";
    print INDEXFILE "<h2><A HREF=\"tlk-toc.html\"> Table of contents</A></h2>\n";
    print INDEXFILE "</center>\n";
    print INDEXFILE "</noframes>\n";
    print INDEXFILE "</frameset>\n";
    print INDEXFILE "</HTML>\n";
    close INDEXFILE
}

sub generateTOC {
    my $file ;
    my $name ;

    print STDERR "Generating table of contents (tlk-toc.html)\n";

    open TOCFILE, ">tlk-toc.html" or die "Could not open tlk-toc.html: $!";
    print TOCFILE "<HTML>\n";
    print TOCFILE "<HEAD>\n";
    print TOCFILE "<TITLE>The Linux Kernel: Table of Contents</TITLE>\n";
    print TOCFILE "</HEAD>\n";
    print TOCFILE "<BODY>\n";

    print TOCFILE "<p>\n";
    print TOCFILE "<center><i><h3>Table of Contents</h3></i></center>\n";
    print TOCFILE "<hr>\n";
    print TOCFILE "<UL>\n";
    print TOCFILE "<li><a href=\"tlk-title.html\" target=\"view_frame\"> Title Page</A>\n";
    foreach $part (@_) {
	($chapter, $name, $file) = split ':', $part, 3; 
	print TOCFILE "<LI><A HREF=\"$file.html\" target=\"view_frame\">$name</A>\n"; 
    }
    print TOCFILE "</UL>\n";

    print TOCFILE "<hr>\n";
    print TOCFILE "<center><b><i>\n";
    print TOCFILE "David A Rusling<br>\n";
    print TOCFILE "3 Foxglove Close,<br>\n";
    print TOCFILE "Wokingham,<br>\n";
    print TOCFILE "Berkshire RG41 3NF,<br>\n";
    print TOCFILE "United Kingdom<br>\n";
    print TOCFILE "<p>\n";
    print TOCFILE "</B></I></CENTER>\n";

    print TOCFILE "<hr>\n";
    print TOCFILE "<center>\n";
    print TOCFILE "<A href=\"tlk.html\" target=\"_top\"> Show Frames</A>, \n";
    print TOCFILE "<A href=\"tlk-toc.html\" target=\"_top\"> No Frames</A><br> \n";
    print TOCFILE " 1996-1999 David A Rusling <A HREF=\"../misc/copyright.html\">copyright notice</a><br>\n";
    print TOCFILE "<A HREF=\"mailto:david.rusling\@arm.com\">david.rusling\@arm.com</A>\n";
    print TOCFILE "</center>\n";

    print TOCFILE "</BODY>\n";
    print TOCFILE "</HTML>\n";
    close TOCFILE;
}

#
# Convert the source files into HTML
#
sub convert2HTML {
    my ($chapter, $texfile) = (@_) ;

    print STDERR "Processing [$chapter]: $file.tex\n";
    system("./misc/htmlchapter -c=$chapter $file.tex | ./tth/tth -e2 > $file.html") ;
}

#
# add back references to the contents and title pages
#
sub addReferences {
    my ($chapter, $htmlfile) = (@_) ;
    
    print STDERR "Adding back references to $htmlfile\n";
    open INFILE, "$htmlfile" or die "Could not open $htmlfile: $!";
    open OUTFILE, ">$htmlfile~" or die "Could not open $htmlfile~: $!";

    while ($line = <INFILE>) {
      # check for the start of the html
      if ($line eq "<HTML>\n") {
	print OUTFILE $line;
	print OUTFILE "<center>\n";
	print OUTFILE "<A HREF=\"../tlk-toc.html\"> Table of Contents</A>, \n"; 
	print OUTFILE "<A href=\"../tlk.html\" target=\"_top\"> Show Frames</A>, \n";
	print OUTFILE "<A href=\"../$htmlfile\" target=\"_top\"> No Frames</A> \n";
	print OUTFILE "</center>\n";
	print OUTFILE "<hr>\n";
      } else {
	# check for the end of the html
	if ($line eq "</HTML>") {
	  print OUTFILE "<hr>\n";
	  print OUTFILE "<center>\n";
	  print OUTFILE "<A HREF=\"../$htmlfile\"> Top of Chapter</A>, \n"; 
	  print OUTFILE "<A HREF=\"../tlk-toc.html\"> Table of Contents</A>, \n"; 
	  print OUTFILE "<A href=\"../tlk.html\" target=\"_top\"> Show Frames</A>, \n";
	  print OUTFILE "<A href=\"../$htmlfile\" target=\"_top\"> No Frames</A><br> \n";
	  print OUTFILE " 1996-1999 David A Rusling <A HREF=\"../misc/copyright.html\">copyright notice</a>.\n";
	  print OUTFILE "</center>\n";
	  print OUTFILE $line;
	} else {
	  print OUTFILE $line;
	}
      } 
    }


    close INFILE;
    close OUTFILE;
    system("mv -f $htmlfile~ $htmlfile");
}

#
# Cleanup the HTML generated.
#
sub cleanHTML {
    my ($chapter, $htmlfile) = (@_) ;

    print STDERR "Cleaning up $htmlfile\n";

    open INFILE, "$htmlfile" or die "Could not open $htmlfile: $!";
    open OUTFILE, ">$htmlfile~" or die "Could not open $htmlfile~: $!";
    while ($line = <INFILE>) {
#
# Get the relative references right (tedius)
#
	$line =~ s@src="basics/@src="@g;
	$line =~ s@src="mm/@src="@g;
	$line =~ s@src="kernel/@src="@g;
	$line =~ s@src="ipc/@src="@g;
	$line =~ s@src="dd/@src="@g;
	$line =~ s@src="fs/@src="@g;
	$line =~ s@src="modules/@src="@g;
	$line =~ s@src="net/@src="@g;
	$line =~ s@src="sources/@src="@g;
	$line =~ s@src="ds/@src="@g;
	$line =~ s@src="processors/@src="@g;
	$line =~ s@src="appendices/@src="@g;
	$line =~ s@src="logos/@src="../logos/@g;

#
# hack chapter titles
#
	$line =~ s@Chapter 0     <br>@@g;

#
# hack the inter-chapter references
#
	$line =~ s@href="#sources-chapter@href="../sources/sources.html@g;
	$line =~ s@href="#modules-chapter@href="../modules/modules.html@g;
	$line =~ s@href="#kernel-chapter@href="../kernel/kernel.html@g;
	$line =~ s@href="#hw-basics-chapter@href="../basics/hw.html@g;
	$line =~ s@href="#network-chapter@href="../net/net.html@g;
	$line =~ s@href="#dd-chapter@href="../dd/drivers.html@g;
	$line =~ s@href="#interrupt-chapter@href="../dd/interrupts.html@g;
	$line =~ s@href="#sw-basics-chapter@href="../basics/sw.html@g;
	$line =~ s@href="#mm-chapter@href="../mm/memory.html@g;
	$line =~ s@href="#IPC-chapter@href="../ipc/ipc.html@g;
	$line =~ s@href="#PCI-chapter@href="../dd/pci.html@g;
	$line =~ s@href="#dd-chapter@href="../dd/drivers.html@g;
	$line =~ s@href="#processors-chapter@href="../processors/processors.html@g;
	$line =~ s@href="#filesystem-chapter@href="../fs/filesystem.html@g;
	$line =~ s@href="#www-appendix@href="../appendices/www.html@g;
	$line =~ s@href="#LPD-appendix@href="../appendices/LPD-manifesto.html@g;
	print OUTFILE $line;
    }
    close INFILE;
    close OUTFILE;
    system("mv -f $htmlfile~ $htmlfile");
}

#
# The list of files that we are going to play with
@files = (
	  "0:Preface:intro/preface",
	  "1:Hardware Basics:basics/hw",
	  "2:Software Basics:basics/sw",
	  "3:Memory Management:mm/memory",
	  "4:Processes:kernel/processes",
	  "5:Interprocess Communication Mechanisms:ipc/ipc",
	  "6:PCI:dd/pci",
	  "7:Interrupts and Interrupt Handling:dd/interrupts",
	  "8:Device Drivers:dd/drivers",
	  "9:The File System:fs/filesystem",
	  "10:Networks:net/net",
	  "11:Kernel Mechanisms:kernel/kernel",
	  "12:Modules:modules/modules" ,
	  "13:Processors:processors/processors",
	  "14:The Linux Kernel Sources:sources/sources",
	  "15:Linux Data Structures:ds/ds",
	  "16:Useful Web and FTP Sites:appendices/www",
	  "17:The LPD Manifesto:appendices/LDP-Manifesto",
	  "18:The GNU General Public License:appendices/gpl",
	  "19:Glossary:appendices/glossary"
	  ) ;

#
# The list of gifs that we'll need
@pictures = (  	"basics/board.fig",
		"mm/vm.fig", "mm/pte.fig",  "mm/vm_area.fig",  
		"mm/free-area.fig",  "mm/vm-axp.fig",  
		"mm/page-tables.fig",  "mm/mm.fig",  
		"mm/shm.fig",  "mm/page-cache.fig",  
		"dd/pci-structures.fig",  
		"dd/pci-system.fig",  "dd/pci-bars.fig",  "dd/pci-config-header.fig",  
		"dd/pci-pci-config-eg-1.fig",  "dd/pci-pci-config-eg-2.fig",  
		"dd/pci-pci-config-eg-3.fig",  "dd/pci-pci-config-eg-4.fig", 
		"dd/pci-cycle-0.fig",  "dd/pci-cycle-1.fig",  
		"dd/interrupts.fig",  "dd/interrupt-handling.fig",  
		"dd/blk_dev.fig",  "dd/gendisk.fig",  
		"dd/scsi.fig",  "dd/chrdevs.fig", 
		"fs/ext2_inode.fig",  "fs/ext2.fig",  "fs/ext2_dir.fig",  
		"fs/file-systems.fig",  "fs/vfs.fig",  "fs/mounted.fig",  
		"fs/buffer-cache.fig",  
		"modules/modules.fig",  
		"kernel/bh.fig",  "kernel/tqueue.fig",  "kernel/timers.fig",  
		"kernel/binfmt.fig",  "kernel/files.fig",  "ipc/pipes.fig",  
		"kernel/process-vm.fig",  "kernel/elf.fig",  
		"ipc/msg.fig",  "ipc/shm.fig",  "ipc/sem.fig",  
		"net/protocols.fig",  "net/fib.fig",  "net/sk_buff.fig",  
		"net/layers.fig",  "net/sockets.fig"
) ;

# Generate an index page
generateIndex() ;

# Generate a Title page
generateTitle() ;

# Generate a table of contents
generateTOC(@files) ;

# generate the gif files
system("make figures") ;

# convert each file into HTML
foreach $part (@files) {
    ($chapter, $name, $file) = split ':', $part, 3; 
    convert2HTML($chapter, $file) ;

# Clean up the HTML
    cleanHTML($chapter, "$file.html") ;

# Add references
    addReferences($chapter, "$file.html") ;
}


