#!/bin/sh
## $Id: genconf,v 1.6 1997/05/19 22:58:13 luethje Exp $
##
## installation script for the config file of isdnlog
##
## Copyright 1997 by Stefan Luethje (luethje@sl-gw.lake.de)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
## $Log: genconf,v $
## Revision 1.6  1997/05/19 22:58:13  luethje
## - bugfix: it is possible to install isdnlog now
## - improved performance for read files for vbox files and mgetty files.
## - it is possible to decide via config if you want to use avon or
##   areacode.
##
## Revision 1.5  1997/05/10 01:21:00  luethje
## some primitive changes
##
## Revision 1.4  1997/05/04 20:19:41  luethje
## README completed
## isdnrep finished
## interval-bug fixed
##
## Revision 1.3  1997/04/15 22:37:05  luethje
## allows the character `"' in the program argument like the shell.
## some bugfixes.
##
## Revision 1.2  1997/04/08 21:20:02  luethje
## primitive changes
##
## Revision 1.1  1997/04/08 21:18:50  luethje
## File to generate the config file `isdn.conf'
##

CONFFILE=$1
CALLERIDFILE=$2

RESFILE=/tmp/`basename $0`.res.$$

setentry() {
	SECTION=$1
	ENTRY=$2
	VALUE=$3
	AWKFILE=/tmp/`basename $0`.$$

	grep $ENTRY $CONFFILE >/dev/null 2>&1
	if [ $? -eq 1 ]
	then
		cat >$AWKFILE << EOF
		{
			if (\$0 == "$SECTION")
				printf ( "%s\n%s\t= %s\n", \$0, "$ENTRY", "$VALUE" )
			else
				print
		}
EOF
		awk -f $AWKFILE $CONFFILE >$RESFILE
		cp $RESFILE $CONFFILE
	fi

	rm -f $AWKFILE $RESFILE
}


if [ ! -f $CONFFILE ]
then
	echo -n "Creating config file \"$CONFFILE\"..."
	cat >$CONFFILE << EOF
############################################################################
#
# File $CONFFILE generated by `basename $0` on `date`
#
############################################################################

[GLOBAL]

[VARIABLES]

[ISDNLOG]

EOF
else
	echo -n "Adding some entries to the config file \"$CONFFILE\"..."
fi

setentry "[GLOBAL]"  "AREACODE"      "$AREACODE"
setentry "[GLOBAL]"  "AREAPREFIX"    "$AREAPREFIX"
setentry "[GLOBAL]"  "COUNTRYCODE"   "$COUNTRYCODE"
setentry "[GLOBAL]"  "COUNTRYPREFIX" "$COUNTRYPREFIX"
setentry "[ISDNLOG]" "CHARGEMAX"     "$CHARGEMAX"
setentry "[ISDNLOG]" "REPFMT"        '\"  %X %D %15.15H %T %-15.15F %7u %U %I %O\"'
setentry "[ISDNLOG]" "REPFMTSHORT"   '\"%X%D %8.8H %T %-14.14F%U%I %O\"'
setentry "[ISDNLOG]" "REPFMTWWW"     '\"%X %D %17.17H %T %-17.17F %-20.20l SI: %S %9u %U %I %O\"'
setentry "[ISDNLOG]" "OLABEL"        "$OLABEL"
setentry "[ISDNLOG]" "ILABEL"        "$ILABEL"
setentry "[ISDNLOG]" "LOGFILE"       "$LOGFILE"


# Change the variables $1 to \$1 and so one.
sed -e "s/\([^\]\)\$\([0-9][ 	]\)/\1\\\\$\2/g" $CONFFILE   |\
sed -e "s/\([^\]\)\$\([0-9][  ]\)/\1\\\\$\2/g" >$RESFILE
cp $RESFILE $CONFFILE

echo "  Ready."

if [ -f $USERFILE ]
then
	# Change the variables $1 to \$1 and so one.
	echo -n "Adding some entries to the config file \"$CALLERIDFILE\"..."
	sed -e "s/\([^\]\)\$\([0-9][ 	]\)/\1\\\\$\2/g" $CALLERIDFILE |\
	sed -e "s/\(NUMBER[   ]*=[    ]*\)\*/\1/"                    |\
	sed -e "s/\([^\]\)\$\([0-9][  ]\)/\1\\\\$\2/g" >$RESFILE
	cp $RESFILE $CALLERIDFILE
	echo "  Ready."
fi

rm -f $RESFILE
