-if [ -f "$OUTFILE" ]; then
- echo "Error: output file $OUTFILE already exists."
- exit $EXIT_OUTPUT_FILE_EXISTS
-fi
-
-if [ -f "$TEMPFILE" ]; then
- echo "Error: temporary file $TEMPFILE already exists."
- exit $EXIT_TEMP_FILE_EXISTS
-fi
-
-# Common options. Color screws up the output.
-GREP="grep --extended-regexp --color=never"
-
-# Grep grabs all of the "#define MACRO..." lines, except for
-# MACRO_X_COUNT and MACRO_ENV_VAR_PREFIX which aren't real macros.
-# Sed just removes the "MACRO_" prefixes.
-$GREP --only-matching '[[:space:]]MACRO_[[:alnum:]_]+' "$INFILE" \
- | $GREP -v 'MACRO_X_COUNT' \
- | $GREP -v 'MACRO_ENV_VAR_PREFIX' \
- | sed 's/ MACRO_//g' >> "$OUTFILE"
+# Grabs all of the "#define MACRO..." lines, except for MACRO_X_COUNT
+# and MACRO_ENV_VAR_PREFIX which aren't real macros, and outputs the
+# macro name (everything after "MACRO_") enclosed in quotes and dollar
+# signs, appropriate to be copy/pasted directly into nagios-mode.el.
+MACRO_REGEX='#define[[:space:]]\{1,\}MACRO_\([^[:space:]]\{1,\}\)[[:space:]]\{1,\}.*'
+sed -n "s/${MACRO_REGEX}/\"\$\1\$\"/p" "${INFILE}" \
+ | sed '/X_COUNT\|ENV_VAR_PREFIX/d' \
+ >> "${TEMPFILE}"