#!/usr/bin/perl -w
#
#   This file is part of the KDE libraries
#
#   Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de)
#                 2004-2005 Nikolas Zimmermann (wildfox@kde.org)
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Library General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This library 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
#   Library General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public License
#   along with this library; see the file COPYING.LIB.  If not, write to
#   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.
#

use strict;

my $nsFull = $ARGV[1];
my $ns = $nsFull; $ns =~ s/K//;
my $basename = "cssproperties";
if (defined($ARGV[2]) && $ARGV[2] eq "-f") {
    my $inputfile = $ARGV[3];
    $inputfile =~ m/(.*)\.in/;
    $basename = $1;
}

my $filec = "$basename.c";
my $fileh = "$basename.h";
my $filein = "$basename.in";
my $filegperf = "$basename.gperf";

open IN, $filein or die "Can't open $filein\n";
open HEADER, ">$fileh" or die "Can't open $fileh\n";
open OUT, ">$filegperf" or die "Can't open $filegperf\n";

print OUT "%{\n/* This file is automatically generated from $filein by cssmakeprops, do not edit */\n#include \"$fileh\"\n%}\n";
print OUT "struct css_prop {\n    int name;\n    int id;\n};\n\n";
print OUT "static const struct css_prop *find${ns}Prop (register const char *str, register unsigned int len);\n\n%%\n";

print HEADER "/* This file is automatically generated from $filein by cssmakeprops, do not edit */\n/* Copyright 1999 Lars Knoll */\n\n#ifndef ${nsFull}_CSSPROPERTIES_H\n#define ${nsFull}_CSSPROPERTIES_H\n\n";

my %amap = ();
my @a = ();

my $num = 0;
if ($nsFull ne "KDOM") {
	$num = 10001; # Bigger than all properties from HTML CSS

	print HEADER "#define ${ns}CSS_PROP_INVALID 0\n";
	print HEADER "#define ${ns}CSS_PROP_MIN $num\n";

	$num = $num - 1;
} else {
	print HEADER "#define CSS_PROP_INVALID 0\n";
	print HEADER "#define CSS_PROP_MIN 1\n";
}

while (<IN>) {
    chomp;
    my $prop = $_;

	if(not($prop =~ /#/) and (length($prop) > 0)) {
	    $num = $num + 1;

	    my $up = uc($prop);
	    push(@a, $up);

		my $pup = $up;
		$pup =~ s/-/_/g;

	    $amap{$pup} = $num;
		
		if ($nsFull eq "KDOM") {
	    	print OUT $prop . ", CSS_PROP_" . $pup . "\n";
		    print HEADER "#define CSS_PROP_" . $pup . " " . $num . "\n";
		} else {
	    	print OUT $prop . ", ${ns}CSS_PROP_" . $pup . "\n";
		    print HEADER "#define ${ns}CSS_PROP_" . $pup . " " . $num . "\n";
		}
    }
}

close(IN);

if ($nsFull eq "KDOM") {
	print HEADER "#define CSS_PROP_MAX $num\n";
} else {
	print HEADER "#define ${ns}CSS_PROP_MAX $num\n";
}

print OUT "%%\n";
close OUT;

print HEADER "\nnamespace ${nsFull}\n{\n";
print HEADER "    const char *getSVGCSSPropertyName(unsigned short id);\n";
print HEADER "    int getSVGCSSPropertyID(const char *tagStr, int len);\n";
print HEADER "}\n";

print HEADER "\n#endif\n";
close HEADER;

my $result = system("/bin/sh", "-c", "gperf -c -a -L 'ANSI-C' -P -G -D -E -C -o -t -k '*' -Nfind${ns}Prop -Hhash_prop -Wwordlist_prop -Qspool_prop -s 3 $filegperf > $filec");
if ($result) {
  unlink "$filec";
  exit $result;
}

# Avoid clashes in parser.cpp, which includes both CSSValueKeywords.c & CSSPropertyNames.c
system("/bin/sh", "-c", "perl -pi -e \"s/TOTAL_KEYWORDS/PROP_TOTAL_KEYWORDS/g\" $filec");
system("/bin/sh", "-c", "perl -pi -e \"s/MIN_WORD_LENGTH/PROP_MIN_WORD_LENGTH/g\" $filec");
system("/bin/sh", "-c", "perl -pi -e \"s/MAX_WORD_LENGTH/PROP_MAX_WORD_LENGTH/g\" $filec");
system("/bin/sh", "-c", "perl -pi -e \"s/MIN_HASH_VALUE/PROP_MIN_HASH_VALUE/g\" $filec");
system("/bin/sh", "-c", "perl -pi -e \"s/MAX_HASH_VALUE/PROP_MAX_HASH_VALUE/g\" $filec");
system("/bin/sh", "-c", "perl -pi -e \"s/lookup/lookupProp/g\" $filec");

# read the hash mappings (is there a better way?)
my %hmap = ();
open(IN, "< $filec");
while(<IN>) {
	my $lookFor = "CSS_PROP_";
	if ($nsFull ne "KDOM") {
		$lookFor = $ns . $lookFor;
	}

	if (/spool_prop_str(\d+), ${lookFor}([\w_]+)/) {
		$hmap{$amap{$2}} = $1;
	}
}
close(IN);  

open(OUT, ">> $filec");
print OUT "\nnamespace ${nsFull}\n{\n";
print OUT "\n\nstatic const unsigned short propList[] = {\n";
print OUT "    65535,\n";

while(defined (my $line = shift @a)) {
    my $l = $line;

	if(not($l =~ /#/) and (length($l) > 0)) {
		$l =~ y/-/_/;
    	
		die if !length($hmap{$amap{$l}});

	    print OUT "    " .$hmap{$amap{$l}}.",\n";
	}
}

my $lookFor = "CSS_PROP_MAX";
if ($nsFull ne "KDOM") {
	$lookFor = $ns . $lookFor;
}

print OUT "    65535\n};\n\n";
print OUT "const char *getSVGCSSPropertyName(unsigned short id)\n{\n";
print OUT "    if (!id || id > $lookFor) return \"\";\n";
print OUT "    return spool_prop + wordlist_prop[propList[id]].name;\n";
print OUT "}\n";
print OUT "\nint getSVGCSSPropertyID(const char *tagStr, int len)\n";
print OUT "{\n";
print OUT "    const struct css_prop *propPtr = find${ns}Prop(tagStr, len);\n";
print OUT "    if(!propPtr)\n";
print OUT "      return 0;\n";
print OUT "    return propPtr->id;\n";
print OUT "}\n\n";
print OUT "}\n";
