#!/usr/bin/env python

# Based on libxkbcommon script makeheader and GDK script update-keysyms-header.

import os
import re


output_file = os.path.join(os.path.dirname(__file__), '..', 'include', 'wpe', 'keysyms.h')
output = open(output_file, 'wb')

output.write('''#if !defined(__WPE_H_INSIDE__) && !defined(WPE_COMPILATION)
#error "Only <wpe/wpe.h> can be included directly."
#endif

#ifndef wpe_keysyms_h
#define wpe_keysyms_h

/* This file is autogenerated; use https://github.com/WebPlatformForEmbedded/libwpe/blob/master/scripts/update-keysyms-header to update it. */
''')

with open('/usr/include/X11/keysymdef.h') as header:
    for line in header:
        if '#ifdef' in line or '#ifndef' in line or '#endif' in line:
            continue

        output.write(re.sub(r'#define\s*XK_', '#define WPE_KEY_', line))

with open('/usr/include/X11/XF86keysym.h') as header:
    for line in header:
        if '#ifdef' in line or '#ifndef' in line or '#endif' in line:
            continue

        # Ignore XF86XK_Q
        if 'XF86XK_Q' in line:
            continue

        # XF86XK_Calculater is misspelled, and a dupe
        if 'XF86XK_Calculater' in line:
            continue

        # XF86XK_Clear and XF86XK_Select could end up a dupe of XK_Clear and XK_Select.
        if 'XF86XK_Clear' in line:
            line = re.sub(r'#define\s*XF86XK_', '#define WPE_KEY_Window', line)
        elif 'XF86XK_Select' in line:
            line = re.sub(r'#define\s*XF86XK_Select', '#define WPE_KEY_SelectButton', line)
        else:
            line = re.sub(r'#define\s*XF86XK_', '#define WPE_KEY_', line)

        output.write(line)

output.write('''#endif /* wpe_keysyms_h */
''')

output.close()
