#!/usr/bin/perl
#   This file is part of the KDE libraries
#
#   Copyright (C) 1998 Waldo Bastian (bastian@kde.org)
#                 1999 Lars Knoll (knoll@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.
#
#----------------------------------------------------------------------------
#
#  KDE HTML Widget -- Script to generate htmltags.c and htmltags.h
#
open IN, "htmltags.in"
  or die "Can't open in\n";
open header, ">htmltags.h"
  or die "Can't open header\n";
open out, ">htmltags.gperf"
  or die "Can't open out\n";

print out "%{\n/* This file is automatically generated from htmltags.in by maketags, do not edit */\n/* Copyright 1999 Lars Knoll */\n#include \"htmltags.h\"\n%}\n";
print out "struct tags {\n    const char *name;\n    int id;\n};\n%%\n";

print header "/* This file is automatically generated from
htmltags.in by maketags, do not edit */\n/* Copyright 1999 Lars Knoll */\n\n#ifndef KHTML_TAGS_H\n#define KHTML_TAGS_H\n\n#include \"dom/dom_string.h\"\n\n";

print header "DOM::DOMString getTagName(unsigned short id);\n";

$num = 0;
while (<IN>) {
    chomp;
    $attr = $_;
    $num = $num + 1;
    $up = uc($attr);
    push(@a, "    \"$up\",");
    push(@b, "    \"/$up\",");
    $up =~ s/-/_/;
    print out $attr . ", ID_" . $up . "\n";
    print header "#define ID_" . $up . " " . $num . "\n";
}
print out "anchor, ID_A\n";
print out "image, ID_IMG\n";
print out "listing, ID_PRE\n";
$num = $num+1;
print header "#define ID_TEXT $num\n";
$num = $num+1;
print header "#define ID_COMMENT $num\n";
print header "#define ID_CLOSE_TAG $num\n";
print header "#define ID_LAST_TAG $num\n";

print out "%%\n";
close out;
print header "\n#endif\n";
close header;

my $result = system("/bin/sh", "-c", "gperf -a -L 'ANSI-C' -D -E -C -l -o -t -k '*' -NfindTag -Hhash_tag -Wwordlist_tag htmltags.gperf > htmltags.c");
if ($result) {
  unlink "htmltags.c";
  exit $result;
}

open(OUT, ">>htmltags.c");
print OUT "\n\nstatic const char * const tagList[] = {\n";
print OUT "\"\",\n";
while(defined ($line = shift @a))
{
    print OUT $line."\n";
}
print OUT "\"TEXT\",\n";
print OUT "\"COMMENT\",\n";
while(defined ($line = shift @b))
{
    print OUT $line."\n";
}
print OUT "    0\n};\n";
print OUT "DOMString getTagName(unsigned short id)\n{\n";
print OUT "    if(id > ID_CLOSE_TAG*2) id = ID_CLOSE_TAG+1;\n";
print OUT "    return DOMString(tagList[id]);\n";
print OUT "};\n";
