#!/bin/bash -p
#
# (c) 2010, Amfeltec Corp
# support@amfeltec.com
#


ROOT=
TMPFILE=/tmp/tmp.$$
INITD=/etc/init.d

system=redhat  # assume redhat
if [ -f /etc/debian_version ]; then
    system=debian
fi

if [ -d /etc/rc.d/init.d ]; then
	INITD=/etc/rc.d/init.d
elif [ -d  /etc/init.d ]; then
	INITD=/etc/init.d
else
	echo "Error: No /etc/init.d directory found!"
	exit 1
fi

#Check for Zaptel Startup Script
#In case zaptel startup script exist we will
#attach to zaptel

err=1;
SNAME="dahdi"
CNAME="Dahdi"

if [ -f $INITD/zaptel ]; then
	cat $INITD/zaptel | sed -e s/ztdummy/voicesync/ > $TMPFILE
	cat $TMPFILE > $INITD/zaptel
	rm $TMPFILE
	SNAME="zaptel"
	CNAME="Zaptel"
	err=0;
fi

if [ -f $INITD/dahdi ]; then
	cat $INITD/dahdi | sed -e s/dahdi_dummy/voicesync/ > $TMPFILE
	cat $TMPFILE > $INITD/dahdi
	rm $TMPFILE
	SNAME="dahdi"
	CNAME="Dahdi"
	err=0;
fi

if [ $err -eq 0 ]; then
	echo
	echo "VoiceSync Boot Setup Complete!"
	echo "--------------------------------------"
	echo
	echo "$CNAME $INITD/$SNAME startup script has been updated with VoiceiSync!"
	echo "The $SNAME dummy has now been replaced with voiceisync as the"
	echo "default timing source for $SNAME."
	echo
	echo "Simply start $SNAME using: $INITD/$SNAME start"
	echo "No other configuratoin is necessary!"
	echo
	err=0
else
	echo
	echo
	echo "Error: No Zaptel/Dahdi startup script found in $INITD"
	echo
	echo "Please install and configure Dahdi/Zaptel before VoiceSync"
	echo 
	echo "Example:"
	echo "------- "
	echo "-> cd /usr/src/dahdi-linux-<version>/"
	echo "-> make		#build dahdi"
	echo "-> make install	#install dahdi modules"
	echo "-> make config	#install dahdi boot scripts"
	echo 
	echo "-> cd /usr/src/zaptel-<version>/"
	echo "-> ./configure  (from zaptel 1.4 and higher)"
	echo "-> make		#build zaptel"
	echo "-> make install	#install zaptel modules"
	echo "-> make config	#install zaptel boot scripts"
	echo
	echo "-> cd /usr/src/voicesync-<version>/"
	echo "-> make boot"
	echo
	echo
	err=1
fi

exit $err

