#!/bin/sh

# set -x

usage()
{
  echo ""
  echo "Usage: $0 sheperd_pid"; echo "0"	# Return unsigned integer
  exit $return_value
}

PATH=$PATH:/bin:/usr/bin

case $# in
   1) PID=$1 ;;			# pid of the sheperd process
   *) return_value=1; usage ;;
esac


case `uname` in
   AIX*|FreeBSD*|HP-UX*|IRIX*|Linux*|OSF*|SunOS*) :
	   UNIX95="";export UNIX95
	   PGID=`(ps -eo pid,ppid,pgid,time | sort) |
	     awk 'BEGIN {pgid=-1}
	     { if ($2 ~ /^'${PID}'$/) {pgid=$3}	#pgid of exec_dir
	     } END { print int(pgid) }' `	#end of body

	    if [ ${PGID} -gt  0 ]; then
	     echo `(ps -eo pid,ppid,pgid,time | sort) |
	        awk 'BEGIN {found=0; seconds=0}
		{ if ($3 ~ /^'${PGID}'$/) { found++;
		    ptr = index($4,"-")		#Parse days,"dd-", string
		    if (ptr > 0) {
		      tmp_str[1] = substr($4,1,ptr-1)
		      tmp_str[2] = substr($4, (ptr+1), length($4))
		      $4 = tmp_str[1] ":" tmp_str[2]	# replace "-" w/ ":"
		    }
		    n = split($4, x, ":");
		    if (n == 2) { seconds += x[1]*60 + x[2] }
		    if (n == 3) { seconds += x[1]*3600 + x[2]*60 + x[3] }
		    if (n == 4) { seconds += x[1]*86400+x[2]*3600+x[3]*60+x[4] }
		} }			#end of body
		END { print int(seconds) } ' `
	   else
	     echo 0;	# Do not return a signed integer for unsigned integer.
	   fi
	   ;;		# End of AIX, HP-UX, SunOS, IRIX, Linux, FreeBSD, OSF1

        *) echo 0 ;;	# Do not return a signed integer for unsigned integer.
esac

exit 0

