#!/usr/bin/env perl

# Updates http://git.gnome.org/browse/gtkmm/tree/gdk/gdkmm/keysyms.h from gtk+ 
# keysymdefs file from http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h
# 
# Author  : Simos Xenitellis <simos at gnome dot org>.
# Authos  : Bastien Nocera <hadess@hadess.net>
# Authos  : Mark Vender <markv743@yahoo.co.uk>
# Version : 1.0
#
# Input   : http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h
# Output  : http://git.gnome.org/browse/gtkmm/tree/gdk/gdkmm/keysyms.h
# 
# Notes   : It downloads gdkkeysymdef.h from the Internet, if not found locally,
# Notes   : and creates an updated keysyms.h

use strict;

# Used for reading the keysymdef symbols.
my @keysymelements;

if ( ! -f "gdkkeysyms.h" )
{
    print "Trying to download gdkkeysyms.h from\n";
    print "http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h";
    die "Unable to download keysymdef.h from http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h\n" 
        unless system("wget -c -O gdkkeysyms.h \"http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h\"") == 0;
    print " done.\n\n";
}
else
{
    print "We are using existing gdkkeysyms.h found in this directory.\n";
    print "It is assumed that you took care and it is a recent version\n";
    print "as found at http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h\n\n";
}

if ( -f "keysyms.h" )
{
    print "There is already a keysyms.h file in this directory. We are not overwriting it.\n";
    print "Please move it somewhere else in order to run this script.\n";
    die "Exiting...\n\n";
}

# Source: http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h
die "Could not open file keysymdef.h: $!\n" unless open(IN_KEYSYMS, "<:utf8", "gdkkeysyms.h");

# Output: gtkmm/gdk/gdkmm/keysyms.h
die "Could not open file eysyms.h: $!\n" unless open(OUT_KEYSYMS, ">:utf8", "keysyms.h");

my $LICENSE_HEADER= <<EOF;
/* Copyright (C) 1998-2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
EOF

print OUT_KEYSYMS $LICENSE_HEADER;

print OUT_KEYSYMS<<EOF;

/*
 * File auto-generated by script 
 * http://git.gnome.org/cgit/gtkmm/tree/gdk/gdkmm/keysyms-update.pl
 * using the input file
 * from http://git.gnome.org/browse/gtk+/plain/gdk/gdkkeysyms.h
 */

#ifndef _GDKMM_KEYSYMS_H
#define _GDKMM_KEYSYMS_H

namespace Gdk
{
    
enum KeyVal
{
EOF

while (<IN_KEYSYMS>)
{
    next if ( ! /^#define / );
    next if ( /^#define\s+__/ );

    @keysymelements = split(/\s+/);
    die "Internal error, no \@keysymelements: $_\n" unless @keysymelements;

    $_ = $keysymelements[1];
    die "Internal error, was expecting \"GDK_KEY_*\", found: $_\n" if ( ! /^GDK_KEY_/ );

    $_ = $keysymelements[2];
    die "Internal error, was expecting \"0x*\", found: $_\n" if ( ! /^0x/ );

    my $element = $keysymelements[1];
    my $binding = $element;
    $binding =~ s/^GDK_KEY_/KEY_/g;

    printf OUT_KEYSYMS "  %s = 0x%03x,\n", $binding, hex($keysymelements[2]);
}

close IN_KEYSYMS;

print OUT_KEYSYMS<<EOF;
};

} // namespace Gdk

#endif /* _GDKMM_KEYSYMS_H */
EOF


printf "We just finished converting gdkkeysyms.h to keysyms.h\nThank you\n";
