#!/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
#
# a wrapper for starting of a bayonne server based on seeking the config
# directory.

cxxcompiler="c++"
cxxflags="-g -O2 -I/usr/include/cc++2 -O2 -pipe -march=i586 -mcpu=pentiumpro -I/usr/include/libxml2/libxml/ -D_GNU_SOURCE -DXML_SCRIPTS"
basename="bayonne"

prefix="/usr"
exec_prefix="${prefix}"
sysconfig="/etc"
dsopath="${exec_prefix}/lib/bayonne/1.1.11"
datapath="${prefix}/share/bayonne"

control=""
program=""
driver="$BAYONNE_DRIVER"
dflag=""
cflag=""
opts=""
check=""
vflag=""
vpath=""

case "$1" in
--wrapper) 
	shift
	program=$dsopath/wrapper.bin
	;;
--setup)
	shift
	program=$dsopath/setup.bin
	;;
--policy)
	shift
	program=$dsopath/policy.bin
	;;
--dsopath)
	echo $dsopath
	exit 0
	;;
--datapath)
	echo $datapath
	exit 0
	;;
--adminpath)
	echo $datapath/admin
	exit 0
	;;
--homepath)
	echo /home/bayonne
	exit 0
	;;
--admin)
	shift
	program=$datapath/admin/main.sh
	;;
--down)
	control="down"
	;;
--service)
	if test "$2" == "up"
	then
		control="up"
	else
		control="down $2"
	fi
	;;
--restart)
	control="restart"
	;;
--up)
	control="up"
	;;
--flags)
	echo $cxxflags
	exit 0
	;;
--make|--module)
	shift
	for path in $* ; do
		$cxxcompiler $cxxflags -c $path
	done
	exit 0
	;;
--link)
	shift
	name="$1"
	shift
	exec libtool --mode=link $cxxcompiler -o $dsopath/$name -module -shared $*
	exit 0
	;;
--install)
	shift
	dir="$datapath/$1"
	shift
	mkdir -p $dir
	for path in $* ; do
		base=`basename $path`
		install $path $dir/$base
	done
	exit 0
	;;
--compile)
	control="compile"
	;;
--start)
	shift
	;;
--control)
	shift
	control="$*"
	;;
esac

if test ! -z "$program" ; then
	if test -f /etc/sysconfig/bayonne ; then
		. /etc/sysconfig/bayonne
		if test ! -z "$CONFIG" ; then
			sysconfig="$CONFIG"
		fi
	fi
	if test ! -z "$BAYONNE_CONFIG" ; then
		sysconfig="$BAYONNE_CONFIG"
	fi

	CONFIG_KEYDATA=$sysconfig/
	export CONFIG_KEYDATA

	exec $program $*
	exit -1
fi

if test ! -z "$control" ; then

	run=`ps ax`
	run=`echo $run | grep bayonne.bin`
	if test -z "$run" ; then
        	echo "bayonne --control: no bayonne instance running"
        	exit -1
	fi

	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 -f /etc/sysconfig/bayonne ; then
	DRIVER=""
	CONFIG=""
	PRELOAD=""
	VOICE=""
	LANGUAGE=""
	PROXY_SERVER=""
	PROXY_PORT=""

	. /etc/sysconfig/bayonne

	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_CONFIG" ; then
		if test ! -z "$CONFIG" ; then
			BAYONNE_CONFIG=$CONFIG/
		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
fi

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

for opt in $* ; do
	if test ! -z $dflag ; then
		driver=$opt
		dflag=""
	elif test ! -z $cflag ; then
		BAYONNE_CONFIG=$opt/
		cflag=""
		opt=""
	else
	case $opt in
	--dialogic)
		driver="dialogic"
		;;
	--dummy | --sound*)
		driver="dummy"
		;;
	--vpb | --voicetronix)
		driver="vpb"
		;;
	--aculab)
		driver="aculab"
		;;
	--quicknet)
		driver="phonedev"
		;;
	--capi*)
		driver="capi20"
		;;
	--global*)
		driver="globalcall"
		;;
	--check)
		check=$opt
		;;
	--config=*)
		BAYONNE_CONFIG=`echo $opt s/--config=//`/
		;;		
	--config)
		cflag=$opt
		;;
	--driver=*)
		driver=`echo $opt | sed s/--driver=//`
		;;
	--alt=*)
		alt=`echo $opt | sed s/--alt=//`
		if test -d /$sysconfig/$alt ; then
			BAYONNE_CONFIG=`echo /$sysconfig/$alt/`
		fi
		opts="$opts --alt $alt"
		;;
	-d | --driver)
		dflag="$opt"
		;;
	--version=*)
		vpath=`echo $opt | sed s/--version=//`
		dsopath="${exec_prefix}/lib/bayonne/$vpath"
		;;
	--version | -V)
		vflag="--version"
		;;
	--status)
		vflag="--status"
		;;
	*)
		opts="$opts $opt"
		;;
	esac
	fi
done

if test ! -z "$driver" ; then
	case $driver in
	global*)
		driver="globalcall"
		;;
	capi*)
		driver="capi20"
		;;
	voicetronix)
		driver="vpb"
		;;
	quicknet)
		driver="phonedev"
		;;
	sound*)
		driver="dummy"
		;;
	esac
	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
fi

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_PROXY_SERVER" ; then
	export BAYONNE_PROXY_SERVER
fi

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

if test -d /etc/$basename ; then
	CONFIG_KEYDATA=/etc/$basename/
elif test -d "/usr/etc/$basename" ; then
	CONFIG_KEYDATA="/usr/etc/$basename/"
fi

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

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

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

if test ! -z "$BAYONNE_PRELOAD" ; then
	if test -z "$LD_PRELOAD" ; then
		export LD_PRELOAD="$BAYONNE_PRELOAD"
	else
		export LD_PRELOAD="$LD_PRELOAD:$BAYONNE_PRELOAD"
	fi
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
	path=`dirname $0`
	if test -x $path/bayonne.bin -a $path != "$exec_prefix"/sbin ; then
		exec $path/bayonne.bin --prefix "/usr" $opts
	else
		exec $dsopath/bayonne.bin --prefix "/usr" $opts
	fi
	exit -1
fi

if test -z $CONFIG_KEYDATA ; then
	CONFIG_KEYDATA="/etc/"
fi

echo "CONFIG = $BAYONNE_CONFIG"
echo "PRELOAD = $LD_PRELOAD"
echo "DRIVER = $BAYONNE_DRIVER"
echo "CONFIG = $CONFIG_KEYDATA"
