#!/bin/sh
# Copyright (c) 2005 Men & Mice
# Startup script for the Men & Mice Updater
#
# chkconfig: - 55 45
# description: Startup script for the Men & Mice Updater
# probe: false
#
### BEGIN INIT INFO
# Provides:			mmupdater
# Required-Start:	$network $syslog
# Required-Stop:
# Default-Start:	3 5
# Default-Stop:
# Description:		Starts the Men & Mice Updater
### END INIT INFO

PROGRAM=mmupdaterd
PROGRAMNAME="Men & Mice Updater"
LOCATION="_PATH_"
CHROOTDIR="_CHROOTDIR_"
PARAMS="_PARAM1_"

##################### End of program specific definitions #####################

# Echo replacement. Doesn't print anything if we are in quiet mode
doEcho() {
	if [ $quiet -eq 0 ] ; then
		if [ "$1" = "-n" ] ; then
			shift
			if [ $target_os -eq $solaris ] ; then
				echo $@ "\c"
			else
				echo -n $@
			fi
		else
			echo $@
		fi
	fi
}

# Returns true if $PROGRAM is running
isRunning() {
	PIDFILE=""
	if [ -f "$CHROOTDIR/var/run/$PROGRAM/$PROGRAM.pid" ] ; then
		PIDFILE="$CHROOTDIR/var/run/$PROGRAM/$PROGRAM.pid"
	elif [ -f "$CHROOTDIR/var/run/$PROGRAM.pid" ] ; then
		PIDFILE="$CHROOTDIR/var/run/$PROGRAM.pid"
	fi

	if [ "x$PIDFILE" != "x" ] ; then
		# PID file exists -- check the PID it contains
		PID=`cat "$PIDFILE"`
		if [ "x$PID" = "x" ] ; then
			# PID file is empty -- remove PID file and return failure
			rm "$PIDFILE"
			return 1
		fi


		if [ $target_os -eq $tinycore ] ; then
			PS_OUTPUT=`ps -o pid | grep -q $PID`
		else
			PS_OUTPUT=`ps -p $PID 2> /dev/null`
		fi
			
		if [ $? -ne 0 ] ; then
			# PID in file not running -- remove PID file and return failure
			rm "$PIDFILE"
			return 1
		fi

		# Make sure the PID file is accessible outside the CHROOT as well
		# (helps special distro functions, relying on the PID file, to function)
		if [ "x$CHROOTDIR" != "x" -a ! -h /var/run/$PROGRAM.pid ] ; then
			if [ ! -f /var/run/$PROGRAM.pid ] ; then
				ln -s "$PIDFILE" /var/run/$PROGRAM.pid
			else
				# There's another PID file for this process in /var/run :S
				doEcho "There are PID files for $PROGRAM in (at least) two places:"
				doEcho "$PIDFILE and /var/run/$PROGRAM.pid"
				doEcho -n "The process specified in $PIDFILE is: "
			fi
		fi

		# PID in file is running -- return success
		return 0
	fi

	# PID file does not exist -- fall back on looking for $PROGRAM in ps output
	if [ $target_os -eq $freebsd -o $target_os -eq $macosx ] ; then
		PSARGS="auxww"
	else
		PSARGS="-ef"
	fi

	PS_OUTPUT=`ps $PSARGS | grep $PROGRAM | grep -v "grep $PROGRAM"`
	if [ -n "$PS_OUTPUT" ] ; then
		# A process was found with the name $PROGRAM using ps -- return success
		return 0
	fi

	# No process found with name $PROGRAM using ps -- return failure
	return 1
}

# Attempts to start $PROGRAM
doStart() {
	if isRunning ; then
		doEcho "$PROGRAMNAME is already running"
		exit $RETVAL
	fi

	if [ ! -d "$CHROOTDIR/dev" ] ; then
		mkdir "$CHROOTDIR/dev"
	fi
	if [ ! -c "$CHROOTDIR/dev/urandom" ] ; then
		DEVFSDIR=`df | grep "$CHROOTDIR/dev"`
		DEVFS=`which devfs 2> /dev/null`
		MKNOD=`which mknod 2> /dev/null`
		if [ "x$DEVFSDIR" != "x" -a "x$DEVFS" != "x" ] ; then
			$DEVFS -m "$CHROOTDIR/dev" rule apply path urandom unhide
		elif [ "x$MKNOD" != "x" ] ; then
			if [ $target_os -eq $freebsd -o $target_os -eq $macosx ] ; then
				RANDOM_MINOR=`ls -l /dev/random | awk '{ print $6; }'`
				RANDOM_MAJOR=`ls -l /dev/random | awk '{ print $5; }'`
				RANDOM_MAJOR=`echo $RANDOM_MAJOR | awk -F, '{ print $1; }'`
				$MKNOD "$CHROOTDIR/dev/urandom" c $RANDOM_MAJOR $RANDOM_MINOR
			else
				$MKNOD "$CHROOTDIR/dev/urandom" c 1 9
			fi
		fi
	fi

	if [ `uname` = "Linux" -a "x$CHROOTDIR" != "x" -a -f /lib/libgcc_s.so.1 ] ; then
		# We're entering a chroot jail and there's a libgcc_s.so.1 under
		# /lib -- we need to add /lib/libgcc_s.so.1 to LD_PRELOAD so
		# that it will be loaded before entering the chroot jail.
		# Fixes issue where we crash with error:
		#   "libgcc_s.so.1 must be installed for pthread_cancel to work"
		export LD_PRELOAD="/lib/libgcc_s.so.1 $LD_PRELOAD"
	fi

	if [ $target_os -eq $redhat ] ; then
		doEcho -n "Starting $PROGRAMNAME: "
		if [ "x$CHROOTDIR" != "x" ] ; then
			daemon "$LOCATION/$PROGRAM" $PARAMS -t "$CHROOTDIR" < /dev/null
		else
			daemon "$LOCATION/$PROGRAM" $PARAMS < /dev/null
		fi
		RETVAL=$?
		echo
	elif [ $target_os -eq $suse ] ; then
		doEcho -n "Starting $PROGRAMNAME: "
		if [ "x$CHROOTDIR" != "x" ] ; then
			startproc "$LOCATION/$PROGRAM" $PARAMS -t "$CHROOTDIR" < /dev/null
		else
			startproc "$LOCATION/$PROGRAM" $PARAMS < /dev/null
		fi
		rc_status -v
	elif [ $target_os -eq $solaris ] ; then
		doEcho -n "starting $PROGRAMNAME: "
		if [ "x$CHROOTDIR" != "x" ] ; then
			"$LOCATION/$PROGRAM" $PARAMS -t "$CHROOTDIR" < /dev/null >> /dev/null 2>&1
		else
			"$LOCATION/$PROGRAM" $PARAMS < /dev/null >> /dev/null 2>&1
		fi
		RETVAL=$?
		sleep 1
		if isRunning ; then
			doEcho "$PROGRAMNAME running."
		else
			doEcho "$PROGRAMNAME not running."
		fi
	elif [ $target_os -eq $freebsd ] ; then
		if [ "x$CHROOTDIR" != "x" ] ; then
			"$LOCATION/$PROGRAM" $PARAMS -t "$CHROOTDIR" && doEcho -n " $PROGRAMNAME"
		else
			"$LOCATION/$PROGRAM" $PARAMS && doEcho -n " $PROGRAMNAME"
		fi
		RETVAL=0
	elif [ $target_os -eq $macosx -o $target_os -eq $tinycore ] ; then
		doEcho "Starting $PROGRAMNAME"
		if [ "x$CHROOTDIR" != "x" ] ; then
			"$LOCATION/$PROGRAM" $PARAMS -t "$CHROOTDIR" < /dev/null
		else
			"$LOCATION/$PROGRAM" $PARAMS < /dev/null
		fi
		RETVAL=0
	fi
}

# Attempts to stop $PROGRAM
doStop() {
	if isRunning ;	then
		if [ $target_os -eq $redhat ] ; then
			doEcho -n "Shutting down $PROGRAMNAME: "
			killproc "$LOCATION/$PROGRAM"
			RETVAL=$?
			echo
		elif [ $target_os -eq $suse ] ; then
			doEcho -n "Shutting down $PROGRAMNAME: "
			killproc "$LOCATION/$PROGRAM"
			rc_status -v
		elif [ $target_os -eq $solaris ] ; then
			# pkill only matches the first 15 chars of the executable name
			PS_ARG=`echo $PROGRAM | cut -c1-15`
			[ -x /usr/bin/pkill ] && /usr/bin/pkill -x $PS_ARG
			[ -x /usr/bin/pkill ] || kill `ps -ef | grep /$PS_ARG | cut -c10-18`
		elif [ $target_os -eq $freebsd -o $target_os -eq $macosx -o $target_os -eq $tinycore ] ; then
			killall $PROGRAM >> /dev/null 2>&1
			RETVAL=0
		fi
	else
		doEcho "$PROGRAMNAME is not running"
	fi

	if [ -f "$CHROOTDIR/var/run/$PROGRAM.pid" ] ; then
		rm "$CHROOTDIR/var/run/$PROGRAM.pid"
	fi
}

# Prints out the current status of $PROGRAM
doStatus() {
	if [ $target_os -eq $suse ] ; then
		doEcho -n "Checking for $PROGRAMNAME"
		checkproc "$LOCATION/$PROGRAM"
		rc_status -v
	else
		if isRunning ;	then
			doEcho "Running"
		else
			doEcho "Stopped"
		fi
	fi
}

# Attempts to stop $PROGRAM and then start it again
doRestart() {
	doStop
	sleep 1
	doStart
}

# Set to quiet-mode if -q was given as first argument
quiet=0
if [ $# -eq 2 ] ; then
	if [ "$1" = "-q" ] ; then
		quiet=1
		shift
	fi
fi

# Detect OS (and supported distro on Linux)
target_os=0
redhat=1
suse=2
solaris=3
freebsd=4
macosx=5
tinycore=6
case `uname` in
	Linux)
		if [ -f /opt/.filetool.lst ]; then
			target_os=$tinycore
		elif [ -f /etc/rc.d/init.d/functions ] ; then
			target_os=$redhat
		else
			target_os=$suse
		fi
		;;
	SunOS)
		target_os=$solaris
		;;
	FreeBSD)
		target_os=$freebsd;
		;;
	Darwin)
		target_os=$macosx;
		;;
	*)
		target_os=0
esac

if [ $target_os -eq 0 ] ; then
	doEcho "Unknown OS.  Please contact support@bluecatnetworks.com."
	exit 1
fi

if [ $target_os -eq $redhat ] ; then
	# Source function library.
	. /etc/rc.d/init.d/functions
	
	# Source networking configuration.
	. /etc/sysconfig/network
	
	# Check that networking is up.
	[ ${NETWORKING} = "no" ] && exit 0
elif [ $target_os -eq $suse ] ; then
	if [ -r /etc/rc.status ] ; then
		. /etc/rc.status
	fi
	rc_reset
fi

RETVAL=0

if [ ! -f "$LOCATION/$PROGRAM" ] ; then
	doEcho "$PROGRAM is not installed in $LOCATION"
	exit 0
fi

case "$1" in
	start)
		doStart
		;;
	stop)
		doStop
		;;
	status)
		doStatus
		;;
	restart|reload)
		doRestart
		;;
	*)
		doEcho "Usage: $0 {start|stop|status|restart}"
		exit 1
esac

if [ $target_os -eq $suse ] ; then
	rc_exit
fi

exit $RETVAL

