#!/bin/sh

###############################################################################
# Licensed Materials - Property of IBM
# 5724-T40 IBM License Metric Tool
# 5725-C68 IBM Tivoli Asset Discovery for Distributed
#
# (C) Copyright IBM Corp. 2002, 2011.  All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
###############################################################################
# tlm                This script starts or stops the Agent
#
# chkconfig: 235 20 01
# description: starts the IBM License Metric Tool, IBM Tivoli Asset Discovery for Distributed Agent


### BEGIN INIT INFO
# Provides:          itlmagent
# Required-Start:    $syslog $network $named +pcmcia
# Required-Stop:     $syslog $network +pcmcia
# Default-Start:     2 3 5
# Default-Stop:      0 1
# Short-Description:
# Description:
### END INIT INFO

PATH=/usr:/usr/bin:/sbin:/usr/sbin:/bin:/var/itlm/; export PATH

[ -f /var/itlm/tlmagent ] || exit 1

case "$*" in
'start')
	 # tlmagent Start Command
	 /var/itlm/tlmagent -g 1>&0 2>&0
	 if [ $? -ne 0 ] ; then
		 exit 1
	 fi
	 ;;

'reload')
	 # tlmagent Reload Command
	 /var/itlm/tlmagent -reload
	 if [ $? -ne 0 ] ; then
		 exit 1
	 fi
	 ;;

'stop')
	 # tlmagent Stop Command
	 /var/itlm/tlmagent -e

	 # If tlmagent was not running exit now
	 if [ $? -eq 6 ] ; then
		 exit 1
	 fi

	 # Wait until process(es) termination
	 i="."
	 while [ $i != "..........." ]
	 do
		 ps -ef | egrep 'tlmagent -g$|tlmagent$' >/dev/null 2>&1
		 if [ $? -eq 0 ] ; then
			 sleep 3
			 i=${i}"."
		 else
			 break
		 fi
	 done

	 # If 30 seconds have elapsed kill process(es) brutally
	 if [ $i = "..........." ] ; then
		 kill -9 `ps -eo comm,pid,ppid | grep tlmagent | tr -s ' ' | cut -d ' ' -f2,3 | grep " 1$" | cut -d ' ' -f1`>/dev/null 2>&1
	 fi
	 ;;

'capture')
	 # tlmagent Capture Command
	 /var/itlm/tlmagent -capture
	 if [ $? -ne 0 ] ; then
		 exit 1
	 fi
	 ;;

*)
	 echo "Usage: $0 { start | stop | reload | capture}"
	 exit 1
	 ;;

esac
exit 0
