#!/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

. /lib/lsb/init-functions

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..."

		start_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
