#!/bin/sh
#
# Startup script for the Adobe Flash Media Server (FMS)
#
# description:
# processname: fmsmaster 
# check user

USERID=`id | sed -e 's/).*//; s/^.*(//;'`
if [ "X$USERID" != "Xroot" ]; then
  echo ""
  echo "ERROR: You must be logged in as root to use this command."
  echo "       If you really need to start the server as non-root please"
  echo "       modify this script."
  echo ""
  exit 1
fi

ulimit -n 64536
ulimit -s 1024

# update the number of semaphores available
# final parameter changes the default of 128 semaphore handles
# to 1024 semaphore handles
/sbin/sysctl -q -w kernel.sem="250 32000 32 1024"

# increase max number of processes; since each thread is treated
# as a process, we want to bump this number up so we can create
# more core processes.
max_processes=`ulimit -u`
if [ "$max_processes" != "unlimited" ]; then
  if [ $max_processes -lt 32768 ]; then
    ulimit -u 32768
  fi
fi

PATH=${PATH}:/usr/bin
export PATH

# Path to FMS binary, and short-form for messages.
INSTALLDIR=`dirname $0`
prog="Adobe Flash Media Server"
server=fmsmaster
RETVAL=0
NLSPATH=./tcSrvMsg:$NLSPATH; export NLSPATH
getconf GNU_LIBPTHREAD_VERSION

# setup tmp dir; check if env var is set, otherwise use install dir
tmpdir=${FMS_TMP_DIR}
if [ "$tmpdir" = "" ]; then
  # tmpdir is empty, so use install dir
  tmpdir=$INSTALLDIR"/tmp"
fi

# if tmpdir doesn't end in a slash, add one
lastchar=${tmpdir:${#tmpdir}-1:1}
if [ "$lastchar" != "/" ]; then
  tmpdir=$tmpdir"/"
fi

# check if dir exists; create if necessary
if [ ! -e $tmpdir ]; then
  mkdir -p $tmpdir
fi

# change permissions to make it rwx for everyone
chmod -R 777 $tmpdir

# make sure only one script (server or adminserver) is running at a time
lockname=$tmpdir"script.lck"
displayCount=0
while [ 1 ]
  do
    if mkdir "$lockname" > /dev/null 2>&1
    then
      trap 'rmdir "$lockname"' 0
      trap "exit 2" 1 2 3 15
      break
    else
        if [ $displayCount -eq 0 ]; then
          echo "Waiting for another script to finish..."
        fi
      let displayCount=displayCount+1
      sleep 1
    fi
  done

cleanup() {
	# remember current dir
	pushd $PWD > /dev/null 2>&1
	# cd into tmp dir before doing ls so we don't get the path
	cd $tmpdir > /dev/null 2>&1
	if [ $? -eq 0 ]; then
        	for i in `ls -1 *.pid 2> /dev/null`; do
                	PROCEXISTS=`echo $i | cut -d "." -f 1 | xargs skill -n`
                	if [ "x$PROCEXISTS" = "x" ]; then
                        	rm -rf $i
                	fi
        	done
	fi
	# cd back to old dir
	popd > /dev/null 2>&1
}
start() {
        echo "Starting $prog (please check /var/log/messages)"
	cleanup
	pushd $PWD > /dev/null 2>&1
	cd $INSTALLDIR
	export LD_LIBRARY_PATH=".${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"

	# check for nspr
	ldd ./$server | grep "\(libnspr\)*\(not found\)" > /dev/null
	if [ $? -eq 0 ]; then
		echo "Error: Flash Media Server needs the NSPR library installed."
		exit 1
	fi

	ldd ./$server | grep "not found"
	if [ $? -eq 0 ]; then
		echo "Error: Flash Media Server needs the missing library installed."
		exit 1;
	fi

	if [ $# -eq 0 ]; then
        	nohup ./$server -console $@ > /dev/null 2>&1 &
	else
        	nohup ./$server $@ > /dev/null 2>&1 &
	fi
        RETVAL=$?
        echo
	popd > /dev/null 2>&1
        return $RETVAL
}
stop() {
        echo "Stopping $prog (please check /var/log/messages)"
	pushd $PWD > /dev/null 2>&1
	cd $INSTALLDIR
        os="`uname -s`"
        if [ "x$os" = "xLinux" ]; then
          killall -s TERM $server
	else
          pkill -TERM -x $server
	fi

	# We can wait a longer period of time here; master
	# has a built-in timeout and will kill any edges
	# and cores that don't shutdown. So let master do
	# its thing and give it every opportunity to shut
	# down normally
	count=60
	stopped=0
	while [ $count -gt 0 ] && [ $stopped -eq 0 ]
	do
		if [ ! -f ./fmsmaster.pid ]; then
			stopped=1
		else
			let count=count-1;
			sleep 1
		fi
	done

	if [ ! -f ./fmsmaster.pid ]; then
		echo "Server has shutdown..."
	else
		killall -s KILL fmsmaster
		killall -s KILL fmsedge
		killall -s KILL fmscore
	fi
        RETVAL=$?
	#remove unused shared memory
	./shmrd
	popd > /dev/null 2>&1
	cleanup
        echo
}

# See how we were called.
cmd=$1
export cmd
shift
case "$cmd" in
  start)
        start $@
	sleep 5
        ;;
  stop)
        stop
        ;;
  restart)
        stop
	sleep 5
        start $@
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $RETVAL
