#!/bin/sh
# Copyright (C) 2000-2001 Open Source Telecom Corporation.
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# a wrapper for starting of a bayonne server based on seeking the config
# directory.

VERSION="20040708"
prefix="/usr/local"
datadir="/usr/local/share"
libdir="/usr/local/lib"
bindir="/usr/local/bin"
localstatedir="/var"
sysconfig="/etc/sysconfig/bayonne"
sysconfdir="/etc"
keyconfdir="/etc/bayonne"
libexecdir="/usr/local/libexec"
datadir_prompts="/usr/local/share/bayonne/sys"
datadir_voices="/usr/local/share/bayonne"
datadir_scripts="/usr/local/share/bayonne"
datadir_libexec="/usr/local/libexec/bayonne"
libdir_bayonne="/usr/local/lib/bayonne/20040708"
bindir_bayonne="/usr/local/lib/bayonne/20040708"
basename="bayonne"
output=""

dsopath="/usr/local/lib/bayonne/20040708"
datapath="${prefix}/share/bayonne"
repository="ftp://ftp.gnu.org/gnu/bayonne"
method="wget"
release="3"

heaps="8"
gui=""
tflag=""
trflag=""
control=""
driver="$BAYONNE_DRIVER"
soundcard="oss"
dflag=""
cflag=""
opts=""
check=""
vflag=""
vpath=""
dsn=""
vlib="UsEngM"
gdb_arg=""

DRIVER=""
CONFIG=""
PRELOAD=""
VOICE=""
DIALPLAN=""
LANGUAGE=""
PROXY_SERVER=""
PROXY_PORT=""

if test -f $sysconfig ; then
	. $sysconfig ; fi

if test -f ${keyconfdir}/bayonne.conf ; then
	. ${keyconfdir}/bayonne.conf ; fi

if test ! -z "$CONFIG" ; then
	keyconfdir="$CONFIG" ; fi

if test ! -z "$BAYONNE_CONFIG" ; then
	keyconfdir="$BAYONNE_CONFIG"/ ; fi

CONFIG_KEYDATA=${keyconfdir}/
export CONFIG_KEYDATA

if test ! -z "$BAYONNE_HEAPS" ; then
        heaps="$BAYONNE_HEAPS" ; fi

if test ! -z "$HEAPS" ; then
        heaps="$HEAPS" ; fi

if test -z "$BAYONNE_EXTENSIONS" ; then
        if test ! -z "$EXTENSIONS" ; then
                BAYONNE_EXTENSIONS=$EXTENSIONS
        fi
fi

if test -z "$BAYONNE_DRIVER" ; then
        if test ! -z "$DRIVER" ; then
                BAYONNE_DRIVER="$DRIVER"
        fi
fi
if test -z "$BAYONNE_PRELOAD" ; then
        if test ! -z "$PRELOAD" ; then
                BAYONNE_PRELOAD="$PRELOAD"
        fi
fi
if test -z "$BAYONNE_DIALPLAN" ; then
        if test ! -z "$DIALPLAN" ; then
                BAYONNE_DIALPLAN=$DIALPLAN
        fi
fi
if test -z "$BAYONNE_VOICE" ; then
        if test ! -z "$VOICE" ; then
                BAYONNE_VOICE=$VOICE
        fi
fi
if test -z "$BAYONNE_LANGUAGE" ; then
        if test ! -z "$LANGUAGE" ; then
                BAYONNE_LANGUAGE=$LANGUAGE
        fi
fi
if test -z "$BAYONNE_PROXY_SERVER" ; then
        if test ! -z "$PROXY_SERVER" ; then
                BAYONNE_PROXY_SERVER=$PROXY_SERVER
        fi
fi
if test -z "$BAYONNE_PROXY_PORT" ; then
        if test ! -z "$PROXY_PORT" ; then
                BAYONNE_PROXY_PORT=$PROXY_PORT
        fi
fi

if test ! -z "$BAYONNE_VOICE" ; then
        vlib="$BAYONNE_VOICE"
fi

if test ! -z "$BAYONNE_DRIVER" ; then
        driver="$BAYONNE_DRIVER"
fi

check_server() {
	# return successful if server running, else report offline and exit
        run=`ps -ae | grep 'bayonne[.]bin' | grep -v grep`
        if test -z "$run" ; then
                if test ! -z "$*" ; then
			echo "$*" ; fi
                exit -1
	fi
}

case "$1" in
--print)
	if test -r ${localstatedir}/bayonne.routes ; then
		cat ${localstatedir}/bayonne.routes
	elif test -f ~/.bayonne.routes ; then
		cat ~/.bayonne.routes
	else
		echo "unavailable"
		exit -1
	fi
	exit 0
	;;

--usage)
	check_server offline
        if test -r ${localstatedir}/bayonne.usage ; then
                cat ${localstatedir}/bayonne.usage
        elif test -f ~/.bayonne.usage ; then
                cat ~/.bayonne.usage
        else
                echo "unavailable"
                exit -1
        fi
        exit 0
        ;;

--stats)
	check_server offline
        if test -r ${localstatedir}/bayonne.stats ; then
                cat ${localstatedir}/bayonne.stats
        elif test -f ~/.bayonne.stats ; then
                cat ~/.bayonne.stats
        else
		echo "unavailable"
		exit -1
	fi
        exit 0
        ;;

--calls)
	check_server offline
        if test -r ${localstatedir}/bayonne.calls ; then
		cat ${localstatedir}/bayonne.calls
	elif test -f ~/.bayonne.calls ; then
		cat ~/.bayonne.calls
	else
		echo "unavailable"
		exit -1
	fi
	exit 0
	;;
--save)
	if test -w ${keyconfdir} ; then
		grep -v ^default ${localstatedir}/bayonne.routes >${keyconfdir}/route.conf ; fi
	;;
--add)
	shift
	control "route set $*"
	;;
--del)
	shift
	control "route clear $*"
	;;
--kill)
	killall -9 bayonne.bin
	exit 0
	;;
--status|--uptime)
	check_server offline
	path=`dirname $0`
	if test -x $path/btsinfo ; then
		exec $path/btsinfo $*
	fi
	exit -1
	;;
--active-scripts|--active-voices|--active-prompts|--active-voices)
        path=`dirname $0`
        if test -x $path/btsinfo ; then
                exec $path/btsinfo $*
        fi
        exit -1
	;;
--dsopath)
	echo $dsopath
	exit 0
	;;
--keypath)
	echo $keyconfdir
	exit 0
	;;
--binpath)
	echo $bindir_bayonne
	exit 0
	;;
--scripts)
	echo $datadir_scripts
	exit 0
	;;
--prompts)
	echo $datadir_prompts
	exit 0
	;;
--voices)
	echo $datadir_voices
	exit 0
	;;
--datapath)
	echo $datapath
	exit 0
	;;
--keyconfdir)
	echo $keyconfdir
	exit 0
	;;
--sysconfdir)
	echo $sysconfdir
	exit 0
	;;
--sysconfig)
	echo $sysconfig
	exit 0
	;;
--libexec)
	echo $datadir_libexec
	exit 0
	;;
--homepath)
	echo /home/bayonne
	exit 0
	;;
--down|--stop)
	control="down"
	;;
--service)
	if test "$2" == "up"
	then
		control="up"
	else
		control="down $2"
	fi
	;;
--restart)
	control="restart"
	;;
--up)
	control="up"
	;;
--version)
	echo 20040708
	exit 0
	;;
--compile)
	shift
	control="compile $*"
	;;
--perl)
	shift
	base=`dirname $0`
	if test -d ${base}/../modules/perl5 ; then
		PERL5LIB=${base}/../modules/perl5
	else
		PERL5LIB=${libdir_bayonne}/perl5
	fi
	export PERL5LIB
	exec perl $*
	exit -1
	;;
--python)
	shift
	base=`dirname $0`
	if test -d ${base}/../modules/python ; then
		PYTHONPATH=${base}/../modules/python
	else
		PYTHONPATH=${libdir_bayonne}/python
	fi
	export PYTHONPATH
	exec python $*
	exit -1
	;;
--start)
	shift
	;;
--control)
	shift
	control="$*"
	;;
esac

if test ! -z "$control" ; then

	check_server "bayonne --control: no bayonne instance running"

	if test -w /var/run/bayonne/bayonne.ctrl ; then
        	echo "$control" >/var/run/bayonne/bayonne.ctrl
	elif test -w /var/run/bayonne.ctrl ; then
        	echo "$control" >/var/run/bayonne.ctrl
	elif test -w ~/.bayonne.ctrl ; then
        	echo "$control" >~/.bayonne.ctrl
	else
        	echo "bayonne --control: unable to find fifo"
		exit -1
	fi
	exit 0
fi

if test ! -d "$datapath/$vlib" -a -w "$datapath" ; then
	wd=`pwd`
	cd "$datapath"
	wget "$repository/voices/$vlib".tar.gz
	if test -f "$vlib".tar.gz ; then
		tar -xzf "$vlib".tar.gz
		rm -f "$vlib".tar.gz
	fi
	cd "$wd"
fi

for opt in $* ; do
	if test ! -z "$dflag" ; then
		driver="$driver $opt"
		dflag=""
	elif test ! -z "$cflag" ; then
		CONFIG_KEYDATA=$opt/
		export CONFIG_KEYDATA
		cflag=""
		opt=""
	else
	case $opt in
	--test)
		# this assures "--test" mode does not use/import info
		# from production or "live" configurations...
		CONFIG_KEYDATA=${keyconfdir}-test/
		export CONFIG_KEYDATA
		tflag="--test"
		trflag="--trace"
		;;
	--gui|-G)
		gui="--gui"
		;;
	--dialogic)
		driver="dialogic"
		;;
	--virtual)
		driver="virtual"
		;;
	--dummy | --sound*)
		driver="$soundcard"
		;;
	--vpb | --voicetronix)
		driver="vpb"
		;;
	--openline*)
		driver="openline4"
		;;
	--openswitch)
		driver="openswitch"
		;;
	--aculab)
		driver="aculab"
		;;
	--quicknet|--ltapi|--phonedev)
		driver="ltapi"
		;;
	--capi*)
		driver="capi20"
		;;
	--global*)
		driver="globalcall"
		;;
	--oh323)
		driver="oh323"
		;;
	--iax)
		driver="iax"
		;;
	--gdb)
		AIXTHREAD_RWLOCK_DEBUG=ON
		AIXTHREAD_COND_DEBUG=ON
		AIXTHREAD_MUTEX_DEBUG=ON
		export AIXTHREAD_RWLOCK_DEBUG AIXTHREAD_COND_DEBUG AIXTHREAD_MUTEX_DEBUG
		gdb_arg="gdb --args"
		;;
	--ldd)
		gdb_arg="ldd"
		;;
	--check)
		check=$opt
		;;
	--config=*)
		BAYONNE_CONFIG=`echo $opt s/--config=//`/
		;;		
	--config)
		cflag=$opt
		;;
	--driver=*)
		driver=`echo $opt | sed s/--driver=//`
		;;
	-d | --driver)
		dflag="$opt"
		;;
	--version=*)
		vpath=`echo $opt | sed s/--version=//`
		dsopath="${exec_prefix}/lib/bayonne/$vpath"
		libdir_bayonne=${dsopath}
		bindir_bayonne=${dsopath}
		;;
	--version | -V)
		vflag="--version"
		;;
	--status)
		vflag="--status"
		;;
	--dbdriver=*)
                tmp=`echo $opt | sed s/--dbdriver=//`
                SQL_DRIVER="$tmp"
		export SQL_DRIVER
                ;;
	--dbserver=*)
		tmp=`echo $opt | sed s/--dbserver=//`
		SQL_SERVER="$tmp"
		export SQL_SERVER
		;;
	--database=*)
		tmp=`echo $opt | sed s/--database=//`
		SQL_DATABASE="$tmp"
		export SQL_DATABASE
		;;
	--dsn=*)
		tmp=`echo $opt | sed s/--dsn=//`
		SQL_DATABASE="$tmp"
		export SQL_DATABASE
		;;
	*)
		opts="$opts $opt"
		;;
	esac
	fi
done

drivers="$driver"
BAYONNE_DRIVER=""
for driver in $drivers ; do
	case $driver in
	global*)
		driver="globalcall"
		;;
	capi*)
		driver="capi20"
		;;
	voicetronix)
		driver="vpb"
		;;
	quicknet|phonedev)
		driver="ltapi"
		;;
	sound*|dummy|demo)
		driver="$soundcard"
		;;
	esac
	BAYONNE_DRIVER="$BAYONNE_DRIVER $driver"
	if test -z "$BAYONNE_PRELOAD" ; then
		case $driver in
		dialogic)
			BAYONNE_PRELOAD=/usr/lib/libLiS.so
			;;
		globalcall)
			BAYONNE_PRELOAD=/usr/lib/libLiS.so:/usr/dialogic/lib/libgc.so:/usr/dialogic/lib/libdevmap.so
			;;
		esac
	fi
done

if test ! -z "$BAYONNE_DRIVER" ; then
	export BAYONNE_DRIVER
fi

if test ! -z "$BAYONNE_LANGUAGE" ; then
	export BAYONNE_LANGUAGE
fi

if test ! -z "$BAYONNE_VOICE" ; then
	export BAYONNE_VOICE
fi

if test ! -z "$BAYONNE_PROXY" ; then
	export BAYONNE_PROXY
fi

if test ! -z "$BAYONNE_DIALPLAN" ; then
	export BAYONNE_DIALPLAN
fi

if test ! -z "$BAYONNE_PROXY_SERVER" ; then
	export BAYONNE_PROXY_SERVER
fi

if test ! -z "$BAYONNE_PROXY_PORT" ; then
	export BAYONNE_PROXY_PORT
fi

if test ! -z "$BAYONNE_PRELOAD" ; then
	if test -z "$LD_PRELOAD" ; then
		LD_PRELOAD="$BAYONNE_PRELOAD"
	else
		LD_PRELOAD="$LD_PRELOAD:$BAYONNE_PRELOAD"
	fi
	export LD_PRELOAD
fi

if test ! -z "$vflag" ; then opts=$vflag ; fi

if test ! -z "$BAYONNE_EXTENSIONS" ; then opts="-$BAYONNE_EXTENSIONS $opts" ; fi

if test -z "$check" ; then
        run=`ps -ae | grep 'bayonne[.]bin' | grep -v grep`
        if test ! -z "$run" ; then
		echo "bayonne: *** already running" ; exit -1 ; fi

	# AIX-ism to paralelize memory allocation
	if test ! -z "$heaps" ; then
		MALLOCMULTIHEAP="$heaps"
		export MALLOCMULTIHEAP
	fi

	BAYONNE_LIBRARY=${dsopath}
	export BAYONNE_LIBRARY

	path=`dirname $0`
	if test -x ${path}/bayonne.bin -a "$path" != "$bindir" ; then
		exec $gdb_arg $path/bayonne.bin --prefix "/usr/local" $gui $tflag $opts
	else
		exec $gdb_arg ${bindir_bayonne}/bayonne.bin --prefix "/usr/local" $gui $trflag $opts
	fi
	exit -1
fi

if test ! -z "$SQL_DATABASE" ; then
        echo "DATABASE = $SQL_DATABASE" ; fi

if test ! -z "$SQL_SERVER" ; then
        echo "DBSERVER = $SQL_SERVER" ; fi

echo "PRELOAD = $LD_PRELOAD"
echo "SYSCONF = $sysconfig"
echo "DRIVER = $BAYONNE_DRIVER"
echo "CONFIG = $CONFIG_KEYDATA"

