#!/bin/sh

####################################################
#          DO NOT DELETE THIS LINE.
#          THIS IS LIKE AN INCLUDE FILE.
#          USE IT FOR DEVELOPING SCRIPTS.
. /usr/kits/SystemMate/scripts/stm_script_init
#------------------------------------------------------------------
# Set whether this script must be run by root user
# (default true)
#------------------------------------------------------------------
ROOT_MANDATORY=true

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



####################################################
####################################################
#           TO BE MODIFIED BY THE USER
#                   BEGIN
# 
####################################################
####################################################
# GENERAL COMMENTS:
#
#      1) YOU MUST DEFINE A SINGLE EXIT POINT
#
#      2) THE RETURN STATUS MUST BE 0 FOR COMPLETION
#         AND 1 FOR FAILURE (USE THE EXIT_STATUS VARIABLE)
#
#      3) EXECUTE A COMMAND USING launch_command OR
#         REDIRECT COMMAND OUTPUT TO THE FILE /tmp/trace.$$
#
#      4) USE THE COMMON FUNCTIONS TO LOG ERRORS
#         OR MESSAGES:
#
#               log_OK "string"
#            or log_KO "string"   
#                          for setting a status message
#               log_private_comments "string"
#                          for defining a comment in the 
#                          trace returned by a command
#
#------------------------------------------------------------------
# Set the title of the HTML page and the 
# Netscape window
#------------------------------------------------------------------

TITLE="TeMIP MAP Oracle database monitor (MASTER or SNAPSHOT)"

#-----------------------------------------------------------------
#  This function provides help (manpage-like)
#-----------------------------------------------------------------
help()
{
    cat << !EOF!
    Monitor the TeMIP MAP Oracle database (MASTER or SNAPSHOT)
    
    
Syntax: 
      
	`basename $0`   action temip_config_path director_user oracle_home oracle_sid oracle_admin_user
  
Where:

	action is START or STOP
	

Return Values:

	0 Successful execution
	1 Failure

Messages:

        OK:`basename $0` success. Map Oracle database started.
        OK:`basename $0` success. Map Oracle database stopped.

	KO:`basename $0` failure. Map Oracle database could not start.
	KO:`basename $0` failure. Map Oracle database could not stop.
	KO:`basename $0` failure. Missing script /usr/mcc/map/bin/temip_map_database_setup.
	KO:`basename $0` failure. You must be root.
	KO:`basename $0` failure. Invalid number of parameters.

Restriction:
        * You must be super user to run this script

!EOF!
}

#--------------------------------------------------------------------
# Parse command line
# getopt must list all the options supported by 
# the script. The syntax is:
#       option x and arguments required
#       option y without argument required
#       mandatory option is "getopt h"
#
#      set -- `getopt hx:y $*`
#
#

set -- `getopt h $*`


#-----------------------------------------------------------------
# Valid options
#-----------------------------------------------------------------
while [ $1 != -- ]
do
    case $1 in
	-h) OPT="HELP";;
    esac
    shift # next flag
done
shift # skip double dash
  
#------------------------------------------------------
# Check if user is root, if root is mandatory
#------------------------------------------------------
if [ "`id -u`" != 0 ]
then
  log_KO "You must be root."
  EXIT_STATUS=1    
else
  #--------------------------------------------------------------
  # Treat the Help option
  #--------------------------------------------------------------    
  if [ "$OPT" = "HELP" ]
  then
    log_OK "Help option."
    EXIT_STATUS=0
  else      
    #-----------------------------------------------------------------
    # Check the number of parameters
    #-----------------------------------------------------------------
    if [ $# != 6 ]
    then
	log_KO "Invalid number of parameters."
	EXIT_STATUS=1
    else
	ACTION=$1
	shift
	TCP=$1
	DU=$2
	OH=$3
	OS=$4
	OAU=$5
	if [ -f /usr/mcc/map/bin/temip_map_database_setup ]
	then
    #-------------------------------------------------------------------------
    # Execute commands using output redirection to the log file: /tmp/trace.$$
    #-------------------------------------------------------------------------
	    case $ACTION in
	    START)
	    /usr/mcc/map/bin/temip_map_database_setup  <<EOF  >> /tmp/trace.$$ 2>&1
$TCP
$DU
$OH
$OS
Y
3
2
$OAU
e
EOF
		case $? in
		0) EXIT_STATUS=0 
		log_OK "Map Oracle database started"
		;;
		1) EXIT_STATUS=1
		log_KO "Map Oracle database could not start."
		;;
		esac
	    ;;
	    STOP)
	    /usr/mcc/map/bin/temip_map_database_setup <<EOF  >> /tmp/trace.$$ 2>&1
$TCP
$DU
$OH
$OS
Y
3
1
$OAU
e
EOF
		case $? in
		0) EXIT_STATUS=0 
		log_OK "Map Oracle database stopped"
		;;
		1) EXIT_STATUS=1
		log_KO "Map Oracle database could not stop."
		cat /tmp/temip_map_ora*.log >> /tmp/trace.$$
		;;
		esac
	    ;;
	    esac
	else
	    EXIT_STATUS=1
	    log_KO "Missing script /usr/mcc/map/bin/temip_map_database_setup."
	fi # check temip_map_database_setup file exists
    fi # endif number of parameters checking
  fi # endif on Help option
fi # endif root id checking
  
  
####################################################
####################################################
#
#                   END
#
####################################################
####################################################



#---------------------------------------------------
# Concatenate the traces
#---------------------------------------------------
final_log

#---------------------------------------------------
# Return the status
#---------------------------------------------------
exit $EXIT_STATUS




#CHECKSUM-CRC32=2844752057
