#!/bin/bash
#
# Startup script for the SCMS-SM
#
# description: Cisco Service Control Management Suite Subscriber Management Server
# processname: ems


# ~ expansion for /bin/sh 
pcubehome=`echo ~pcube`
export pcubehome

if . $pcubehome/sm.sh; then
   BIN_DIR=$UMROOT/bin/
else
   BIN_DIR=`dirname "$0"`	
   case $BIN_DIR in
    */) ;;
    *)  BIN_DIR=$BIN_DIR/ ;;
   esac
   BIN_FULL_DIR=`pwd`
   case $BIN_FULL_DIR in
    */) ;;
    *)  BIN_FULL_DIR=$BIN_FULL_DIR/ ;;
   esac
   BIN_FULL_DIR=$BIN_FULL_DIR$BIN_DIR
   # UMROOT must be the full path and not relative so ...
   UMROOT=$BIN_FULL_DIR../
fi

# verify UMROOT has a slash at the end
case $UMROOT in
 */) ;;
 *)  UMROOT=$UMROOT/ ;;
esac

. ${UMROOT}bin/clu-common

PROCNAME="SMSERVER"
export PROCNAME
OLD_PROCNAME="umserver"

# support file helper
addToSupportFile() {
    COMMAND="$1"
    echo ">> $COMMAND" > "$BIN_DIR"tmp-sm-support/"$2" 2>/dev/null
    $COMMAND >>  "$BIN_DIR"tmp-sm-support/"$2" 2>/dev/null

    echo -n "."
}

# find the --auto option and set the AUTOMATED_CALL variable accordingly
# this variable is used to decide things based on the knowledge that the 
# script was called by an automatic procedure
case "$2" in
    --auto)
	AUTOMATED_CALL=1;;
    *)
	AUTOMATED_CALL=0;;
esac
export AUTOMATED_CALL

# the wait can be located at the second or third option
case "$2" in
    --wait)
	WAIT_FOR_SM=1;;
    *)
	WAIT_FOR_SM=0;;
esac

case "$3" in
    --wait)
	WAIT_FOR_SM=1;;
    *)
	;;
esac

export WAIT_FOR_SM

wait_for_sm_up () {
    # repeatedly probe the SM during it's bootup until it's
    # responsive.
    # note: if there is a problem, this will loop indefinitely, allowing
    # the operator to fix things in another window.
    
    echo "waiting for SM to come up" | append_to_tmp_file
    echo -n "waiting.." | append_to_tmp_file
    # give the SM some time to start up before polling
    sleep 10
    
    while true; do
        [ `$BIN_DIR/p3sm --sm-status | grep operational | wc -l` -gt 0 ] && {
	    echo "SM is up" | append_to_tmp_file
            return 0
	}
        echo -n "." | append_to_tmp_file
        sleep 2
    done
}

# create temporary file, along the way redirect all relevant mesages to this file, 
# while the script ends write the file to CLU log in order to improve loging messages.
# will be used only in --start, --stop, --restart, --abort and --quit operations
create_tmp_file
echo "p3sm $*" | redirect_to_tmp_file

case "$1" in
  --start)
      # prevent running this script by root
      if [ -w /etc/passwd ]; then 
         echo "This script cannot be run by root" | append_to_tmp_file; 
         write_and_delete_tmp_file_to_clu_log_and_exit 1;
      fi
      
      # check whether is running
      case "`uname`" in
	SunOS) PSARG="-eaf"; ;;
	Linux) PSARG="uaxww"; ;;
	    *) echo unknown architecture | append_to_tmp_file 
	       PSARG="uaxwww" ;;
      esac
      if [ `ps $PSARG | grep "$PROCNAME" | grep -v grep | awk '{print $2}' | wc -l` -gt 0 ]; then
	 echo "SM is already running, please stop it before re-starting" | append_to_tmp_file; 
	 write_and_delete_tmp_file_to_clu_log_and_exit 1
      else 
	 if [ `ps $PSARG | grep "$OLD_PROCNAME" | grep -v grep | awk '{print $2}' | wc -l` -gt 0 ]; then
	    echo "an old version of the SM is running, please remove it from the machine before re-starting" | append_to_tmp_file; 
	    write_and_delete_tmp_file_to_clu_log_and_exit 1
	 else
	    echo "Starting SM "	| append_to_tmp_file;
	    ${BIN_DIR}umserverproc $* &
	    if [ $WAIT_FOR_SM -eq 1 ] ;
		then wait_for_sm_up ;
	    fi   
	 fi
      fi      
      ;;

  --stop)
      ${BIN_DIR}umserverproc $*   
      ;;

  --abort)
      ${BIN_DIR}umserverproc $* &  
      ;;

  --restart)
      # prevent running this script by root
      if [ -w /etc/passwd ]; then 
        echo "This script cannot be run by root" | append_to_tmp_file;
        write_and_delete_tmp_file_to_clu_log_and_exit 1;
      fi
      
      ${BIN_DIR}umserverproc --stop   
      echo "Starting SM " | append_to_tmp_file
      ${BIN_DIR}umserverproc --start &  
      if [ $WAIT_FOR_SM -eq 1 ] ;
	then wait_for_sm_up ;
      fi   
      ;;

  --quit)
      ${BIN_DIR}umserverproc $* &  
      ;;
  --extract-support-file)
      # remove the previous temp dir if exists
      rm -rf "$BIN_DIR"tmp-sm-support/ 2>/dev/null
      # create a temp dir
      mkdir "$BIN_DIR"tmp-sm-support/  2>/dev/null

      echo -n "Support file pre-processing "

      ######### add the info to the support file  #############
      addToSupportFile "ps -eaf"                                a
      addToSupportFile "netstat -a"                             b 
      addToSupportFile "df -kh"                                 c
      addToSupportFile "/sbin/ifconfig -a"                      d
      addToSupportFile "cat /etc/redhat-release"                e
      addToSupportFile "cat /proc/cpuinfo"                      f
      addToSupportFile "cat /proc/meminfo"                      g
      addToSupportFile "/usr/platform/sun4u/sbin/prtdiag -v"    h
      addToSupportFile "ls -l $BIN_DIR../../../var/tt"          i
      addToSupportFile "uname -a"                               j

      ###########################################################

      # concat the files to a text file
      cat "$BIN_DIR"tmp-sm-support/* > "$BIN_DIR"../root/support.txt

      # last progress indication
      echo "."
      # remove the tmp dir
      rm -rf "$BIN_DIR"tmp-sm-support/ 2>/dev/null

      # run regual CLU
      CLU_CMDL=p3sm
      . "$BIN_DIR"clumain.sh
      ;;
  *)
      # run regual CLU
      CLU_CMDL=p3sm
      . "$BIN_DIR"clumain.sh

esac

# write and delete temporary file to CLU log 
write_and_delete_tmp_file_to_clu_log
				      

