#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Copyright: GPL
# Written by Guido Socher <guido.socher@linuxfocus.org>
#
use strict;
use vars qw($opt_L $opt_h);
use Getopt::Std;
my $head;
my $content;
my $bfile;
my $ver="version 0.5";
#
&getopts("Lh")||die "ERROR: No such option. -h for help.n";
&help if ($opt_h);
&help unless ($ARGV[0]);

open(SKEL,$ARGV[0])||die "ERROR con not read $ARGV[0]\n";
while(<SKEL>){
    print;
    if (/macro\s+insert\s+boxes/){
        foreach $bfile (&boxfiles(&basedir($ARGV[0]))){
            &readboxtemplate($bfile);
            print "<!-- $bfile -->\n<TR><TD>\n <TABLE CELLSPACING=1 CELLPADDING=3 BORDER=0 BGCOLOR=\"#FFFFFF\" ALIGN=\"CENTER\" WIDTH=\"100%\">\n";
            print " <TR>\n  <TD BGCOLOR=\"#113366\"><FONT COLOR=\"#FFFFFF\" SIZE=\"+1\">\n";
            print "  <!-- TABLE HEAD -->\n";
            print "    <i>$head</i>\n  <!-- END TABLE HEAD -->\n";
            print "  </FONT></TD>\n </TR>\n";
            print " <TR><TD>\n$content\n </TD></TR>\n </TABLE>\n</TD></TR>\n";
            print "<!-- end $bfile -->\n\n";
        }
    }
    if (/macro\s+insert\s+body/){
        foreach $bfile (&bodyfiles(&basedir($ARGV[0]))){
            open(BODY,$bfile)||die "ERROR con not read $bfile\n";
            $content=join("",<BODY>);
            $content=~s/_LF_/Linux<font color=\"#FF0000\">Focus<\/font>/g;
            &htmlumlaute(\$content);
            close BODY;
            print "<!-- $bfile -->\n<BR>\n";
            print "$content\n";
            print "<!-- end $bfile -->\n";
        }
    }
}
close SKEL;
#--------
# get files which match the name body from the directory
#--------
sub bodyfiles($){
    my $dir=shift;
    my @result;
    my $file;
    opendir(DIR,$dir)||die "ERROR: can not read directory $dir\n";
    foreach $file (readdir(DIR)){
        if ($file=~/body.+html/){
            $file="$dir/$file";
            push(@result,$file);
        }
    }
    closedir DIR;
    die "ERROR: no files with name SOMETHING.body.SOMETHING.html in directoy $dir\n" unless(@result);
    return(sort @result);
}
#--------
# get files which match the name box from the directory
#--------
sub boxfiles($){
    my $dir=shift;
    my @result;
    my $file;
    opendir(DIR,$dir)||die "ERROR: can not read directory $dir\n";
    foreach $file (readdir(DIR)){
        if ($file=~/box.+html/){
            $file="$dir/$file";
            push(@result,$file);
        }
    }
    closedir DIR;
    die "ERROR: no files with name SOMETHING.box.SOMETHING.html in directoy $dir\n" unless(@result);
    return(sort @result);
}
#--------
# get the base dir from a file name
#--------
sub basedir($){
    my $file=shift;
    if ($file=~m|(.+)/|){
        return($1);
    }else{
        return(".");
    }
}
#--------
# read a box template and take <h2>...</h2> as box header and
# all text that follows as box content.
# Writes to the global vaiables $head and $content
#--------
sub readboxtemplate($){
    my $file=shift;
    my $dat;
    open(BOX,$file)||die "ERROR con not read $file\n";
    $dat=join("",<BOX>);
    close BOX;
    if ($dat=~ m/<[hH]2>(.+?)<\/[hH]2>/s){
        $head=$1;
        $head=~s/_LF_/Linux<font color=\"#FF0000\">Focus<\/font>/g;
        &htmlumlaute(\$head);
        $content=$';
        $content=~s/_LF_/Linux<font color=\"#FF0000\">Focus<\/font>/g;
        &htmlumlaute(\$content);
    }else{
        die "ERROR: $file does not start with <h2>...</h2>\n";
    }
}
#--------------
sub htmlumlaute($){
    my $txt_ptr=shift;
    return 0 if ($opt_L);
	$$txt_ptr=~s//\&iexcl;/g;
	$$txt_ptr=~s//\&iquest;/g;
	$$txt_ptr=~s//\&Agrave;/g;
	$$txt_ptr=~s//\&Aacute;/g;
	$$txt_ptr=~s//\&Acirc;/g;
	$$txt_ptr=~s//\&Atilde;/g;
	$$txt_ptr=~s//\&Auml;/g;
	$$txt_ptr=~s//\&Aring;/g;
	$$txt_ptr=~s//\&Ccedil;/g;
	$$txt_ptr=~s//\&Egrave;/g;
	$$txt_ptr=~s//\&Eacute;/g;
	$$txt_ptr=~s//\&Ecirc;/g;
	$$txt_ptr=~s//\&Euml;/g;
	$$txt_ptr=~s//\&Igrave;/g;
	$$txt_ptr=~s//\&Iacute;/g;
	$$txt_ptr=~s//\&Icirc;/g;
	$$txt_ptr=~s//\&Iuml;/g;
	$$txt_ptr=~s//\&Ntilde;/g;
	$$txt_ptr=~s//\&Ograve;/g;
	$$txt_ptr=~s//\&Oacute;/g;
	$$txt_ptr=~s//\&Ocirc;/g;
	$$txt_ptr=~s//\&Otilde;/g;
	$$txt_ptr=~s//\&Ouml;/g;
	$$txt_ptr=~s//\&Oslash;/g;
	$$txt_ptr=~s//\&Ugrave;/g;
	$$txt_ptr=~s//\&Uacute;/g;
	$$txt_ptr=~s//\&Ucirc;/g;
	$$txt_ptr=~s//\&Uuml;/g;
	$$txt_ptr=~s//\&Yacute;/g;
	$$txt_ptr=~s//\&szlig;/g;
	$$txt_ptr=~s//\&agrave;/g;
	$$txt_ptr=~s//\&aacute;/g;
	$$txt_ptr=~s//\&acirc;/g;
	$$txt_ptr=~s//\&atilde;/g;
	$$txt_ptr=~s//\&auml;/g;
	$$txt_ptr=~s//\&aring;/g;
	$$txt_ptr=~s//\&aelig;/g;
	$$txt_ptr=~s//\&ccedil;/g;
	$$txt_ptr=~s//\&egrave;/g;
	$$txt_ptr=~s//\&eacute;/g;
	$$txt_ptr=~s//\&ecirc;/g;
	$$txt_ptr=~s//\&euml;/g;
	$$txt_ptr=~s//\&igrave;/g;
	$$txt_ptr=~s//\&iacute;/g;
	$$txt_ptr=~s//\&icirc;/g;
	$$txt_ptr=~s//\&ntilde;/g;
	$$txt_ptr=~s//\&ograve;/g;
	$$txt_ptr=~s//\&oacute;/g;
	$$txt_ptr=~s//\&ocirc;/g;
	$$txt_ptr=~s//\&ouml;/g;
	$$txt_ptr=~s//\&ugrave;/g;
	$$txt_ptr=~s//\&uacute;/g;
	$$txt_ptr=~s//\&ucirc;/g;
	$$txt_ptr=~s//\&uuml;/g;
}
#--------------
#--------
sub help{
print STDERR "lfpagecomposer -- a simple macro expander for LinuxFocus index pages.
lfpagecomposer takes a skeleton file as argument and inserts the
content of all *body*.html files in the same directory after the line
<!-- macro insert body --> in the skeleton. lfpagecomposer looks also 
for <!-- macro insert boxes --> in the skeleton file and generates boxes
form the files *box*.html files. These box files must have a <h2>...</h2> at the
beginning. This will be put into the box header and the text after </h2>
into the box body. 
The *body*.html and *box*.html files are processed in alphabetical order
(same order as \"ls *body*.html *box*.html\").

The special character combination _LF_ is replaced by LinuxFocus

USAGE: lfpagecomposer [-L][-h] skeletonfile.html

OPTIONS: -h this help
         -L do not convert latin-1 characters into &xxx; combinations.
         (useful for non latin languages)

EXAMPLE: lfpagecomposer skel.html > index.html

The homepage of lfpagecomposer can be reached form 
http://www.linuxfocus.org/developer/Guido/

$ver
\n";
exit;
}
__END__ 

