#!/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="Install/Uninstall TeMIP MAP Subset"

#-----------------------------------------------------------------
#  This function provides help (manpage-like)
#-----------------------------------------------------------------
help()
  {
    cat << !EOF!
    Installs or uninstalls the TeMIP MAP subset.
    
    
Syntax: 
      
      `basename $0` [-h] [-I "kit location" | -D ] "subset name"
      
Where:
      -h Help
      
      -I This option is for installing "subset name" starting from the "kit location".
	The "subset name" argument defines the name of the subset to install without
	specifying the version number (MAPTEA and not MAPORAV410).
	This mechanism allows the script to be used for every version of the "subset name".
	  
      -D This option is for uninstalling "subset name".
	   

Return Values:

	0 Successful execution
	1 Failure


Messages:

        OK:`basename $0` success. All subsets have been correctly installed.
	OK:`basename $0` success. All subsets have been correctly deleted.

	KO:`basename $0` failure. Some subsets have not been installed.
	KO:`basename $0` failure. Unsupported option.
	KO:`basename $0` failure. You must be root.
	KO:`basename $0` failure. Some subsets have not been deleted.


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

!EOF!
}




#-----------------------------------------------------------------
# Define functions called by the script
#-----------------------------------------------------------------
Install_function ()
  {
    
    #---------------------------------------------------------------
    # Example: private comment which is a hyperlink
    #---------------------------------------------------------------
    log_private_comments "<A HREF="#template_script_1">Installation phase</A>" 
    
    #---------------------------------------------------------------
    # Log private comments in the execution traces
    #---------------------------------------------------------------
    log_private_comments "This is the starting point of the installation"
    
    #---------------------------------------------------------------
    # Example: a private comment which is a 
    # hyperlink target
    #----------------------------------------------------------------
    log_private_comments "<A NAME="template_script_1"></A>"
    
    
    #---------------------------------------------------------------
    # Get the name of the installed subset
    #---------------------------------------------------------------
    KIT_LOCATION=$1
    SUBSET_NAME=$2
    
    #-------------------------------------------------------------------------
    # Execute commands using output redirection to the log file: /tmp/trace.$$
    #-------------------------------------------------------------------------
    # Y   Would you like to run the MAPxxxvnnn IVP? (y/n) [y]

    MAPTEA_SUBSET=`echo $SUBSET_NAME | grep MAPTEA`
    MAPSRV_SUBSET=`echo $SUBSET_NAME | grep MAPSRV`

    if [ ! -z "$MAPTEA_SUBSET" ] ||  [ ! -z "$MAPSRV_SUBSET" ] 
    then
	if [ -f /usr/bin/temip_stop ] 
	then
	  launch_command "/usr/bin/temip_stop"
	fi
    fi

    if [ ! -z "$MAPSRV_SUBSET" ] 
    then
	    /usr/sbin/setld -l $KIT_LOCATION $SUBSET_NAME <<EOF >>  /tmp/trace.$$ 2>&1
 
Y
EOF
    else
    /usr/sbin/setld -l $KIT_LOCATION $SUBSET_NAME <<EOF >>  /tmp/trace.$$ 2>&1
Y
EOF
    fi
    
}




#---------------------------------------------------------------
# This function checks that a list of subsets is installed
# 
# Parameter : 
#    Input
#	$1 The list of subsets
#    Output
#       if error
#         Log the subset not installed using log_private_comments
#         Log an error message using log_KO
#         Set the EXIT_STATUS with 1
#       else
#	  Log a success message using log_OK
#       fi
#    Return
#	0: The subset exists in the kit location
#	1: The subset does not exist
#----------------------------------------------------------------
Check_subset_installation()
  {
    # Init temp file
    REMAINING_SUBSET_FILE=/tmp/remaining_subset_file.$$
    SUBSET_LIST="$1"
    
    for subset in $SUBSET_LIST
    do
      is_installed $subset > /dev/null
      if [ $? = 0 ]
      then
	echo $subset >> $REMAINING_SUBSET_FILE
      fi
    done
    
    #-------------------
    # Display the result
    #-------------------
    log_private_comments ""
    log_private_comments "Check the installed subsets"
    log_private_comments "---------------------------"
    if [ ! -s $REMAINING_SUBSET_FILE ]
    then
      log_private_comments "The following subsets have not been installed:" F
      log_KO "Some subsets have not been installed."
      EXIT_STATUS=1
    else
      #--------------------------------
      # Log a comment using colors
      #           'F' for Failure
      #		  'S' for success
      #		  'W' for Warning
      #		  'T' for simple trace
      #--------------------------------
      
      log_private_comments "All subsets have been correctly installed." S
      launch_command "cat $REMAINING_SUBSET_FILE"
      log_OK "All subsets have been correctly installed."
      EXIT_STATUS=0
    fi
    
    rm -f $REMAINING_SUBSET_FILE
}


#-----------------------------------------------------------------
# Define functions called by the script
#-----------------------------------------------------------------
Uninstall_function ()
  {
    
    #---------------------------------------------------------------
    # Example: private comment which is a hyperlink
    #---------------------------------------------------------------
    log_private_comments "<A HREF="#template_script_2">Uninstallation phase</A>" 
    
    #---------------------------------------------------------------
    # Log private comments in the execution traces
    #---------------------------------------------------------------
    log_private_comments "This is the starting point of the uninstallation"
    
    #---------------------------------------------------------------
    # Example: a private comment which is a 
    # hyperlink target
    #----------------------------------------------------------------
    log_private_comments "<A NAME="template_script_2"></A>"
    
    #---------------------------------------------------------------
    # Check if there are no dependencies
    #---------------------------------------------------------------
    check_subset_dependencies $1
    if [ $? = 0 ]
    then
      log_private_comments "Deleting $1 subset" T
      
      #----------------------------------------------------------------
      # Execute commands using launch_command
      # on variables (the output is automatically 
      # redirected to /tmp/trace.$$)
      #----------------------------------------------------------------
      launch_command "/usr/sbin/setld -d $1"
    else
      log_private_comments "Impossible to remove $1, subset has dependencies." F
      get_subset_dependencies $1 >>  /tmp/trace.$$ 2>&1
    fi # endif dependencies checking
    
}



#--------------------------------------------------------------------
# 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 hI:D $*`

#--------------------------------------------------------------------
# Treat unsupported option
#--------------------------------------------------------------------

if [ "$?" != "0" ]
then
  log_KO "Unsupported option."
  EXIT_STATUS=1
else
  
  
  #-----------------------------------------------------------------
  # Valid options
  #-----------------------------------------------------------------
  while [ $1 != -- ]
  do
    case $1 in
      -h) OPT="HELP";;
      -I) OPT="INSTALL";shift;KIT_LOCATION=$1;;
      -D) OPT="UNINSTALL";;
    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
    #----------------------------------------------------------------
    # Get the argument
    #----------------------------------------------------------------
    SUBSET_NAME=$1
    
    #--------------------------------------------------------------
    # Treat the Help option
    #--------------------------------------------------------------
    
    if [ "$OPT" = "HELP" ]
    then
      log_OK "Help option."
      EXIT_STATUS=0
    else
      
      #-----------------------------------------------------------------
      # Check the number of parameters
      #-----------------------------------------------------------------
      if [ $# != 1 ]
      then
	log_KO "Invalid number of parameters."
	EXIT_STATUS=1
	
      else
	# --------------------------------------------------------
	# Treat the INSTALL option
	# --------------------------------------------------------
	if [ "$OPT" = "INSTALL" ]
	then	
	  #-----------------------------------------------------------------
	  # Get the Full Subset Name
	  #-----------------------------------------------------------------
	  FULL_SUBSET_NAME=`check_if_subset_can_be_installed $KIT_LOCATION $SUBSET_NAME`
	  
	  #-----------------------------------------------------------------
	  # Run the installation
	  #-----------------------------------------------------------------
	  Install_function $KIT_LOCATION $FULL_SUBSET_NAME
	  
	  #-----------------------------------------------------------------
	  # Check the result
	  #-----------------------------------------------------------------
	  Check_subset_installation $FULL_SUBSET_NAME
	  
	  
	  # --------------------------------------------------------
	  # Treat the UNINSTALL option
	  # --------------------------------------------------------
	elif [ "$OPT" = "UNINSTALL" ]
	then
	  #-----------------------------------------------------------------
	  # Get the Full Subset Name
	  #-----------------------------------------------------------------
	  FULL_SUBSET_NAME=`get_installed_subset_name $SUBSET_NAME`
	  
	  #-----------------------------------------------------------------
	  # Run the uninstallation
	  #-----------------------------------------------------------------
	  Uninstall_function $FULL_SUBSET_NAME
	  
	  #-----------------------------------------------------------------
	  # Check the result
	  #-----------------------------------------------------------------
	  # Init temp file
	  REMAINING_SUBSET_FILE=/tmp/remaining_subset_file.$$
	  /usr/sbin/setld -i | grep -e $SUBSET_NAME  | grep -i installed > $REMAINING_SUBSET_FILE
	  
	  #-------------------
	  # Display the result
	  #-------------------
	  log_private_comments "Check uninstalled subsets"
	  log_private_comments "-------------------------"
	  if [ -s $REMAINING_SUBSET_FILE ]
	  then
	    log_private_comments "The following subsets have not been deleted:" F
	    launch_command "cat $REMAINING_SUBSET_FILE"
	    log_KO "Some subsets have not been deleted."
	    EXIT_STATUS=1
	    rm -f $REMAINING_SUBSET_FILE
	  else
	    log_private_comments "All subsets have been correctly deleted." S
	    log_OK "All subsets have been correctly deleted."
	    EXIT_STATUS=0
	  fi # endif  if [ -s $REMAINING_SUBSET_FILE ]
	else
	  log_KO "Unsupported option."
	  EXIT_STATUS=1	  
	fi # endif INSTALLATION option
      fi # endif root id checking
    fi # endif on Help option
  fi # endif number of parameters checking
fi # endif on Unsupported option
  
  
####################################################
####################################################
#
#                   END
#
####################################################
####################################################



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

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




#CHECKSUM-CRC32=2844752057
