#!/bin/sh
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
}

check_libsng_isdn ()
{
	check_32=0; #0 not found; 1 found`
	check_64=0;	
	if [ ! -f "/usr/lib/libsng_isdn.so" ];then
		check_32=1; 
	fi

	if [ -d "/usr/lib64/" ];then
		if [ ! -f "/usr/lib64/libsng_isdn.so" ];then
			check_64=1;
			#check_32=0;
		else check_32=0;
		fi
	fi


	if [ "$check_64" -eq 1 ];then
		echo "LIBSNG_ISDN is not installed in your system !!!";
		echo "----------------------------------------------";
		echo "";
		echo "Do you want to continue? (y/n)"
		read choice
		
		case $choice in
			y) ./wancfg_zaptel.pl --trillium --conf_dir=$WAN_BASE $ARGS;;
			n) exit;
		esac
	
	elif [ "$check_32" -eq 1 ];then
		echo "LIBSNG_ISDN is not installed in your system!!!";
		echo "----------------------------------------------";
		echo "";
		echo "Do you want to continue? (y/n)"
		read choice

		case $choice in
			y) ./wancfg_zaptel.pl --trillium --conf_dir=$WAN_BASE $ARGS;;
			n) exit;;
		esac
	
	else
		./wancfg_zaptel.pl --trillium --conf_dir=$WAN_BASE $ARGS;		
	fi
}


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
#clear
#check_libsng_isdn;
./wancfg_zaptel.pl --trillium --conf_dir=$WAN_BASE $ARGS;		
cd $home
