#! /bin/sh

usage () {
  echo commands: help enums  gperf
}

header () {
    echo '/* Automatically generated by process-types,' 
    echo ' * ' `date`
    echo ' * Do not edit! */'
    echo
}

atom_id=0

getatom () {
    atom=''
    while [ "x$atom" = x ]; do
	read atom ignored
	if [ $? != 0 ] ; then
	    return 1
	fi
	case "$atom" in
	    \#*)
		atom=''
		;;
	esac ;
    done
    atom_id=`expr $atom_id + 1`
}

atom2enum () {
    echo "SPKI_TYPE_$1" | tr '[a-z]-' '[A-Z]_' | sed -e 's/\*/STAR/'
}

length () {
    expr `echo "$1" | wc -c` - 1
}

if [ $# != 1 ]; then
    usage
    exit 1
fi

case $1 in
    header)
	header
	echo 'enum spki_type {'
	echo '  /* Used both for unknown types and for sexp syntax errors */'
	echo '  SPKI_TYPE_SYNTAX_ERROR = 0,'
	while getatom; do
	    echo "  `atom2enum $atom`,"
	done
	echo '  SPKI_TYPE_END_OF_EXPR'
	echo '};'
	;;
    gperf)
	echo '%{'
	header
	echo '%}'
	echo 'struct spki_assoc { const char *name; enum spki_type id; };'
	echo '%%'
	    
	while getatom; do
	    echo "$atom, `atom2enum $atom`"
	done
	;;
    names)
        header
	echo '{'
	echo '  { 0, NULL },'
	while getatom; do
	    echo "  { `length $atom`, "'"'$atom'" },'
	done
	echo '};'
	;;
    help)
	usage
	exit 0
	;;
    *)
	usage
	exit 1
	;;
esac
