#!/bin/bash

BASE=/SpliceCom
CMD="$BASE/bin/VoicemailApp -gk 127.0.0.1 -logcp"
export LD_LIBRARY_PATH=$BASE/lib:$BASE/swift/lib
export VXIDIR=$BASE/OpenVXI

cd $BASE

# Now Check for Voicemail Restore
if [ -f /SpliceCom/ftp/voicemail.tar ]; then
mv /SpliceCom/ftp/voicemail.tar .
tar -xf voicemail.tar
rm voicemail.tar                                                            
fi                                                                         
                                                              
# Remove Voicemail HTTP cache             
rm -rf /SpliceCom/OpenVXI/cache_sbinet

echo $$ > $0.pid

while :
do
	# start process
	$CMD > /dev/null &
	pid=$!

	# catch signals
	trap "break" SIGINT SIGTERM

	# wait for process to exit
	wait $pid

	# process exited, wait then restart
	if [ -f /SpliceCom/logrestart ]; then /SpliceCom/logrestart $CMD; fi
	sleep 2
done

# signal occurred, so terminate the process
kill $pid

rm -f $0.pid

