#!/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 Servers Configuration"

#-----------------------------------------------------------------
#  This function provides help (manpage-like)
#-----------------------------------------------------------------
help()
  {
    cat << !EOF!
    Configure the TeMIP MAP Servers Configuration.
    
    
Syntax: 
      
      `basename $0` Oracle_Host Default_Format
      
Where:

       Oracle_Host 
        Hostname of the Oracle database server.
	Can be 'none'.

       Default_Format
        Default Map format used by the servers
	Format allowed are 'Oracle' or 'Map_File'
	If Oracle_Host is 'none', Map_File will be used.
	

Return Values:

	0 Successful execution
	1 Failure


Messages:

        OK:`basename $0` success. Map Servers configured.

	KO:`basename $0` failure. Failed directive SHOW when retreiving the available Map Formats
	KO:`basename $0` failure. Could not start TeMIP
	KO:`basename $0` failure. You must be root
	KO:`basename $0` failure. Map Format not provided after -f option
	KO:`basename $0` failure. File .temip_config not found in /var/mcc/config
	KO:`basename $0` failure. Invalid number of parameters.


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

!EOF!
}




#-----------------------------------------------------------------
# Define functions called by the script
#-----------------------------------------------------------------
MapSetup()
  {
    
    #---------------------------------------------------------------
    # Get the parameters
    #---------------------------------------------------------------
    ORACLE_HOST=$1
    DEFAULT_FORMAT=$2
    
    ORACLE_HOST=`echo $ORACLE_HOST | tr "[A-Z]" "[a-z]"`

    #-------------------------------------------------------------------------
    # Execute commands using output redirection to the log file: /tmp/trace.$$
    #-------------------------------------------------------------------------

    if [ -f /usr/mcc/map/bin/temip_map_setup ]
    then
	if [ "$ORACLE_HOST" = "none" ]
	then
	    /usr/mcc/map/bin/temip_map_setup -s <<EOF  >> /tmp/trace.$$ 2>&1
Y
EOF
	else
	    if [ -z "$DEFAULT_FORMAT" ] 
	    then
		/usr/mcc/map/bin/temip_map_setup -s $ORACLE_HOST  <<EOF  >> /tmp/trace.$$ 2>&1
Y
EOF
	    else
		/usr/mcc/map/bin/temip_map_setup -s $ORACLE_HOST -f $DEFAULT_FORMAT <<EOF  >> /tmp/trace.$$ 2>&1
Y
EOF
	    fi
	fi
	case $? in
	0) EXIT_STATUS=0 
	log_OK "Map Servers configured"
	;;
	3) EXIT_STATUS=1
	log_KO "Failed directive SHOW when retreiving the available Map Formats"
	;;
	4) EXIT_STATUS=1
	log_KO "Could not start TeMIP"
	;;
	5) EXIT_STATUS=1
	log_KO "You must be root"
	;;
	6) EXIT_STATUS=1
	log_KO "Map Format not provided after -f option"
	;;
	7) EXIT_STATUS=1
	log_KO "File .temip_config not found"
	esac
    else
	EXIT_STATUS=1
	log_KO "Could not find file /usr/mcc/map/bin/temip_map_setup"
    fi

}



#--------------------------------------------------------------------
# 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 [ $# != 2 ]
    then
	log_KO "Invalid number of parameters."
	EXIT_STATUS=1
    else
	MapSetup $1 $2
    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
