#!/bin/sh
## $Id: genconf,v 1.9 1999/08/20 19:27:54 akool 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.9  1999/08/20 19:27:54  akool
## isdnlog-3.45
##  - removed about 1 Mb of (now unused) data files
##  - replaced areacodes and "vorwahl.dat" support by zone databases
##  - fixed "Sonderrufnummern"
##  - rate-de.dat :: V:1.10-Germany [20-Aug-1999 21:23:27]
##
## Revision 1.8  1999/03/20 14:32:49  akool
## - isdnlog Version 3.08
## - more tesion)) Tarife from Michael Graw <Michael.Graw@bartlmae.de>
## - use "bunzip -f" from Franz Elsner <Elsner@zrz.TU-Berlin.DE>
## - show another "cheapest" hint if provider is overloaded ("OVERLOAD")
## - "make install" now makes the required entry
##     [GLOBAL]
##     AREADIFF = /usr/lib/isdn/vorwahl.dat
## - README: Syntax description of the new "rate-at.dat"
## - better integration of "sondernummern.c" from mario.joussen@post.rwth-aachen.de
## - server.c: buffer overrun fix from Michael.Weber@Post.RWTH-Aachen.DE (Michael Weber)
##
## Revision 1.7  1997/05/28 21:22:47  luethje
## isdnlog option -b is working again ;-)
## isdnlog has new \$x variables
## README completed
##
## 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 $CALLERIDFILE ]
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
