#!/usr/bin/perl
# Filename:	?
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License/
# Description:	?
use strict;

##################################################
# Setup the variables
##################################################
my $PROGNAME = $0; $PROGNAME =~ s|.*/||;
my ($BASENAME,$PROGNAME) = ($0 =~ m|(.*)/(.+)|) ? ($1?$1:'/',$2) : ('.',$0);

chdir($ARGV[0]) if @ARGV;

open(FONT,"Font") || die("No Font");
open(CSS,">Style.css") || die("No write css");
my %repl;
sub repl {
	my ($str) = @_;
	foreach my $k ( keys %repl ) {
		$str =~ s/\$$k/$repl{$k}/g;
	}
	$str;
}


while (<FONT>) {
	chomp;
	s/^\s*#.*//;
	next unless /\S/;
	if (/\$(.+)_FONT = "(.+)";/) {
		my ($name, $v) = (lc($1),$2);
		print CSS ".$name {\n";
		while ($v =~ s/^([^=]+)='([^']+)'\s*//) {
			my ($key,$val) = ($1,$2);
			$val = repl($val);
			$key = "font-family" if $key eq "face";
			#$key = "text-color" if $key eq "color";
			if ($key eq "size") {
				$key = "font-size";
				if ($val =~ /^[+-]\d+/) {
					$val =
						($val==-1 ? 'smaller' :
						($val==+1 ? 'larger' :
						($val==-2 ? '60%' :
						($val==+2 ? '150%' :
						($val==+4 ? '300%' :
							((100 + ($val*10)). '%'))))));
				} else {
					my @sizes=qw(xx-small xx-small	x-small	small	medium	large	x-large	xx-large);
					$val = $sizes[$val] || "xx-large";
				}
			}
			print CSS "\t$key: $val;\n";
		}
		print CSS "}\n";
		print STDERR "UNKNOWN VAL: $v\n" if $v;
		next;
	}
	if (/\$BODY = "(.+)";/) {
		my $v = $1;
		my %links;
		print CSS "BODY {\n";
		while ($v =~ s/^([^=]+)='([^']+)'\s*//) {
			my ($key,$val) = ($1,$2);
			$val = repl($val);
			if ($key =~ /link$/) {
				$links{$key}=$val;
				next;
			}
			if ($key eq "background") { # && $val =~ /\.(gif|jpg)/) { }
				$key = "background-image";
				$val =~ s|^\$PATH/||;
				$val = "url($val)";
			}
			$key = "color" if $key eq 'text';
			$key = "background-color" if $key eq 'bgcolor';
			print CSS "\t$key: $val;\n";
		}
		print CSS "}\n";
		foreach my $l ( keys %links ) {
			my $css;
			$css = 'a:link' if $l eq 'link';
			$css = 'a:visited' if $l eq 'vlink';
			$css = 'a:active' if $l eq 'alink';
			print CSS "$css { color: $links{$l}; }\n";
		}
		print STDERR "UNKNOWN VAL: $v\n" if $v;
		next;
	}
	if (/^\$(\S+)\s*=\s*"(\S+)";\s*$/) {
		$repl{$1} = $2;
		next;
	}
	print STDERR "UNKNOWN $_\n";
}
close FONT;
close CSS;

system("mv Font Font.old");

