#! /usr/bin/perl
#
# build_html.pl - Command line utility for auto-building HTML-pages
# Copyright (C) 2003-2007 Tommi Saviranta <wnd@iki.fi>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# Version: build_html.pl v0.3.0  9-Jun-2007  wnd@iki.fi

use strict;
use warnings FATAL => 'all';

my $basedir = ".";
my $file_header = "header.html";
my $file_footer = "footer.html";
# $basedir = $ENV{'basedir'};
my $xhtml = "/";

my $tmpl_header = &read_file($file_header);
my $tmpl_footer = &read_file($file_footer);



sub read_file($) {
	my $r = "";
	open(FILE, "< $_[0]") or die "$!: $_[0]";
	while (<FILE>) {
		$r .= $_;
	}
	close(FILE);
	return $r;
}



sub replace_simple_single($$$) {
	$_ = shift;
	my ($key, $val) = @_;
	$val = "" unless (defined($val));
	s/-\??!?\Q$key\E-/$val/g;
	return $_;
}



sub replace_simple($\%) {
	$_ = shift;
	my %keys = %{(shift)};

	# replace declared tags and...
	while (my ($key, $val) = each %keys) {
		s/-\??\Q$key\E-/$val/g;
		s/-\?!\Q$key\E-/$val/g;
	}

	# removed tags that may not be used on every page
	s/-\?[A-Z]+-//g;

	# remove lines with undefined such tags
	my @t;
	foreach my $line (split(/\n/, $_)) {
		my $pass = 1;
		foreach my $key ($line =~ /-\??!([A-Z]+)-/g) {
			undef $pass unless (defined($keys{$key}));
		}
		$line =~ s/-\??![A-Z]+-.*// if (defined($pass));
		push(@t, $line) if (defined($pass));
	}
	$_ = join("\n", @t);

	return $_;
}



foreach my $file (@ARGV) {
	my %keys;
	my $t_tags;

	my @file_path = split(/\//, $file);
	my $file_in = $file_path[$#file_path];
	$file_in = "_$file_in" unless ($file_in =~ /^_/);
	my $file_out = $file_in;
	$file_out =~ s/_(.*)/$1.html/;

	pop(@file_path);
	$file_in = join("/", @file_path) . "/$file_in";
	$file_out = join("/", @file_path) . "/$file_out";

	print "$file_in -> $file_out\n";

	my $d_content = &read_file($file_in);
	($t_tags, $d_content) = split(/\n\n/s, $d_content, 2);
	my $d_header = $tmpl_header;
	my $d_footer = $tmpl_footer;

	# create root path
	$keys{'ROOTPATH'} = $basedir . "/";
	$keys{'ROOTPATH'} =~ s/\.\///g;
	$keys{'ROOTPATH'} =~ s/^\///g;
	$keys{'ROOTPATH'} = "../" . $keys{'ROOTPATH'} foreach (@file_path);
#	print "root: " . $keys{'ROOTPATH'} . "\n";

	# get modified -date
	my @sb = stat($file_in) or die "$!: $file";
	$keys{'EDITED'} = localtime($sb[10]);

	# get user keys
	foreach (split(/\n/, $t_tags)) {
		my ($key, $val) = split(/\s*=\s*/, $_, 2);
		$keys{$key} = $val;
	}

	# use TITLE as PAGETITLE if not set
	$keys{'PAGETITLE'} = $keys{'TITLE'} if (defined($keys{'TITLE'})
				&& ! defined($keys{'PAGETITLE'}));

	# parse OPTPICTURE
	if (defined($keys{'OPTPICTURE'})) {
		my ($file, $width, $height, $alt) =
			$keys{'OPTPICTURE'} =~ /(.*?) (\d+)[x*](\d+) (.*)/;
		my $t = "<img src=\"$file\" ";
		$t .= "width=\"$width\" " if (defined($width));
		$t .= "height=\"$height\" " if (defined($height));
		$t .= "alt=\"$alt\" " if (defined($height));
		$t .= "$xhtml>";
		$keys{'OPTPICTURE'} = $t;
	}

#	while (my ($key, $val) = each %keys) {
#		print "$key: $val\n";
#	}


	$d_header = &replace_simple($d_header, \%keys);
	$d_footer = &replace_simple($d_footer, \%keys);
	if (defined($keys{'ALSOPARSE'})) {
		foreach my $tag (split(/\s+/, $keys{'ALSOPARSE'})) {
			$d_content = &replace_simple_single($d_content,
					$tag, $keys{$tag});
		}
	}

	open(FILE, "> $file_out") or die "$!: $file_out";
	print FILE "$d_header\n";
	print FILE "$d_content\n";
	print FILE "$d_footer\n";
	close(FILE);
}
