#!/bin/ksh
#
# ident "@(#)master_lib.ksh	1.20 02/10/30 SMI"
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
#

#
# master_lib.ksh
#
# set of functions to be used by the master tasks (utinstall, utpreserve)
#
# NOTE: Fatal function refers to a function which is not defined here and
# must be defined in the master task. This function (CleanupAndExit) provides
# a clean up procedure specific for the master task
#

#
# Fatal()
#
# Description:
#    To deal with fatal errors. This function calls CleanupAndExit which must
#    be defined in the master task
#
# Parameters:
#    (none)
#
# Globals used:
#    G_PROGRAM_ID

Fatal() {
  print -u2 "$G_PROGRAM_ID: fatal, $*"
  CleanupAndExit 2
}

#
# Warning()
#
# Description:
#    To print a warning message
#
# Parameters:
#    (none)
#
# Globals used:
#    G_PROGRAM_ID

Warning() {
  print -u2 "$G_PROGRAM_ID: warning, $*"
}

#
# CheckUidIsZero()
#
# Description:
#    Check that install/preserve tool are run by root users.
#
# Parameters:
#    (none)
#
# Globals used:

CheckUidIsZero() {
  case "$(id)" in
    'uid=0('*) return 0  # uid is zero
               ;;

    *)         Fatal "must be run as uid 0 (root)"
               ;;
  esac
}

#
# CheckMediaDir()
#
# Description:
#    Check that media directory is valid
#
# Parameters:
#    (none)
#
# Globals used:
#    G_MEDIA_DIR

CheckMediaDir() {
   if [[ ! -d ${G_PRODUCT_DIR} || ! -d ${G_MEDIA_DIR}/iu_modules || \
         ! -d ${G_MEDIA_DIR}/support_lib ]]; then
      Fatal "$G_MEDIA_DIR is not a valid directory"
   fi

   return 0
}


#
# CheckAdminFile()
#
# Description:
#    Check admin file to be used by pkgadd
#
# Parameters:
#    (none)
#
# Globals used:
#    G_ADMIN_FILE

CheckAdminFile() {
  [[ -r "$G_ADMIN_FILE" ]] || Fatal "cannot open for read $G_ADMIN_FILE"
  return 0
}

#
# CheckTmpDir()
#
# Description:
#    Check temporary directory exists and set G_TMP_DIR.
#
# Parameters:
#    (none)
#
# Globals used:
#    G_TMP_DIR

CheckTmpDir() {
   
   if [[ -d /var/tmp ]]; then
      G_TMP_DIR=/var/tmp
      return 0
   fi
   if [[ -d /tmp ]]; then
      G_TMP_DIR=/tmp
      return 0
   fi
   if [[ -d /usr/tmp ]]; then
      G_TMP_DIR=/usr/tmp
      return 0
   fi
   Fatal "cannot find a temporary directory"
}

#
# ShowPostInitMessage()
#
# Description:
#    To print out a message from all modules after initialization
#
# Parameters:
#    (none)
#
# Globals used:
#    G_MESSAGE_FILE

PrintPostInitMessage() {

   if [[ -f "$G_MESSAGE_FILE" ]]; then
      echo "\nAbout to carry out the following operations:\n"
      cat $G_MESSAGE_FILE
    
      # This message is usable once... (init procedures may be called more than
      # once so this variable must be cleared)
     
      rm -f $G_MESSAGE_FILE 1> /dev/null 2>&1

      return 0
   fi
   return 1
}

#
# SendEventToModule()
#
# Description:
#    To send one (or more) event(s) to all modules
#
# Parameters:
#    $@ : list of events to be sent to the modules (possible events are: Init,
#         Install, Preserve, Remove, Restore, Exit, Abort)
#
# Globals used:
#    G_MEDIA_DIR

SendEventToModule() {

   typeset ret_no=0
   typeset modules_list
   typeset message
   typeset subtask
   typeset msg

   for message in $@; do

      # use different ordering for install/upgrade and remove operations
      # module name is prefixed by a tag and 2 digit sequence number.
      # This tag (one character) determines the ordering for install/upgrade 
      # and remove. M-modules deals with all events while R-modules determine
      # the order for remove operations and just call their corresponding 
      # M-module.

      if [[ $message = "Remove" ]]; then
         modules_list="${G_MEDIA_DIR}/iu_modules/R[0-9][0-9]*"
      else
         modules_list="${G_MEDIA_DIR}/iu_modules/M[0-9][0-9]*"
      fi

      for subtask in $modules_list; do
          if [[ -x $subtask ]]; then

             #
             # call the module with a specific event
             #
             $subtask $message

             if [[ $? != 0 && $? != 1 ]]; then
                msg="error in the module `basename ${subtask}` \
                     (event ${message})."
                if [[ $message = "Abort" || $message = "Exit" ]]; then

                # in case of Abort/Exit event, we just sent of notification 
                # that something was wrong (if we used Fatal, then the exit 
                # procedure would call again this function to send the Abort 
                # message ending up in a deadlock situation)

                   Warning $msg

                else

                # Otherwise, notify the error and start the cleanup procedure 
                # before exiting

                   Fatal $msg

                fi

             elif [[ $? = 1 ]]; then

                ret_no=1

             fi

          else
             Warning "cannot execute module `basename ${subtask}`"
          fi
      done
   done

   return $ret_no
}

