#!/bin/sh

# description: Start/Stop roulettesound
#
# This file belongs in /etc/init.d where it will be run
# on system startup and shutdown to start the roulettesound
# This program is used for repairing the database file, every
# bootup.


# This init script contains functions specific for redhat
# and mandrake init scripts.

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


#**************** ROULETTESOUND ******************
export eSRV_DB_SERVER_IP=127.0.0.1
export eSRV_DB_PATH=/ROULETTE_DATA.GDB
export eSRV_DB_USER=SYSDBA
export eSRV_DB_PASS=masteraloha
export eSRV_MASTER_IP=192.168.0.254

export eSOUND_SOUND_FOLDER=/home/ROULETTE/FC5/RouletteMaster/AUDIO/ENG
export eSOUND_XML_RPC_PORT=6666
export eSOUND_XML_RPC_IP=127.0.0.1

export PROGRAM_FOLDER=/home/ROULETTE/FC5/RouletteMaster
export PROGRAM_FILENAME=roulettesound

export DEPENDS_ON=roulettemaster
export LD_LIBRARY_PATH="/home/ROULETTE/FC5/opt/firebird/lib:/home/ROULETTE/FC5/usr/local/lib"

export DISPLAY=:0.0

#default card number, if HDMIaudio is located on card 0, then we set CARD_NUMBER to 1
export eCARD_NUMBER=0
cat /proc/asound/pcm | grep "HDMI" | grep "00-" 2>$1 1>/dev/null 
#[ "$?" == "0" ] && eCARD_NUMBER=1


# Check if the file is there and it is executable.
if [ ! -x $PROGRAM_FOLDER/$PROGRAM_FILENAME ] 
then
	echo $"$PROGRAM_FOLDER/$PROGRAM_FILENAME does not exist or it is not an executable" 
	exit 0
fi


start(){
	if [ -z "$(ps -A |grep $DEPENDS_ON)" ] 
	then
	 	echo $"$PROGRAM_FILENAME depends on $DEPENDS_ON which is not running. Please start $DEPENDS_ON first." 
	#	return 0
	fi

	if [ -n "$(ps -A |grep $PROGRAM_FILENAME)" ] 
	then
	 	echo $"$PROGRAM_FILENAME is already running" 
		return 0
	fi

	echo -n $"Starting $PROGRAM_FILENAME:     "

	echo -n $"Setting volume."
	echo -h $"Using card "$eCARD_NUMBER
	# to naj nastavi roulettesnd /usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='Master Playback Volume', 100% unmute >> ./program_snd.out 2>/dev/null 1>/dev/null
	/usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='Front', 100% unmute >> ./program_snd.out 2>/dev/null 1>/dev/null
	/usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='Front Playback Volume', 100% unmute >> ./program_snd.out 2>/dev/null 1>/dev/null
	/usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='PCM Playback Volume', 100% unmute >> ./program_snd.out 2>/dev/null 1>/dev/null
	/usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='Center Playback Volume', 100% unmute >> ./program_snd.out 2>/dev/null 1>/dev/null
	/usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='Capture Volume', 0% mute >> ./program_snd.out 2>/dev/null 1>/dev/null
	/usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='Mic Playback Volume', 0% mute >> ./program_snd.out 2>/dev/null 1>/dev/null
	/usr/bin/amixer -c $eCARD_NUMBER cset iface=MIXER,name='CD Playback Volume', 0% mute >> ./program_snd.out 2>/dev/null 1>/dev/null

	cd $PROGRAM_FOLDER
        #before starting program, we check if a _update file is pending as program update - usefull to update at next reboot
        if [ -e $PROGRAM_FILENAME"_update" ]; then
            echo
            echo "    "Updating $PROGRAM_FOLDER/$PROGRAM_FILENAME with file $PROGRAM_FOLDER/$PROGRAM_FILENAME"_update"
            echo "    "Old version will be renamed to $PROGRAM_FOLDER/$PROGRAM_FILENAME"_old"
            echo
            
            mv -f $PROGRAM_FILENAME $PROGRAM_FILENAME"_old" > /dev/null
            mv -f $PROGRAM_FILENAME"_update" $PROGRAM_FILENAME > /dev/null
            chmod ugo+x $PROGRAM_FILENAME
        fi;

	#echo "Program start................." >> ./program_snd.out
	local starthere="$1"
	if [ $starthere -eq 1 ]; then
		#STARTHERE
		./$PROGRAM_FILENAME 
		RETVAL=$?
	else
		./$PROGRAM_FILENAME 2>/dev/null 1>/dev/null &
		RETVAL=$?
	fi
	sleep 1
	echo "Done!"

	return $RETVAL
}

stop(){
	if [ -z "$(ps -A |grep $PROGRAM_FILENAME)" ] 
	then
	 	echo $"$PROGRAM_FILENAME is not running" 
		return 0
	fi
	
	echo -n $"Stoping $PROGRAM_FILENAME:     "
	#echo "Program stop................." >> $PROGRAM_FOLDER/program_snd.out
	killall $PROGRAM_FILENAME &>/dev/null	
	RETVAL=$?
	sleep 1
	echo "Done!"
	return $RETVAL
}



# See how we were called.
case "$1" in
  start)
	start 0
	;;	
  starthere)
	start 1
	;;	
  stop) 
	stop
	;;
  restart|reload)
	stop
	start
	;;
  *)
	echo -n $"Usage: roulettesnd {start|starthere|stop|restart|reload|help}"
	exit 1
esac

exit $RETVAL

