#!/bin/sh
# Charles Clément <caratorn@gmail.com>

KEYSYMDEF="/usr/include/X11/keysymdef.h"
KEYBOARD_PREFIX="XK_"
KEYPAD_PREFIX="XK_KP_"

CONFIGURATION_FILE="${HOME}/.qworc"

START_INDEX=129

index=${START_INDEX}

if [ ! "$#" -eq 1 ]
then
    echo "Usage : $0 input_file"
	exit 1
fi

if [ ! -f $1 ]
then
	echo "No file $1 found."
	exit 2
fi

keysym_names=`grep -o -E "\"[[:alnum:]_]*\"" $1 | tr -d "\""`

if [ ! -f ${KEYSYMDEF} ]
then
	echo "No file ${KEYSYMDEF} found."
	exit 3
fi

for i in ${keysym_names}
do
	code=`grep -e "${KEYBOARD_PREFIX}${i}\>" ${KEYSYMDEF} | tr -s ' ' | cut -f 3 -d ' '`
	if [ -z "${code}" ]
	then
		code=`grep -e "${KEYPAD_PREFIX}${i}\>" ${KEYSYMDEF} | tr -s ' ' | cut -f 3 -d ' '`
		if [ -z "${code}" ]
		then
			echo "!${i} is not defined"
		else
			echo "keycode $index = KP_${i}"
			index=$(($index + 1))
		fi
	else
		echo "keycode $index = ${i}"
		index=$(($index + 1))
	fi
done
