#!/usr/bin/perl -w
#
# DECORATE
#
# This script provides a set of "bare" HTML files (which contain the things
# you'd put between <BODY>...</BODY>) with standard decorations. Makes it 
# easy to use style templates, without stylesheets, server-side includes or
# scripts.
#
# The input files are named on the command line; last argument is the target
# directory.
#
# Input documents may contain any number of metadata items. Each item starts 
# with <!-- DECO and may contain an arbitrary number of attr=value pairs 
# before the comment is closed. 
#
# Available attributes are currently:
#
# TITLE			- Defines <TITLE> element in header
# DESCRIPTION		- Defines <META ITEM="DESCRIPTION"> element in header
# KEYWORDS		- Defines <META ITEM="KEYWORDS"> element in header
# LOGO			- Defines <IMG SRC=...> element for title
#
#
# Author: Emile van Bergen, emile@evbergen.xs4all.nl
#
##
# 
# Configure: document header

$BORDER=0;

sub stdopen {
	(my $pairs) = @_;

	my $ret = "<HTML>\n\n<!-- Document generated by decorate.pl -->\n\n";	
	$ret .= "<HEAD>\n";
	$ret .= "<TITLE>$$pairs{'title'}</TITLE>\n" if $$pairs{'title'};
	$ret .= "<META NAME=DESCRIPTION CONTENTS=\"$$pairs{'description'}\">\n"
		if $$pairs{'description'};
	$ret .= "<META NAME=KEYWORDS CONTENTS=\"$$pairs{'keywords'}\">\n"
		if $$pairs{'keywords'};
	$ret .= "</HEAD>\n";

# mime 
	$ret .= "<!-- start Navbar -->

<style type=\"text/css\">

<!--
.TextTable {
border: black 1px solid;
font-family: arial, verdana; 
font-size: 15px;
}
.search[A {
font-size: 8px;
font-family: verdana, arial, sans-serif;
}
.button {
margin-top: 1px;
margin-bottom: 1px;
}
select.search {
text-align: center;
}
.navtext {
color: #000000;
font-family: verdana, arial, sans-serif;
font-size: 12px;
}
small.navtext {
color: #ffffff;
font-family: verdana, arial, sans-serif;
font-size: 8px;
}
.foottext {
color: #666666;
font-family: verdana, arial, sans-serif;
font-size: 12px;
text-align: center;
}

-->
</style>

<!-- mime -->
<BODY BGCOLOR=\"#dddddd\" TEXT=\"#000000\" 
      LINK=\"#0043ff\" VLINK=\"#0043ff\" ALINK=\"#6b7eb2\">

<table border=\"0\" cellspacing=\"2\" cellpadding=\"0\" WIDTH=\"100%\">
<tr valign=\"bottom\">
 <td valign=\"bottom\">
 <IMG SRC=\"images/logo.jpg\" NAME=\"logo1\" ALT=\"logo\" ALIGN=TOP VSPACE=3 WIDTH=400 HEIGHT=68 BORDER=0>
 <IMG SRC=\"images/earth2.gif\" NAME=\"earth\" ALT=\"earth\" ALIGN=TOP VSPACE=3 BORDER=0>
 </td>

 <td nowrap class=\"navtext\" height=\"24\">
 
 <small class=\"navtext\">&nbsp;&nbsp;

 <a href=\"index.html\" class=\"navtext\" 
 title=\"home\"><b>HOME</b></a> &nbsp;

 <a href=\"http://www.qsl.net\" TARGET=\"_blank\" class=\"navtext\" >QSL.net</a> -
 <a href=\"http://www.darc.de\" TARGET=\"_blank\" class=\"navtext\" > DARC</a> - 
 <a href=\"http://www.arrl.org\" TARGET=\"_blank\" class=\"navtext\" >ARRL</a> - 
 <!-- Counter -->
 <!-- &nbsp;&nbsp;<img src=\"http://asus-la.x.org/cgi-bin/wwwcount.cgi?df=sample.dat&ft=0
 class=\"navtext\" ALIGN=TOP VSPACE=3 HEIGHT=10 BORDER=0 \"> -->
 </tr>

<!-- Linie -->
 <tr bgcolor=\"#999999\">
 <td colspan=\"2\"><img src=\"blank.gif\" width=\"1\"
 height=\"2\" alt=\"\"></td>
</tr>
</table>\n";
#
# mime end
#
	$ret .= "
<!--
<BODY BGCOLOR=\"#6b7eb2\" TEXT=\"#000000\" BACKGROUND=\"images/back.jpg\"
      LINK=\"#0043ff\" VLINK=\"#0043ff\" ALINK=\"#6b7eb2\">
-->
<!-- blau
<BODY BGCOLOR=\"#5f1fae\" TEXT=\"#000000\" 
      LINK=\"#0043ff\" VLINK=\"#0043ff\" ALINK=\"#6b7eb2\">
-->

<!-- ist hier falsch <BODY BGCOLOR=\"#e7e7e7\" TEXT=\"#000000\" 
      LINK=\"#0043ff\" VLINK=\"#0043ff\" ALINK=\"#6b7eb2\">
-->

<!-- Table on left for menu table (decorate.pl) -->
<TABLE BORDER=$BORDER ALIGN=LEFT CELLPADDING=0 CELLSPACING=6>
<TR BGCOLOR=\"#ffffff\" ALIGN=CENTER><TD>
<!-- INSERT_MENU -->
<!-- End of table on left -->

</TD></TR>
</TABLE>

<!-- Table on left for document table (decorate.pl) -->
<TABLE BORDER=$BORDER ALIGN=LEFT CELLPADDING=0 CELLSPACING=6>
<TR BGCOLOR=\"#ffffff\" ALIGN=LEFT><TD>

<!-- Document table (decorate.pl)  -->
<TABLE class=TextTable BORDER=$BORDER CELLPADDING=1 CELLSPACING=4>\n";

	if ($$pairs{'title'}) {
		$ret .= "
<!-- Title row -->
<TR BGCOLOR=\"#ffffff\" VALIGN=TOP>
<TD>
<!-- mime: Überschrift -->
<FONT COLOR=\"#000000\" SIZE=\"+1\"><B>
$$pairs{'title'}
</B></FONT><HR NOSHADE SIZE=1>
</TD>";

		$ret .= "
<TD ROWSPAN=2 ALIGN=LEFT>
<IMG SRC=\"$$pairs{'logo'}\" ALT=\"Logo\">
</TD>" if $$pairs{'logo'};

		$ret .= "
</TR>

<!-- Empty row -->
<TR><TD><BR></TD></TR>\n";

	}

	$ret .= "\n<!-- Document row -->\n\n<TR><TD";
	$ret .= " COLSPAN=2" if $$pairs{'logo'};
	$ret .= ">\n";

	$ret;
}

##
#
# Configure: document trailer

sub stdclose {
	my $date = localtime;
	"
<!-- End of document table -->

<hr NOSHADE SIZE=1>
<P ALIGN=CENTER><FONT COLOR=\"#000000\" SIZE=\"-2\">&copy; Michael Meifert</font>
<P ALIGN=CENTER><FONT COLOR=\"#6b7eb2\" SIZE=\"-2\">
Generated on $date by <A HREF=\"tools/decorate.pl\">decorate.pl</A> / 
<A HREF=\"tools/menuize.pl\">menuize.pl</A></FONT>

</TD></TR></TABLE>

<!-- End of table on left -->

</TD></TR></TABLE>
</BODY>
</HTML>
";

}

##
#
# Main
#

# Check args
die("Not enough arguments!\n") unless $#ARGV >= 1;
my $outputdir = pop(@ARGV);

# Loop through files on command line
for my $file (@ARGV) {

	# Read file first time, to obtain attr/value pairs
	open(IN, $file) or die("Cannot open input file $file: $!\n");

	$/ = '-->';
	my %pairs = ();
	while(<IN>) {
		s/.*<!-- DECO//s or next;	# Delete before; skip if none
		s/-->.*//s;			# Delete content after close

		# We now have a series of attrs and values left
		while(s/\s*			# optional whitespace
			([A-Za-z0-9_]+)		# Attribute ($1)
			\s*=\s*			# optional ws., = sign, opt. ws.
			(
			  "(([^"]|\\")*[^\\])"| # quoted Value ($3), or
			  ([^\s]+)		# single word Value ($5)
			)
			(
			  \s+|			# ended by one or more spaces
			  -->|			# or end-of-tag
			  $			# or end-of-record
			)//x) {

			my $atr = lc $1;	# Attributes are lowercase
			my $val = $5 || $3;	# Take single word or quoted
			$pairs{$atr} = $val;	# Save into hash
		}
	}

	# Open output
	my $outf = $file; $outf =~ s/.*\///; $outf = "$outputdir/$outf";
	open(OUT, "> $outf") or die("Cannot open output file $outf: $!\n");

	# Output opening
	print OUT stdopen(\%pairs);

	# Now copy input unchanged, except for DECO pseudo-tags
	$/ = '-->';
	seek(IN, 0, 0);
	while(<IN>) {
		s/<!-- DECO.*-->//s;
		print OUT $_;
	}

	# Output closing
	print OUT stdclose(\%pairs);

	close(OUT) || die("Could not close output $outf: $!\n");
	close(IN) || die("Could not close input $file: $!\n");
}

