#!/bin/sh
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# 
# Note: In Solaris start/stop scripts are always executed by /bin/sh!
#
# start/stop script for ???
#
# Usage:
#
#   script_name [start|stop|restart|status|refresh]
#
# Defined return codes:
#
#     0 - everything okay
#     1 - usage message printed and exit
#   100 - start command not defined
#   101 - stop command not defined
#   102 - refresh command not defined
#
#
# Fill all parts of the script marked with '???' with your code
#
#
# History: 
#   17.01.2006 /bs initial release
#
# Author
#   Bernd.Schemmer@gmx.de
#

# ---------------------------------------
# check the user executing this script
#

# ---------------------------------------
# constants
#
  __TRUE=0
  __FALSE=1


# ---------------------------------------
#
# script specific variables
#
#

# APPLICATION_NAME = Name of the application for messages
#
  APPLICATION_NAME="???"


# THIS_USER = user to execute the script ("" -> do not change the user)
#
  THIS_USER=""


# START_COMMAND = start script/binary ( "" -> error )
#
  START_COMMAND=""


# STOP_COMMAND = stop script/binary ( "" -> use kill )
#
  STOP_COMMAND=""


# REFRESH_COMMAND = refresh script/binary ( "" -> use kill -HUP )
#
  REFRESH_COMMAND=""


# PID_FILE = Name of the file with the PID of the running process
#            If PID_FILE is empty, PROCESS_NAME is used for checking if
#            the application is running
#
  PID_FILE=""   


# PROCESS_NAME = name of the process for checking if the application is running
#                This variable is only used if PID_FILE is empty
#
# PROCESS_NAME="${PROCESS_NAME}"
  PROCESS_NAME=""
 

#
# set __USER_BREAK_ALLOWED to __FALSE to suppress user aborts with CTRL-C
#
  __USER_BREAK_ALLOWED=${__TRUE}


#
# set __VERBOSE to __TRUE for debugging (use LogInfo for debugging messages)
#
  __VERBOSE=${__FALSE}

# ---------------------------------------
# restart the script under a different user if necessary
#
  if [ "${THIS_USER}"x != ""x ] ; then
    if [ "`/usr/ucb/whoami`"x != "${THIS_USER}"x ] ; then
      LogInfo "Restarting the script as user \"${THIS_USER}\" ..."

      su - "${THIS_USER}" -c "cd \"`pwd`\" ; $0 $*"
      exit $?
    fi
  fi

# ---------------------------------------
#
# save the parameter
#
  ACTION="$1"
  [ $# -ne 0 ] && shift
  ACTION_PARAMETER="$*"

# ---------------------------------------
#
# predefined variables
#

# return code of the script
  main_RC=0

  __EXIT_VIA_DIE=${__FALSE}


  __THISSCRIPT=` basename $0` 

#
# ??? change the name of the logfile to your need; use __LOGFILE=""
#    to suppress using a logfile at all
#
#  __LOGFILE="/var/tmp/${__THISSCRIPT}.log"
  __LOGFILE=""
  
  __HOSTNAME=` uname -n `

  __OS=` uname -s `

# __OS_VERSION - Operating system version (e.g 5.8)
#
  __OS_VERSION=` uname -r `

  __RUNLEVEL=`who -r | tr -s " " | cut -f8 -d " " `


# ---------------------------------------
#
# define variables here
#
# Note: Do not use leading underscores (__) for variable names -
#       these names are reserved for internal variables
#
 
# ---------------------------------------
#
# sub routines
#

# ---------------------------------------
#
# Usage: automatically called in the sub routine die
#
cleanup() {

  LogInfo "Doing house keeping (ACTION = \"${ACTION}\" ..."

## ??? add code for house keeping here

  if [ "${ACTION}"x = "start"x ] ; then
:
  fi

  if [ "${ACTION}"x = "status"x ] ; then
:
  fi

  if [ "${ACTION}"x = "refresh"x ] ; then
:
  fi

  if [ "${ACTION}"x = "restart"x ] ; then
:
  fi

  if [ "${ACTION}"x = "stop"x ] ; then
:
  fi

}

# ---------------------------------------
# 
# Usage: LogMsg [message]
#
LogMsg() {
  LogMsg_THISMSG="$*"
  echo "${__THISSCRIPT} - ${LogMsg_THISMSG}"
  [ "${__LOGFILE}"x != ""x ] && echo "${__THISSCRIPT} - ${LogMsg_THISMSG}" >>"${__LOGFILE}"
}

# ---------------------------------------
# 
# Usage: LogInfo [message]
#
# Note: LogInfo only prints messages if ${__VERBOSE} is true
#
LogInfo() {
  [ ${__VERBOSE} -eq ${__TRUE} ] && LogMsg "INFO: $*"
}

# ---------------------------------------
#
# Usage: LogWarning [message]
#
LogWarning() {
  LogWarning_THISMSG="${__THISSCRIPT} - WARNING: $*!"
  echo "${LogWarning_THISMSG}" >&2
  [ "${__LOGFILE}"x != ""x ] && echo "${LogWarning_THISMSG}" >>${__LOGFILE}
}


# ---------------------------------------
#
# Usage: LogError [message]
#
# Note: Error messages are written to STDERR
#
LogError() {
  LogError_THISMSG="${__THISSCRIPT} - ERROR: $*!"
  echo "${LogError_THISMSG}" >&2
  [ "${__LOGFILE}"x != ""x ] && echo "${LogError_THISMSG}" >>${__LOGFILE}
}


# ---------------------------------------
#
# Usage: die [returncode] [message]
# 
die() {  
  die_THISRC=$1
  [ $# -ne 0 ] && shift
  die_THISMSG="$*"
  __EXIT_VIA_DIE=${__TRUE}

  cleanup

  if [ $# -ne 0 ] ; then
    if [ "${die_THISRC}"x = "0"x ] ; then
      LogMsg "${die_THISMSG}"
    else
      LogError "${die_THISMSG}"
    fi
  fi

  exit ${die_THISRC}
}


# ---------------------------------------
# GENERAL_SIGNAL_HANDLER
#
# general trap handler 
#
# usage: called automatically (parameter $1 is the signal number)
#
# returns: -
#
GENERAL_SIGNAL_HANDLER() {

  case $1 in 

    1 )
        LogInfo "HUP signal received."
        ;;

    2 )
        if [ ${__USER_BREAK_ALLOWED} -eq ${__TRUE} ] ; then
          die 252 "Script aborted by the user via signal BREAK (CTRL-C)" 
        else
          LogInfo "Break signal (CTRL-C) received and ignored (Break is disabled)."
        fi
        ;;

    3 )
        die 251 "QUIT signal received" 
        ;;

   15 )
        die 253 "Script aborted by the external signal TERM" 
        ;;

   "exit" | 0 )
        if [ "${__EXIT_VIA_DIE}"x != "${__TRUE}"x ] ; then
          LogWarning "You should use the function \"die\" to end the program"
        fi    

        ;;
       
    * ) die 254 "Unknown signal catched: $1 "
        ;;

  esac
}


# ---------------------------------------
# PrintVariables
#
PrintVariables() {
  
  for PrintVariables_CURVAR in __THISSCRIPT __LOGFILE __HOSTNAME __OS __OS_VERSION __RUNLEVEL ; do
    eval "PrintVariables_CURVALUE=\${${PrintVariables_CURVAR}}"
    LogInfo "${PrintVariables_CURVAR} is \"${PrintVariables_CURVALUE}\" "
  done

}



# ---------------------------------------
# sample subroutine
#
mysubroutine() {
  mysubroutine_THISRC=0

  return ${mysubroutine_THISRC}
}

# ---------------------------------------
#
# main code
#

# install trap handler
  trap "GENERAL_SIGNAL_HANDLER  1"  1
  trap "GENERAL_SIGNAL_HANDLER  2"  2
  trap "GENERAL_SIGNAL_HANDLER  3"  3
  trap "GENERAL_SIGNAL_HANDLER 15" 15
  trap "GENERAL_SIGNAL_HANDLER exit" 0

# check the logfile
  if [ "${__LOGFILE}"x != ""x ] ; then
    LogInfo "Checking if we can write to the logfile \"${__LOGFILE}\" ..."
    touch ${__LOGFILE}
    if [ $? -ne 0 ] ; then
      __LOGFILE=""
      LogError "Can not write to the logfile \"${__LOGFILE}\""
    else          
      LogInfo " ... the logfile used is \"${__LOGFILE}\"."
    fi      
  else
    LogInfo "Running without a logfile"
  fi


# check if the process is running
#
  if [ "${PID_FILE}"x != ""x ] ; then
#
# use a PID file if defined
#
    LogInfo "Using the pidfile \"${PID_FILE}\" for checking ..."

    [ -f ${PID_FILE} ]  && PROCESS_PID=` cat ${PID_FILE} `
    [ "${PROCESS_PID}"x != ""x ] && PROCESS_PID=`/usr/bin/ps -p ${PROCESS_PID} -o pid=`
    [ "${PROCESS_PID}"x != ""x ] && PROCESS_RUNNING=${__TRUE} || PROCESS_RUNNING=${__FALSE} 

  elif [ "${PROCESS_NAME}"x != ""x ] ; then
#
# else check for a running process if defined
#
    LogInfo "Using the process name \"${PROCESS_NAME}\" for checking ..."

    PROCESS_PID=` ps -ef | grep "${PROCESS_NAME}" | grep -v grep | awk '{print $2}' `
    [ "${PROCESS_PID}"x != ""x ] && PROCESS_RUNNING=${__TRUE} || PROCESS_RUNNING=${__FALSE}

  else
#
# neither a PID_FILE nor a PROCESS_NAME is defined
#
    LogInfo "Neither a process name nor a pid file is defined for checking."
    PROCESS_RUNNING=${__FALSE}
  fi

#

  LogInfo "Action is \"${ACTION}\""
  LogInfo "Action parameter are \"${ACTION_PARAMETER}\""
  
  case ${ACTION} in

    start ) 
      if [ ${PROCESS_RUNNING} = ${__TRUE} ] ; then
        LogMsg "${APPLICATION_NAME} already running; the PID is ${PROCESS_PID}"
      else	
        if [ "${START_COMMAND}"x != ""x ] ; then
          LogMsg "Starting ${APPLICATION_NAME} ..." 

          LogInfo "Using the start command \"${START_COMMAND}\" ..."
          ${START_COMMAND}
        else
          LogInfo "No start command defined for the application \"${APPLICATION_NAME}\" "
          main_RC=100
        fi
      fi
      ;;

    status )
      if [ ${PROCESS_RUNNING} = ${__TRUE} ] ; then
        LogMsg "${APPLICATION_NAME} is running; the PID is ${PROCESS_PID}"
        main_RC=0
      else	
        LogMsg "${APPLICATION_NAME} is not running"
        main_RC=1
      fi
      ;;
   

    stop )
      if [ ${PROCESS_RUNNING} != ${__TRUE} ] ; then
        LogMsg "${APPLICATION_NAME} is not running"
      else	
        LogMsg "Stopping ${APPLICATION_NAME} ..." 
        if [ "${STOP_COMMAND}"x != ""x ] ; then
          LogInfo "Using the stop command \"${STOP_COMMAND}\" ..."

          ${STOP_COMMAND}
          main_RC=$?

        elif [ "${PROCESS_PID}"x != ""x ] ; then
          LogInfo "Using kill to stop the application ..."

          kill ${PROCESS_PID}
          main_RC=$?
	else
          LogInfo "No stop command defined for the application \"${APPLICATION_NAME}\" "
          main_RC=101
	fi
      fi

      main_RC=0
      ;;


    refresh )
      if [ ${PROCESS_RUNNING} != ${__TRUE} ] ; then
        LogMsg "${APPLICATION_NAME} is not running"
      else
        LogMsg "Refreshing ${APPLICATION_NAME} ..." 
        if [ "${REFRESH_COMMAND}"x != ""x ] ; then
          LogInfo "Using the refresh command \"${REFRESH_COMMAND}\"  ..."

          ${REFRESH_COMMAND}
          main_RC=$?

        elif [ "${PROCESS_PID}"x != ""x ] ; then        
          LogInfo "Using kill -HUP to refresh the application ..."

          kill -HUP ${PROCESS_PID}
        else
          LogInfo "No refresh command defined for the application \"${APPLICATION_NAME}\" "
          main_RC=102
	fi
      fi
      ;;

    restart )
      $0 stop
      $0 start
      ;;

    * ) 
      LogMsg "Usage: ${__THISSCRIPT} [start|stop|restart|status|refresh]"
      main_RC=1
      ;;

  esac

# ---------------------------------------
 
  die ${main_RC}

# ---------------------------------------
