#!/bin/sh
#set -vx

PROG=`basename $0`

if [ "$#" -lt "1" ]; then
	printf "Usage: $PROG <config-file> ...\n"
	printf "Remove comments from specified SellaNMS config file(s).\n\n"
	printf "Example: $PROG /etc/sella_nms.conf etc/policy-options.conf\n\n"
	printf "Report bugs to <sella@digital-genesis.com>.\n"
	exit 1
fi

TMPFILE="/tmp/${PROG}.$$"

while [ "$#" -ge "1" ]; do
	if [ ! -r "$1" ]; then
		printf "Unable to read input file '$1'. [Skipped]\n\n"
		continue
	fi

	touch $TMPFILE > /dev/null 2>&1 
	if [ $? != 0 ]; then
		printf "Unable to write to temporary file '%s'.\n" $TMPFILE
		exit 1;
	fi

	printf "$1..."

	sed 's/;.*$/;/g' $1 | sed 's/--.*$//' | awk 'BEGIN { go=1; } { if (/\/\*/) go=0; if (go) { print; } if (/\*\/[ 	]*$/) go=1; }' | grep -v '^[:space:]*$' > $TMPFILE
	cat >> $TMPFILE << EOF

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
** vim: syntax=sella_nms
*/
EOF
	mv -f $TMPFILE $1
	if [ $? != 0 ]; then
		printf "Unable to overwrite original file '%s'.\n" $3
		exit 1;
	fi

	printf "done\n"

	shift
done
