#!/bin/sh
#
# Parse the object names out of Nagios' xdata/xodtemplate.h, and
# convert them to their definition format.
#
# Operates on whatever file you give it, like the other utilities.
#

EXIT_USAGE=1
EXIT_INPUT_FILE_DOESNT_EXIST=2

if [ $# -lt 1 ]; then
    echo "Usage: $0 <input_file>"
    exit $EXIT_USAGE
fi


INFILE="$1"

if [ ! -f "$INFILE" ]; then
    echo "Error: input file $INFILE doesn't exist."
    exit $EXIT_INPUT_FILE_DOESNT_EXIST
fi

# Sed grabs all of the "#define XODTEMPLATE_FOO" lines, pulls out the
# FOO part, and then prints "define FOO", surrounded in
# quotes. Afterwards we prune the two lines corresponding to
# XODTEMPLATE_NULL and XODTEMPLATE_NONE which do not correspond to
# real object types. Finally we lowercase everything with tr, and sort
# the result.
XOD_REGEX='#define[[:space:]]\{1,\}XODTEMPLATE_\([^[:space:]]\{1,\}\)[[:space:]]\{1,\}.*'

sed -n "s/${XOD_REGEX}/\"define \1\"/p" "${INFILE}" \
  | sed '/define NULL\|NONE/d'                      \
  | tr '[A-Z]' '[a-z]'                              \
  | sort                                            \
