#!/bin/bash -p
home=`pwd`
#include the current directory where the script is present
#as a part of perl environment i.e. in @INC because Perl 5.26 and greater
#doesnot include current directory due to security reason
export PERL_USE_UNSAFE_INC=1
cd $home

read_meta_conf ()
{

	if [ $ostype = "Linux" ]; then
		WAN_BASE=/etc
		WAN_HOME=$WAN_BASE/wanpipe
		META_CONF=$WAN_BASE/wanpipe/wanrouter.rc
	elif [ $ostype = "FreeBSD" -o $ostype = "OpenBSD" ]; then
		WAN_BASE=/usr/local/etc
		WAN_HOME=$WAN_BASE/wanpipe
		wanrouter_rc_file=""
		if [ -r /etc/rc.conf ]; then
			. /etc/rc.conf
		fi
		if [ -n "$wanrouter_rc_file" ]; then
			WAN_HOME=${wanrouter_rc_file%/*}
		fi
		META_CONF=$wanrouter_rc_file
	fi

	# Read meta-configuration file.
	if [ -f $META_CONF ]
	then . $META_CONF
	else
		return 1 
	fi
	return 0
}

ostype=`sysctl -a |grep ostype`
ostype=`echo $ostype | sed 's/.* //'`

while [ ! -z "$1" ]; 
do
	ARGS=$ARGS"$1 "
	shift
done

read_meta_conf
if [ $? -ne 0 ]; then
	echo "ERROR: Failed to find Wanpipe meta config file!"
	exit 1
fi

cd ${WAN_HOME}/wancfg_zaptel
./wancfg_zaptel.pl --dahdi  --conf_dir=$WAN_BASE $ARGS
cd $home
