#!/bin/bash
#
#   This file is part of the KDE libraries
#
#   Copyright (C) 1999 Waldo Bastian (bastian@kde.org)
#             (c) 2006 Nikolas Zimmermann <zimmermann@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., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.
#
#----------------------------------------------------------------------------
#
#  KDE HTML Widget -- Script to generate cssproperties.c and cssproperties.h
#
num=1
NS=""

if [ -n "$1" ] # Eventually specified namespace prefix (ie. SVG)
then
	num=10001 # Bigger than all properties from HTML CSS
	NS=$1
fi

prefix=`echo $NS | awk '{ do { print tolower($0) } while (getline) }'`

grep "^[^\#]" "$prefix"cssproperties.in > "$prefix"cssproperties.strip

echo -e '%{\n/* This file is automatically generated from '$prefix'cssproperties.in by makeprop, do not edit */\n/* Copyright 1999 W. Bastian */\n#include "'$prefix'cssproperties.h"\n%}\nstruct css_prop'$prefix' {\n    const char *name;\n    int id;\n};\n\nstatic const struct css_prop'$prefix' * findProp'$NS' (register const char *str, register unsigned int len);\n\n%%' > "$prefix"cssproperties.gperf
cat "$prefix"cssproperties.strip | awk '{ do { prop = $0; gsub("-", "_"); print prop ", '$NS'CSS_PROP_" toupper($0) } while (getline) }' >> "$prefix"cssproperties.gperf
echo '%%' >> "$prefix"cssproperties.gperf
echo -e '/* This file is automatically generated from '$prefix'cssproperties.in by makeprop, do not edit */\n/* Copyright 1998 W. Bastian */\n\n#ifndef '$NS'CSSPROPERTIES_H\n#define '$NS'CSSPROPERTIES_H\n\nDOM::DOMString get'$NS'PropertyName(unsigned short id) KHTML_NO_EXPORT;\n' > "$prefix"cssproperties.h
cat "$prefix"cssproperties.strip | awk '{ \
i='$num'; \
print "#define '$NS'CSS_PROP_INVALID 0"; \
print "#define '$NS'CSS_PROP_MIN '$num'"; \
do { gsub("-", "_"); print "#define '$NS'CSS_PROP_" toupper($0) " " i; i = i + 1 } while (getline); \
print ""; \
print "#define '$NS'CSS_PROP_TOTAL " (i + 1 - '$num') \
}' >> "$prefix"cssproperties.h
perl -e 'my $max = 0; while (<>) { chomp; $max = length if $max < length; } print "const size_t max'$NS'CSSPropertyNameLength = $max;\n"' < "$prefix"cssproperties.strip >> "$prefix"cssproperties.h

gperf -a -L 'ANSI-C' -E -C -c -o -t -k '*' -NfindProp"$NS" -Hhash_prop"$prefix" -Wwordlist_prop"$prefix" -D -s 2 "$prefix"cssproperties.gperf > "$prefix"cssproperties.c || exit 1
echo -e '#endif\n' >> "$prefix"cssproperties.h

cat "$prefix"cssproperties.strip | awk '{ \
i=1; \
print "static const char * const propertyList'$NS'[] = {"; \
print "\"\","; \
do { print "\"" $0 "\", "; i = i + 1 } while (getline); \
print "    0"; \
print "};"; \
print "DOMString get'$NS'PropertyName(unsigned short id)"; \
print "{"; \
print "    if(id >= '$NS'CSS_PROP_TOTAL || id == 0)"; \
print "      return DOMString();";\
print "    else";\
print "      return DOMString(propertyList'$NS'[id - "('$num' - 1)"]);"; \
print "}"; \
print ""; \
}' >> "$prefix"cssproperties.c

perl -pi -e "s/id\ -\ 0/id/" "$prefix"cssproperties.c
perl -pi -e "s/TOTAL_KEYWORDS/${NS}TOTAL_KEYWORDS/g" "$prefix"cssproperties.c
perl -pi -e "s/MIN_WORD_LENGTH/${NS}MIN_WORD_LENGTH/g" "$prefix"cssproperties.c
perl -pi -e "s/MAX_WORD_LENGTH/${NS}MAX_WORD_LENGTH/g" "$prefix"cssproperties.c
perl -pi -e "s/MIN_HASH_VALUE/${NS}MIN_HASH_VALUE/g" "$prefix"cssproperties.c
perl -pi -e "s/MAX_HASH_VALUE/${NS}MAX_HASH_VALUE/g" "$prefix"cssproperties.c
