#!/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 object manager"

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

	action is CREATE or REMOVE
	type   is MASTER or SNAPSHOT
	...

Return Values:

	0 Successful execution
	1 Failure

Messages:

        OK:`basename $0` success. Map Oracle database objects created.
        OK:`basename $0` success. Map Oracle database objects removed.

	KO:`basename $0` failure. Map Oracle database objects could not be created.
	KO:`basename $0` failure. Map Oracle database objects could not be removed.
	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 [ $# != 10 ]
    then
	log_KO "Invalid number of parameters."
	EXIT_STATUS=1
    else
	ACTION=$1
	TYPE=$2
	shift 2
# temip_config_path director_user oracle_home 
# oracle_sid database_name
# sqlnet_service_name_master sqlnet_service_name_snapshot
# database_sid
	TCP=$1
	DU=$2
	OH=$3
	OS=$4
	DN=$5
	SNM=$6
	SNS=$7
	DS=$8
	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 
	    CREATE)
		MENUOPT=4
		ACTWORD="created"
	    ;;
	    REMOVE)
		MENUOPT=5
		ACTWORD="removed"
	    ;;
	    esac
	    case $TYPE in
	    MASTER)
	    /usr/mcc/map/bin/temip_map_database_setup <<EOF  >> /tmp/trace.$$ 2>&1
$TCP
$DU
$OH
$OS
Y
1
1
$MENUOPT
$DN
$SNM
$DS
e
EOF
	    ;;
	    SNAPSHOT)
	    /usr/mcc/map/bin/temip_map_database_setup <<EOF  >> /tmp/trace.$$ 2>&1
$TCP
$DU
$OH
$OS
Y
1
2
$MENUOPT
$DN
$SNM
$SNS
$DS
e
EOF
	    ;;
	    esac
	case $? in
	    0) EXIT_STATUS=0 
	    log_OK "Map Oracle database objects $ACTWORD ($TYPE)"
	    ;;
	    1) EXIT_STATUS=1
	    log_KO "Map Oracle database objects could not be $ACTWORD ($TYPE)"
	    cat /tmp/IMPMNT_ORA/temip_map_ora*.log >> /tmp/trace.$$
	    ;;
	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
