#!/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_h);
use Getopt::Std;
my $head;
my $content;
my $bfile;
my $ver="version 0.4";
#
&getopts("h")||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=0 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>);
            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;
        $content=$';
    }else{
        die "ERROR: $file does not start with <h2>...</h2>\n";
    }
}
#--------
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\").

USAGE: lfpagecomposer [-h] skeletonfile.html

EXAMPLE: lfpagecomposer skel.html > index.html

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

$ver
\n";
exit;
}
__END__ 

