#!/bin/sh
#
# Copyright (c) 2012 Broadcom Corporation
#
# chkconfig: 235 21 80
#
### BEGIN INIT INFO
# Provides: tg3sd
# Required-Start: network
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: TG3SD Daemon
# Description: TG3SD Daemon
### END INIT INFO

LOCKDIR="/var/lock/tg3sd"
LOCKFILE="/var/lock/tg3sd/lock"
TG3SD=/usr/sbin/tg3sd

#
# Logging of succes messages
#
function log_success_msg ()
{
    echo -en "$@"
    echo -e  "$rc_done"
}

#
# Logging of failure messages
#
function log_failure_msg ()
{
    echo -en "$@"
    echo -e  "$rc_failed"
}

#
# Logging of warn messages
#
function log_warning_msg ()
{
    echo -en "$@"
    echo -e  "${stat}${attn} warning${norm}"
}

# Check if any of $pid (could be plural) are running
checkpid() {
	local i

	for i in $* ; do
		[ -d "/proc/$i" ] && return 0
	done
	return 1
}

# __proc_pids {program} [pidfile]
# Set $pid to pids from /var/run* for {program}.  $pid should be declared
# local in the caller.
# Returns LSB exit code for the 'status' action.
__pids_var_run() {
	local base=${1##*/}
	local pid_file=${2:-/var/run/$base.pid}

	pid=
	if [ -f "$pid_file" ] ; then
	        local line p

		[ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"
		while : ; do
			read line
			[ -z "$line" ] && break
			for p in $line ; do
				[ -z "${p//[0-9]/}" ] && [ -d "/proc/$p" ] && pid="$pid $p"
			done
		done < "$pid_file"

	        if [ -n "$pid" ]; then
	                return 0
	        fi
		return 1 # "Program is dead and /var/run pid file exists"
	fi
	return 3 # "Program is not running"
}

# Output PIDs of matching processes, found using pidof
__pids_pidof() {
	pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
		pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
}


# A function to start a program.
daemon() {
	# Test syntax.
	local gotbase= force= nicelevel corelimit
	local pid base= user= nice= bg= pid_file=
	local cgroup=
	nicelevel=0
	while [ "$1" != "${1##[-+]}" ]; do
	  case $1 in
	    '')    echo $"$0: Usage: daemon [+/-nicelevel] {program}"
	           return 1;;
	    --check)
		   base=$2
		   gotbase="yes"
		   shift 2
		   ;;
	    --check=?*)
		   base=${1#--check=}
		   gotbase="yes"
		   shift
		   ;;
	    --user)
		   user=$2
		   shift 2
		   ;;
	    --user=?*)
	           user=${1#--user=}
		   shift
		   ;;
	    --pidfile)
		   pid_file=$2
		   shift 2
		   ;;
	    --pidfile=?*)
		   pid_file=${1#--pidfile=}
		   shift
		   ;;
	    --force)
		   force="force"
		   shift
		   ;;
	    [-+][0-9]*)
		   nice="nice -n $1"
	           shift
		   ;;
	    *)     echo $"$0: Usage: daemon [+/-nicelevel] {program}"
	           return 1;;
	  esac
	done

        # Save basename.
        [ -z "$gotbase" ] && base=${1##*/}

        # See if it's already running. Look *only* at the pid file.
	__pids_var_run "$base" "$pid_file"

	[ -n "$pid" -a -z "$force" ] && return

	# make sure it doesn't core dump anywhere unless requested
	corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"

	# if they set NICELEVEL in /etc/sysconfig/foo, honor it
	[ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"

	# if they set CGROUP_DAEMON in /etc/sysconfig/foo, honor it
	if [ -n "${CGROUP_DAEMON}" ]; then
		if [ ! -x /bin/cgexec ]; then
			echo -n "Cgroups not installed"; warning
			echo
		else
			cgroup="/bin/cgexec";
			for i in $CGROUP_DAEMON; do
				cgroup="$cgroup -g $i";
			done
		fi
	fi

	# Echo daemon
        [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"

	# And start it up.
	if [ -z "$user" ]; then
	   $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
	else
	   $cgroup $nice runuser -s /bin/bash $user -c "$corelimit >/dev/null 2>&1 ; $*"
	fi

	[ "$?" -eq 0 ] && log_success_msg $"$base startup" || log_failure_msg $"$base startup"
}

start()
{
	test -x $TG3SD || {
		log_failure_msg "$TG3SD not installed";
		exit 1;
	}

	if [ -f ${LOCKFILE} ]
	then
		log_warning_msg "tg3sd service already running..."
	else
		log_success_msg "Starting tg3sd service..."

		daemon ${TG3SD}
		mkdir -p ${LOCKDIR}
		touch ${LOCKFILE}
		chmod 400 ${LOCKFILE}
	fi

}

stop()
{
	local force=$1

	if [ -f ${LOCKFILE} ]
	then
		pid=$(pidof "$TG3SD")
		if [ "$force" == "force" ]
		then
			[ "$pid" ] && kill -HUP $pid
		else
			[ "$pid" ] && kill -TERM $pid
		fi

		log_success_msg "Stopping tg3sd service..."

		rm -f ${LOCKFILE}
		rm -rf ${LOCKDIR}
	else
		log_success_msg "tg3sd service not running..."
	fi
}

status()
{
	[ -f ${LOCKFILE} ] || { echo "$TG3SD is stopped" ; return 3; }

	status=0
	echo "$TG3SD is running, pid=`pidof $TG3SD`"

	return $status
}

case "$1" in
	start)
		start
		;;

	stop)
		stop $2
		;;

	restart)
		stop $2
		start
		;;

	status)
		status
		exit $?
		;;
	*)
		echo -n "Usage: $0 {start|stop [force]|status|restart [force]}"
		exit 1
		;;
esac
