#!/bin/sh
# $Id: linphone-config.init,v 1.2 2003/09/19 14:32:28 dude Exp $
#
# linphone-config    This shell script creates a linhpone config file
#
# chkconfig: 345 98 02
# description: CCSG OSDial Linphone Setup
# processname: linphone-config

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0
LFILE="/home/centos/.linphonerc"
LFILE2="/home/centos/.gnome2/linphone"

start() {
	TIMEOUT=10
	if [ -f "/mnt/live/config.txt" ]; then
		. /mnt/live/config.txt
	fi
	if [ "$SERVER" = "auto" ]; then
		SERVER=""
	fi
	if [ -z "$SERVER" ]; then
		echo -n "Scanning network for dialer."
		SERVER=`grep 'osdial$' /etc/hosts | cut -d '	' -f 1`
		if [ -z "$SERVER" ]; then
			sleep 1
			echo -n "..."
			SERVER=`grep 'osdial$' /etc/hosts | cut -d '	' -f 1`
			if [ -z "$SERVER" ]; then
				sleep 1
				echo -n "..."
				SERVER=`grep 'osdial$' /etc/hosts | cut -d '	' -f 1`
				if [ -z "$SERVER" ]; then
					sleep 2
					echo -n "..."
					SERVER=`grep 'osdial$' /etc/hosts | cut -d '	' -f 1`
					if [ -z "$SERVER" ]; then
						sleep 2
						echo -n "..."
						SERVER=`grep 'osdial$' /etc/hosts | cut -d '	' -f 1`
						if [ -z "$SERVER" ]; then
							sleep 3
							echo -n "..."
							SERVER=`grep 'osdial$' /etc/hosts | cut -d '	' -f 1`
						fi
					fi
				fi
			fi
		fi
	fi
	if [ -z "$SERVER" ]; then
		echo "failed!"
	else
		echo "success!"
	fi
	if [ -n "$SERVER" -a -n "$EXT" -a -n "$USER" -a -n "$PASS" ]; then
		TIMEOUT=3
	fi
	if [ -z "$EXT" ]; then
		EXT=1000
	fi
	if [ -z "$USER" ]; then
		USER=1000
	fi
	if [ -z "$PASS" ]; then
		PASS=1000
	fi
	dialog --clear --backtitle "Call Center Service Group" --pause "Press Enter to configure the softphone." 8 43 $TIMEOUT
	if [ "$?" = "1" ]; then
		TEMP=/tmp/linphone-config.$$
		dialog --clear --backtitle "Call Center Service Group" \
			--form "             Softphone Extension Configuration\n\nIf the dialer was auto-detected, a value will be present.\nValid extensions are 1000-1009.\nUsername and password is same as extension.\n " 20 65 6 \
			"Dialer IP Address    :" 2 9 "$SERVER" 2 32 16 15 \
			"Softphone Extension #:" 3 9 "$EXT" 3 32 5 4 \
			"Softphone Username   :" 4 9 "$USER" 4 32 5 4 \
			"Softphone Password   :" 5 9 "$PASS" 5 32 5 4 \
			2> $TEMP
		if [ "$?" = "0" ]; then
			set `cat $TEMP`
			SERVER=$1
			EXT=$2
			USER=$3
			PASS=$4
		else
			SERVER=""
			EXT=""
			USER=""
			PASS=""
		fi
	fi
	if [ -z "$SERVER" -o -z "$EXT" -o -z "$USER" -o -z "$PASS" ]; then
		EXT=""
	fi
	if [ -f "$LFILE" ]; then
		rm -f $LFILE
	fi
	if [ -f "$LFILE2" ]; then
		rm -f $LFILE2
	fi
	if [ -n "$SERVER" -a -n "$EXT" -a -n "$USER" -a -n "$PASS" ]; then
		cp -f /usr/share/agentcd/linphonerc $LFILE
		/usr/bin/perl -pi -e "s|SERVER|$SERVER|g" $LFILE
		/usr/bin/perl -pi -e "s|EXT|$EXT|g" $LFILE
		/usr/bin/perl -pi -e "s|USER|$USER|g" $LFILE
		/usr/bin/perl -pi -e "s|PASS|$PASS|g" $LFILE
		cp -f $LFILE $LFILE2
		chown centos:centos $LFILE
		chown centos:centos $LFILE2
	fi
	LINPHONEPIDS=`/bin/ps -ef | /bin/grep linphonec | /bin/grep -v grep | /bin/awk '{ print $2 }' | /usr/bin/tr '\n' ' '`
	if [ -n "$LINPHONEPIDS" ]; then
		/bin/kill -9 $LINPHONEPIDS
	fi
	return 0
}

stop() {
	echo
	return 0
}

restart() {
	stop
	start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  *)
	echo "Usage: linphone-config {start|stop|restart}"
	exit 1
esac

exit $?
