#! /usr/bin/env bash
#
# Converts -Dmacro to #define macro
#      and -Umacro to #undef macro
# on standard output
#
# Any options that do not take the form -D* or -U* are placed as comments.
#
# usage: convert_cpp_cmd2defines [-Dmacro1|-Umacro1] [...]
#
cat << EOF
/*  Warning - this file is automatically generated - do NOT edit  */
/*
 Created by convert_cpp_cmd2defines with the following command line arguments:
 $@
*/

EOF

BARRIER=

# Process arguments
for arg in "$@"
do
 case $arg in
  -b*)
        BARRIER=`echo $arg | sed 's/-b//'`
        echo "#ifndef ${BARRIER}"
        echo "#define ${BARRIER}"
        ;;
  -D*)
        echo $arg | sed 's/-D/#define /' | sed 's/=/ /'
        ;;
  -U*)
        echo $arg | sed 's/-U/#undef  /' | sed 's/=/ /'
        ;;
  *)
        echo "/* " $arg " */"
        ;;
 esac
done

if test ! "x${BARRIER}" = x ; then
 echo "#endif /* ${BARRIER} */"
fi
