#! /bin/sh -f
# Written by Furey.
# With additions from Tony.
# Ported to 'sh' by Loki for those who don't have 'csh'.
# Thanks to Alan and Grodyn for their help on the redirecting stuff - Loki

# Lock file checking to prevent multiple startupSH copies running - Marble
echo
if [ -e ROM.LOCK ]
then
	rom_pid=`cat ROM.LOCK`
	if [ -d /proc/$rom_pid ]
	then
		echo WARNING: startupSH shell script already running !!
		echo WARNING: It is running under PID $rom_pid
		echo
		exit
	else
		echo WARNING: ROM.LOCK contains non-existent PID !! Removing file..
		rm ROM.LOCK
	fi
fi

ps -auxww |grep hotl |grep startupSH >LOCK.startupSH
if [ -s LOCK.startupSH ]
then
    echo Strange ... I wonder how did the program come into this loop .. 
	# if [ ! -e ROM.LOCK ]
	# then
	#	echo WARNING: startupSH running without lockfile - ROM.LOCK
	# fi
	# rm LOCK.startupSH
	# echo
	# exit
fi

if [ -e ROM.LOCK ]
then
	echo Removing stale lock file ...
	rm ROM.LOCK
fi

echo Creating lock file for startupSH ...
echo $$ > ROM.LOCK
echo

# exit

# Set the port number.
if [ "$1" != "" ]
then
         port=$1 
else
#        port=5000
         port=4040
fi

if [ "$2" != "" ]
then
         imc_port=$2 
else
         imc_port=7777
fi

# Change to area directory.
cd /home/hotl/HOTL2/area

# No need on BSD - Set core dump limit (Default is 0) - Ender
# ulimit -c 1024

# Set limits. (cannot be used in a 'sh' script - Loki)
# nohup
# nice
# limit stack 1024k
if [ -r shutdown.txt ]
then
         rm -f shutdown.txt
fi

while [ 1 ]
do
    # If you want to have logs in a different directory,
    #   change the 'logfile=' line to reflect the directory name.
    # It isn't used but I let it in anyway
    index=1000
    while [ 1 ]
    do
        logfile=/home/hotl/HOTL2/log/$index.log
        if [ -r $logfile ]
        then
                 # If you don't have bc you have a problem... :(
                 index=`echo $index + 1|bc`
                 if [ $index -gt 1500 ]
                 then exit 1
                 fi
        else
                 break
        fi
    done
    #ps -aux | grep perl | grep bot | grep rom | awk '{print $2}' | xargs kill
    #/home/hotl/HOTL2/IRCBot/bot.pl &

    coreindex=1
    while [ 1 ]
    do
        corefile=/home/hotl/HOTL2/log/rom-core.$coreindex
        if [ -r $corefile ]
        then
                 # If you don't have bc you have a problem... :(
                 coreindex=`echo $coreindex + 1|bc`
                 if [ $coreindex -gt 5 ]
                     then exit 1
                 fi
        else
		        if [ -r /home/hotl/HOTL2/area/rom.core ]
		        then
                	mv /home/hotl/HOTL2/area/rom.core $corefile
				fi
				break
        fi
    done

    # Run rom.
    # Thanks to Alan and Grodyn for their help on the redirecting stuff - Loki

	# Use below to disable imc connection
    /home/hotl/HOTL2/src/rom $port $imc_port 1 1>$logfile 2>&1

	# Use below to enable imc connection
    #/home/hotl/HOTL2/src/rom $port $imc_port 1>$logfile 2>&1

    # Delete this out if no adb.
#    if [ -r core ]
#    then
#            echo '$c' | gdb /home/hotl/HOTL2/src/rom
#    fi
    # Delete the core file if there is one - Ender
#    if [ -r core.rom ]
#    then
#	rm core.rom
#    fi

    # Restart, giving old connections a chance to die.
#    if [ -r shutdown.txt ]
#    then
#        rm -f shutdown.txt
#        exit 0
#    fi
    sleep 10
done
