#!/usr/bin/perl -w
# Copyright: GPL2
# Written by Egon Willighagen (egon.w@linuxfocus.org)
#
use strict;
use vars qw($opt_h);
use Getopt::Std;
use LinuxFocus::SDB;

my $ver="version 2.2";

# Version 2.0 starts making use of the new DB format

my $lfdir = $ENV{'LFDIR'};
my $homedir = "$lfdir/Nederlands";
my $maindb = "$homedir/data/mainindex.def.nl.txt";
my $newdb = "$homedir/data/maindb_nl.txt";
my $issuemark = "Titelpagina";

my $translated = "Vertaald";
my $untranslated = "Onvertaald";
my $transby = "door";

my $xml2any = "$homedir/WorkSpace/bin/xml2any";

&getopts("h") || die "ERROR: No such option. Use -h to display valid options.$/";

&help if ($opt_h);
&help unless ($ARGV[0]);

my $file = $ARGV[0];
die "ERROR: No such file ($file).$/" if (!-e $file);

my $target = "out.html";
if ($file =~ /(.*?)\.(\w*)/) {
    $target = "$1.html";
}

open(OUT, ">$target");
open(INPUT, "<$file");
my $line = "";
while ($line = <INPUT>) {
    if ($line =~ m/\<\!\-\-\s*macro\s*insert\s*(.*?)\s*\-\-\>/i) {
	# try to expand macro
        my $macro = $1;
	#print "Macro: $macro$/";
        if (($macro eq "body") || ($macro eq "boxes")) {
            # ignore these lfpagecomposer macro's
	} elsif ($macro eq "month") {
            my ($month, $month_local) = getMonth();
            $line =~ s/\<\!\-\-\s*macro\s*insert\s*(.*)\s*\-\-\>/$month_local/ig;
	} elsif ($macro eq "issue-toc") {
            my $toc = getIssueToc();
            $line =~ s/\<\!\-\-\s*macro\s*insert\s*(.*)\s*\-\-\>/$toc/ig;
	} elsif ($macro eq "previous_issues") {
            my $toc = getPreviousIssues();
            $line =~ s/\<\!\-\-\s*macro\s*insert\s*(.*)\s*\-\-\>/$toc/ig;
	} elsif ($macro eq "recently_translated") {
            my $list = getRecentlyTranslated();
            $line =~ s/\<\!\-\-\s*macro\s*insert\s*(.*)\s*\-\-\>/$list/ig;
	} else {
            print STDERR "ERROR: unknown macro ($macro)!$/";
	    $line =~ s/\<\!\-\-\s*macro\s*insert\s*(.*)\s*\-\-\>//ig;
	}
    }
    print OUT $line;
}
close(INPUT);
close(OUT);

1;

sub getMonth {
    my $month = "";
    my $month_local = "";
    my $toc = "";

    my $db = LinuxFocus::SDB->new();
    $db->load("../../data/lfdb.nl.xml");
    $db->load("../../data/lfdb.en.xml");
    $db->setLang("nl");

    $toc = $db->getCurrentMonthID();
    #print STDERR "DEBUG: $toc$/";
    return ($toc);
}

sub getIssueToc {
    my $toc = `$xml2any -IN file://$homedir/data/lfdb.nl.xml -XSL file://$homedir/data/issuetoc.xslt`;

    return ($toc);
}

sub getPreviousIssues {
    my $month = "";
    my $month_local = "";
    my $toc = "";

    my $db = LinuxFocus::SDB->new();
    $db->load("../../data/lfdb.nl.xml");
    $db->load("../../data/lfdb.en.xml");
    $db->setLang("nl");

    foreach my $month ($db->getMonthIDs(sort => "desc")) {
	if (-e "../../$month/index.html") {
	    $month_local = $db->getMonthName($month, "nl");    
	    $toc .= "  <li><a href=\"$month/index.html\">$month_local</a>$/";
	}
    }    

    return ($toc);
}

sub getRecentlyTranslated {
  my $toc = `$xml2any -IN file://$homedir/data/lfdb.nl.xml -XSL file://$homedir/data/recent.xslt`;
 
  return ($toc);
}

sub help {
print STDERR "lfboxcomposer -- a simple macro expander for LinuxFocus box pages.

lfboxcomposer takes a box*.html.pre file and expands the <!-- macro insert -->
commands.

USAGE: lfboxcomposer [-h] box*.html.pre

$ver
\n";
exit;
}













