#!/bin/sh
#
# Generate enum.c from GL/gl.h
#

IFILE="gl.h"

for i in $*; do
    TMP=${i#-I}/GL/gl.h
    if [ -f $TMP ]; then
	IFILE=$TMP
	break
    fi
done

if [ ! -f $IFILE ]; then
    echo "Cannot find $IFILE"
    exit -1
fi

cat <<EOF
#include <stdio.h>

#include "GL/gl.h"


static const struct {
    GLenum e;
    const char *n;
} enum_tab[] = {
EOF

awk '/define GL_/ {print "    { " $2 ", \"" $2 "\" },"}' $IFILE | grep -v included | sed '$ s/},/}/'

cat <<EOF
};


const char *
gl_enum_by_nr (GLenum e)
{
    static char tmp[16];
    int i, n = sizeof(enum_tab) / sizeof(enum_tab[0]);
    for (i = 0; i < n; i++) {
	if (e == enum_tab[i].e) {
	    return enum_tab[i].n;
	}
    }
    sprintf(tmp, "0x%04x", e);
    return tmp;
}
EOF
