#!/bin/bash

if [ "$(id -u)" != "0" ]
then
	echo $STRING_APPEND "This script must be run as root."
	exit 1
fi


chmod 666 /var/log/cisco/*.*

# Check if pulseaudio process is running
 echo "uc-start, start checking pulseaudio process">> /var/log/cisco/vxcUpStart.log
 
count=0
waitingtime=20
while [ -z $(pidof pulseaudio) ] && [ "$count" != "$waitingtime" ] ; do
  echo "pulseaudio process is not running,sleep 1s">> /var/log/cisco/vxcUpStart.log
    sleep 1
    count=$[count + 1]
 done
if [ "$count" == "$waitingtime" ]
then
    echo "pulseaudio process is not running after ${count} s">> /var/log/cisco/vxcUpStart.log
else
    echo "pulseaudio process is running after ${count} s" >> /var/log/cisco/vxcUpStart.log
fi

# Start the VXC process.
su - ${USER} -c "pidrun.sh -c run_vxc.sh -o /var/log/cisco/vxcConsole.log -e /var/log/cisco/vxcError.log &"


# If cchwg is not already running.
if [ -z $(pidof cchwg) ]
then
    # We want to wait until the VXC process is up and running before we start the cchwg process.
#    while [ -z $(pidof vxc) ]
#    do
#        echo "waiting for vxc to start"
# 	sleep 0.5
#    done

#    echo "vxc started"

	su -p ${USER} -c "nice -n 19 /usr/bin/cchwg 1> /dev/null 2> /dev/null &"

    # Once the cchwg process is running we can exit.
	while [ -z $(pidof cchwg) ]
	do
        echo "waiting for cchwg to start"
	    sleep 0.5
	done

    echo "cchwg started"

    # We want to fork both of those processes but we will need to wait until they are running before we exit. This is due to the automation scripts for testing VXC.
fi

# If LogMeUp is not already running.
if [ -z $(pidof LogMeUp) ]
then
    # Start LogCollection process
	export STD_LIB_LOCATION="/usr/lib/cpve/"
	export LD_LIBRARY_PATH=$STD_LIB_LOCATION:$LD_LIBRARY_PATH
    su -p ${USER} -c "pidrun.sh -c LogMeUp -o /dev/null -e /dev/null -t 3 &"
	
    echo "LogMeUp started"
fi

echo "Unified Communications Started."

echo "Print info about process vxc and wfica:">> /var/log/cisco/vxcUpStart.log
ps -ef|grep -E "vxc|wfica">> /var/log/cisco/vxcUpStart.log
