#!/bin/sh
#  $Id: stop_cdm.sh 98705 2001-04-04 09:42:57Z julian $
#  Copyright (C) 1999-2000 The Santa Cruz Operation
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Component: script to stop cdm services
#  Author: duncans
#
#  Syntax: stop cdm [--quiet] [--kill]
#
# error codes are
# 0 = the server is not running now (either stopped or wasn't running)
# 1 = failed to stop the running server, try --kill
# 3 = other error
EXIT_SUCCESS=0
EXIT_FAILED=1
EXIT_OTHER=3

if [ -f $TTAMSG/common -a -f $TTAMSG/stop_cdm ]; then
  . $TTAMSG/common
  . $TTAMSG/stop_cdm
else
  echo "Unable to open message file: $TTAMSG/stop_cdm"
  exit 1
fi


case "$trace" in
x) set -x
esac

# cd to the directory containing this script, so that we can use
# relative pathnames
ttacommand=$0
case "$ttacommand" in
/*) cd `dirname $ttacommand`
    ;;
*)  fullpath=`pwd`/$ttacommand
    cd `dirname $fullpath`
esac


# parse the arguments
dokill=false
verbose=true
for o in "$@"; do
  case "$o" in
  --help )
    echo "$lf_usage" 1>&2
    exit $EXIT_OTHER
    ;;
  --kill )
    dokill=true
    ;;
  --quiet )
    verbose=false
    ;;
  esac
done

# Make sure that they are running as root
u="`LANG=C LC_ALL=C id | sed 's/.*uid=\([0-9][0-9]*\).*/\1/'`"
if [ "$u" = "0" ]
then
  : # all is okay
else
  echo "You must be root to run this program"
  exit 15
fi


# check the status
statusscript=$TTADIR/bin/scripts/status_cdm
if [ ! -x "$statusscript" ]; then
  if [ "$verbose" = "true" ]; then echo "$stop_cdm_missing_script$statusscript"; fi
  exit $EXIT_OTHER
fi
  
$statusscript --quiet
case "$?" in
0)
  ;;
1)
  if [ "$verbose" = "true" ]; then echo "$stop_cdm_not_running"; fi
  exit $EXIT_SUCCESS  
  ;;
2)
  if [ "$verbose" = "true" ]; then echo "$stop_cdm_disabled"; fi
  exit $EXIT_SUCCESS  
  ;;
*)
  if [ "$verbose" = "true" ]; then echo "$stop_cdm_status_error$statusscript"; fi
  exit $EXIT_OTHER
  ;;
esac

# the master process id
pidfile=$TTADATADIR/info/cdmserver.pid
masterpid="`cat $pidfile`"

# a list of all the children
childpids=`ps -ef 2>/dev/null | awk '$3=="'"$masterpid"'"{print $2}'`

# are we doing a polite kill?
if [ "$dokill" = "false" ]; then
  # send the signal
  kill $masterpid

  # give it a while to stop
  count=30
  while [ "$count" -gt 0 ]; do
    kill -0 $masterpid 2>/dev/null
    if [ "$?" != "0" ]; then
      # kill any still living children, as they must be rogue
      if [ ! -z "$childpids" ]; then
        kill -9 $childpids 2>/dev/null
      fi
      if [ "$verbose" = "true" ]; then echo "$stop_cdm_success"; fi
      exit $EXIT_SUCCESS
    fi
    count=`expr $count - 1`
    sleep 1
  done

  # failed to kill it after 30 seconds
  if [ "$verbose" = "true" ]; then echo "$stop_cdm_failed"; fi
  exit $EXIT_FAILED
fi

# kill the parent first else it may start more children
kill -9 $masterpid 2>/dev/null

# now the kildren
if [ ! -z "$childpids" ]; then
  kill -9 $childpids 2>/dev/null
fi

# make sure they're all dead
stillliving=""
for i in $childpids $masterpid; do
  # give it a while to stop
  killedit=false
  count=10
  while [ "$count" -gt 0 ]; do
    kill -0 $i 2>/dev/null
    if [ "$?" != "0" ]; then
      killedit=true
      break
    fi
    count=`expr $count - 1`
    sleep 1
  done

  # add this to the list of still living processes
  if [ "$killedit" != "true" ]; then
    stillliiving="$i $stillliiving"
  fi
done

# are there any still lying around
if [ ! -z "$stillliving" ]; then
  if [ "$verbose" = "true" ]; then echo "$stop_cdm_failed_kill$stillliving";fi
  exit $EXIT_OTHER
fi

# remove the pid file
rm -f $pidfile

# worked
exit $EXIT_SUCCESS
