#!/usr/local/bin/bash
#
# Copyright (C) 2004 SIPfoundry Inc.
# Licensed by SIPfoundry under the LGPL license.
#
# Copyright (c) 2002 Pingtel Corp.
# Licensed to SIPfoundry under a Contributor Agreement.
#
# This script generates an application configuration file from a variable 
# definitions file and a symbolic configuration file.  The variable
# definitions file should contain name-value pairs, one per
# line:
#   NAME="VALUE"
#
# The symbolic configuration file should contain ordinary text, with
# variables represented by the form ${NAME} wherever they should be
# filled in. The curly braces may be omitted if whitespace follows
# the name of the variable. The shell escape characters $, \, and `
# should be escaped (with a \) whenever they should appear literally
# in the text.

# Symbolic configuration file + variable definitions file
# = application configuration file

die() {
	echo "$0: $@"
	exit 1
}

# Remove any <CR>'s from the given file(s)
extract_cr() {
	TEMPFILE=/var/tmp/config-cr.$$
	while [ $# -ne 0 ]
	do
		if ! tr -d \\015 < "$1" > $TEMPFILE
		then
			rm -f $TEMPFILE
			die "$TEMPFILE: Unknown write error"
		fi
		if ! cmp -s "$1" $TEMPFILE
		then
			if [ ! -w "$1" ]
			then
				echo "$1 has <CR> characters in it, but cannot be"
				echo "overwritten. This is usually indicative of having used a DOS or Windows editor"
				echo "to edit the file. Please change the permissions on the file so that this script"
				echo "can remove the <CR> characters."
				rm -f $TEMPFILE
				die "$1: Permission denied"
			fi
			# (Try to) keep the original inode, in
			# case we can't write to the directory
			cp -dp "$1" "$1.cr"
			cat $TEMPFILE > "$1"
		fi
		shift
	done
	rm -f $TEMPFILE
}

CONFIG_PP="sipX Config Preprocessor 1.0.0"

if [ $# -eq 0 -o "$1" == "--help" ]
then
	echo "This is $CONFIG_PP"
	echo "Use --defs, --in, and --out to specify the"
	echo "desired config file preprocessing information."
	exit
fi

if [ "$1" == "--debug" ]
then
	shift
	PARM_DEBUG=yes
fi

while [ $# -gt 0 ]
do
	case $1 in
		--defs)
			PARM_DEFS="$2"
			shift
		;;
		--defs=*)
			PARM_DEFS=`echo "$1" | cut -c 8-`
		;;
		--in)
			PARM_IN="$2"
			shift
		;;
		--in=*)
			PARM_IN=`echo "$1" | cut -c 6-`
		;;
		--out)
			PARM_OUT="$2"
			shift
		;;
		--out=*)
			PARM_OUT=`echo "$1" | cut -c 7-`
		;;
	esac
	shift
done

if [ "${PARM_DEBUG-}" == "yes" ]
then
	echo "PARM_DEFS: $PARM_DEFS"
	echo "PARM_IN:   $PARM_IN"
	echo "PARM_OUT:  $PARM_OUT"
fi

[ -z "$PARM_DEFS" -o -z "$PARM_IN" -o -z "$PARM_OUT" ] && die "Must provide --defs, --in, and --out."

[ -f "$PARM_DEFS" ] || die "$PARM_DEFS: No such file or directory"
[ -f "$PARM_IN" ] || die "$PARM_IN: No such file or directory"

extract_cr "$PARM_DEFS" "$PARM_IN"

SCRIPT=/var/tmp/config-script.$$

cat > $SCRIPT << EOF
#!/usr/local/bin/bash

if [ -f "$PARM_OUT" ]
then
	rm -f "$PARM_OUT"
fi

. "$PARM_DEFS"
cat > "$PARM_OUT" << EOF
EOF

cat "$PARM_IN" >> $SCRIPT

echo "EOF" >> $SCRIPT

chmod 755 $SCRIPT

$SCRIPT

rm $SCRIPT

chmod 644 "$PARM_OUT"


syntax highlighted by Code2HTML, v. 0.9.1