#!/bin/sh
#############################################################################
#
# Licensed Materials - Property of IBM
#
# (C) COPYRIGHT International Business Machines Corp. 2011
#
# All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#############################################################################

#
# NAME: db2cptsa
#
# FUNCTION: db2cptsa - Install or update DB2 HA Scripts for IBM Tivoli
#                      System Automation for Multiplatforms (SA MP)
#                      Base Component to work with DB2 HA.
#
########################################################################
# Options for "set" command
setopts="${setopts:-+x}"
set ${setopts?}

TRUE=0                     # True variable
FALSE=1                    # False variable
DEBUG=${DEBUG:-${FALSE?}}
export TRUE FALSE DEBUG
PROGNAME=`basename $0` # Program name
curdir=`/bin/pwd`
if [ "X$DB2CURDIR" != "X" ]; then
   TOPDIR=${DB2CURDIR?}
else
   TOPDIR=${curdir?}
fi
PROGDIR=`dirname $0`
cd ${PROGDIR?}
PROGDIR=`/bin/pwd`
cd ${PROGDIR?}

#-----------------------------------------------------------------------#
#               Start of function definitions
#-----------------------------------------------------------------------#

# To display a message when a function is called
trac ()
{
    if [ ${DEBUG?} -eq ${TRUE?} ]; then
        echo "## call function $@"
    fi
}

# To display a message when exiting a function
trax ()
{
    if [ ${DEBUG?} -eq ${TRUE?} ]; then
        echo "## exit function $1"
    fi
}

# To display the command syntax
syntax ()
{
    display_msg ${DB2CAT?} 98 \
   "DBI1098I Usage: db2cptsa  [-c] [-f] [-r] [-?|-h]"
    exit ${db2cptsa_rc?}
}

# Default initialization for command execution
initialization ()
{
    func_name="initialization"
    trac ${func_name?} $@
    set ${setopts?}

    OPTION_CHK=${FALSE?}
    OPTION_FORCE=${FALSE?}
    OPTION_DELETE=${FALSE?}
    DB2CPTSA_DIR="${PROGDIR?}"
    DB2DIR="${PROGDIR?}/../.."
    HATSASRC="${DB2DIR?}/ha/tsa"
    DB2CHKTSA="${DB2CPTSA_DIR?}/db2chktsa"
    DB2HA_TARGET="/usr/sbin/rsct/sapolicies/db2"
    DB2HA_SPEC="/usr/sbin/rsct/sapolicies/db2/spec"
    DB2HA_SPEC_BK="${DB2HA_SPEC?}_backup"
    db2cptsa_rc=67
    HASCRIPT_LEVEL=${db2cptsa_rc?}

    if [ -f "${DB2CPTSA_DIR?}/../db2ls" -a -d "${DB2CPTSA_DIR?}/../../.metadata" ]; then
       BINDIR="${DB2CPTSA_DIR?}/../../bin"    
       LOCALE_DIR="${DB2CPTSA_DIR?}/../../msg"
       DISP_MSG_DIR="${DB2CPTSA_DIR?}/../../instance"
    else
       BINDIR="${DB2CPTSA_DIR?}/../bin"
       LOCALE_DIR="${DB2CPTSA_DIR?}/locale"
       DISP_MSG_DIR="${BINDIR?}"
    fi
    CMD_DB2LANGDIR=${BINDIR?}/db2langdir
    VRMFIS="9.7.0.4.0"

    PATH=".:"${PATH}
    export PATH
    LIBPATH_ORG=${LIBPATH}
    LIBPATH=.:${LIBPATH}
    export LIBPATH
    LD_LIBRARY_PATH_ORG=${LD_LIBRARY_PATH}
    LD_LIBRARY_PATH=.:${LD_LIBRARY_PATH}
    export LD_LIBRARY_PATH
    SHLIB_PATH_ORG=${SHLIB_PATH}
    SHLIB_PATH=.:${SHLIB_PATH}
    export SHLIB_PATH
 
    LANG_ORG=${LANG}
    export LANG_ORG
    set_lang
    trax  ${func_name?}
}

#----------------------------------------------------------------
# Name       - comp_version
# Function   - Compare the passed two versions
# Parameters - $1 : the version one;
#              $2 : the version two;
#              $3 : the delimiter (one char) used to split the version number.
#                   This is optional. If not given, '.' will be used.
# Returns    - 0  : <version one> equals to <version two>
#              1  : <version one> is smaller than <version two>
#              2  : <version one> is greater than <version two>
#
# NOTE: if two versions do not have the same number of version information,
#       the shorter one will have 0 appended. For instance, 2.2 and 2.2.1.0
#       are passed, the 2.2 will be changed to 2.2.0.0 and then used for
#       comparison.
#----------------------------------------------------------------

comp_version ()
{
   setopts="${setopts:-+x}"
   func_name="comp_version"
   trac ${func_name?} $@
   set ${setopts?}

   comp_version_rc=-1
   comp_version_flip=1

   if [ $# -lt 2 ]
   then
      echo "Usage: comp_version <first version number> <second version number> [<delimiter>] "
      trax ${func_name?}
      exit 67
   fi
   version1=$1
   version2=$2
   splitter=$3
   if [ "X${splitter?}" = "X" ]; then
      splitter="."
   else
      splitter_count=`echo ${splitter?} | wc -c`
      if [ ${splitter_count?} -gt 2 -o ${splitter_count?} -lt 2 ]
      then
         echo "You used the delimiter ${splitter?} with more than one character."
         trax ${func_name?}
         exit 67
      fi
   fi
   if [ `echo ${version1?}  | awk '{print NF}' FS=${splitter?} OFS=${splitter?}` -lt \
        `echo ${version2?}  | awk '{print NF}' FS=${splitter?} OFS=${splitter?}` ]
   then 
      version1_tmp=${version1?}
      version1=${version2}
      version2=${version1_tmp?}
      comp_version_flip=0
      unset version1_tmp
   fi

   while [ -n "${version1?}" -a ${comp_version_rc?} -eq -1 ]
   do
      version1_tmp=`echo ${version1?} | awk '{ print $1 }' FS=${splitter?}`
      version2_tmp=`echo ${version2?} | awk '{ print $1 }' FS=${splitter?}`

      version1=`echo ${version1?} | awk '{ for(i=2;i<=NF;++i) {printf "%s",$i; if (i<NF) { printf "%s", OFS }} print "" }' FS=${splitter?} OFS=${splitter?}`
      version2=`echo ${version2?} | awk '{ for(i=2;i<=NF;++i) {printf "%s",$i; if (i<NF) { printf "%s", OFS }} print "" }' FS=${splitter?} OFS=${splitter?}`

      version1_tmp=`echo ${version1_tmp?} | sed 's/^00*//'`
      version2_tmp=`echo ${version2_tmp?} | sed 's/^00*//'`

      if [ "X${version1_tmp?}" = "X" ]
      then
         version1_tmp=0
      fi
      if [ "X${version2_tmp?}" = "X" ]
      then
         version2_tmp=0
      fi

      version1_tmp_letter=`echo ${version1_tmp?} | sed 's/[0-9]*//g'`
      version1_tmp=`echo ${version1_tmp?} | sed 's/[^0-9]//g'`
      version2_tmp_letter=`echo ${version2_tmp?} | sed 's/[0-9]*//g'`
      version2_tmp=`echo ${version2_tmp?} | sed 's/[^0-9]//g'`

      if [ ${version1_tmp?} -lt ${version2_tmp?} ]
      then
         if [ ${comp_version_flip?} -eq 0 ]; then
            comp_version_rc=2
         else
            comp_version_rc=1
         fi
      elif [ ${version1_tmp?} -gt ${version2_tmp?} ]
      then
         if [ ${comp_version_flip?} -eq 0 ]; then
            comp_version_rc=1
         else
            comp_version_rc=2
         fi
      fi

   done

   if [ ${comp_version_rc?} -eq -1 ]
   then
     comp_version_rc=0
   fi
   trax ${func_name?}
   return ${comp_version_rc?}
}

#----------------------------------------------------------------
# Name       - is_int
# Function   - Checks if a string is an integer
# Parameters - $1 : the string value
# Returns    - 0  : if the string is an integer
#              1  : otherwise.
#----------------------------------------------------------------
is_int ()
{
   setopts="${setopts:-+x}"
   func_name="is_int"
   trac ${func_name?} $@
   set ${setopts?}

   if [ $# -lt 1 ]
   then
      echo "Usage: is_int <string value>"
      trax ${func_name?}
      exit 67
   fi
   var_string=$1
   if [ `expr "${var_string?}" + 0 2> /dev/null` -eq "${var_string?}" 2>/dev/null ]; then
      is_int_rc=0
   else
      is_int_rc=1
   fi
   trax ${func_name?}
   return ${is_int_rc?}
}
#----------------------------------------------------------------
# Name       - validate_version 
# Function   - Check if the version string only contains digits
#
# Parameters - $1 : version string to check
#              $2 : (optional) the delimiter char for the string
# Returns    - 0  : version string only contains delimiter and digits
#              1  : otherwise
#----------------------------------------------------------------
validate_version ()
{
  func_name="validate_version"
  trac ${func_name?} $@
  set ${setopts?}
  TRUE="${TRUE:-0}"
  FALSE="${FALSE:-1}"
  INTERNAL_ERR="${INTERNAL_ERR:-99}"

  if [ $# -lt 1 ]
  then
      echo "Usage: validate_version <version number>"
      trax ${func_name?}
      exit ${INTERNAL_ERR?}
  fi
  val_version=$1
  val_splitter=$2
  var_rc=-1

  if [ "X${val_splitter?}" = "X" ]; then
      val_splitter="."
  else
      val_splitter_count=`echo ${val_splitter?} | wc -c`
      if [ ${val_splitter_count?} -gt 2 -o ${val_splitter_count?} -lt 2 ]
      then
         echo "You used the delimiter ${val_splitter?} with more than one character."
         trax ${func_name?}
         exit ${INTERNAL_ERR?}
      fi
  fi
  while [ -n "${val_version?}" -a ${var_rc?} -eq -1 ]
  do
      val_version_tmp=`echo ${val_version?} | awk '{ print $1 }' FS=${val_splitter?}`
      val_version=`echo ${val_version?} | awk '{ for(i=2;i<=NF;++i) {printf "%s",$i; \
                 if (i<NF) { printf "%s", OFS }} print "" }' FS=${val_splitter?} OFS=${val_splitter?}`
      is_int ${val_version_tmp?}
      if [ $? -ne ${TRUE?} ]; then
         var_rc=${FALSE?}
      fi
  done
  if [ ${var_rc?} -eq -1 ]; then
      var_rc=${TRUE?}
  fi
  trax ${func_name?}
  return ${var_rc?}
}
#----------------------------------------------------------------
# Name       - is_tsa_installed
# Function   - Checks if there exists SA MP Base Component installed
#
# Parameters - none
# Returns    - 0  : The system has SA MP Base Component installed
#              non-zeor  : otherwise
#----------------------------------------------------------------
is_tsa_installed ()
{
   setopts="${setopts:-+x}"
   func_name="is_tsa_installed"
   trac ${func_name?} $@
   set ${setopts?}

   tsa_version=""
   tsa_version_sys=""
   TRUE="${TRUE:-0}"
   FALSE="${FALSE:-1}"
   INTERNAL_ERR="${INTERNAL_ERR:-99}"
   TSAMP_VERSION_SYS="${TSAMP_VERSION_SYS:-""}"

   rpm -q sam 2>/dev/null 1>/dev/null

   if [ $? -eq ${TRUE?} ]; then

      tsa_version_sys=`rpm -q sam | cut -d- -f2`

      if [ $? -ne ${TRUE?} ]; then
         echo "ERROR: Could not find the version string for installed sam or sam.core.rte fileset."
         trax ${func_name?}
         exit ${INTERNAL_ERR?}
      fi

      validate_version ${tsa_version_sys?}
      if [ $? -ne ${TRUE?} ]; then
         echo "ERROR: The version string ${tsa_version_sys?} for sam or sam.core.rte \
fileset contains non-digits besides delimiter."
         trax ${func_name?}
         exit  ${INTERNAL_ERR?}
      else
         TSAMP_VERSION_SYS=${tsa_version_sys?}
         trax ${func_name?}
         return ${TRUE?}
      fi
   else
      trax ${func_name?}
      return ${FALSE?}
   fi
}

# Check if absolute path is given
chk_abs_path ()
{
   set ${setopts?}
   file_path=$1
   echo ${file_path?} | grep '^/' 2>&1 1>/dev/null
   return $?
}

set_lang_internal ()
{
    set ${setopts?}
    bindir="$1"
    msgdir="$2"
    lang="$3"

    CMD_DB2LANGDIR="${bindir?}/db2langdir"

    if [ "X$lang" != "X" ]; then
       LANG_CUSTOM=0
    else
       LANG_CUSTOM=1
    fi
    # Default locale name and locale-specific message directory
    
    LANG=${LANG:-C}
    locname=${LANG?}
    
    if [ ${LANG_CUSTOM?} -eq 0 ]; then
       locname=`${CMD_DB2LANGDIR?} ${lang?} -locale`
       DB2_LANG_JAVA=${locname?}
    fi
    langdir=`${CMD_DB2LANGDIR?} ${locname?}`
    clangdir=`${CMD_DB2LANGDIR?} C`

    if [ -f ${msgdir?}/${langdir?}/db2install.cat ]; then
        DB2CAT="${msgdir?}/${langdir?}/db2install.cat"
        LANGUAGEDIR=${langdir?}
    elif [ -f ${msgdir?}/${clangdir?}/db2install.cat ]; then
        DB2CAT="${msgdir?}/${clangdir?}/db2install.cat"
        LANGUAGEDIR=${clangdir?}
    else
        echo "DBI1055E The message file db2install.cat cannot be found."
        echo
        echo "Explanation:  The message file required by this"
        echo "script is missing from the system; it may have been"
        echo "deleted or the database products may have been loaded"
        echo "incorrectly."
        echo
        echo "User Response:  Verify that the product option containing"
        echo "the message file is installed correctly.  If there are"
        echo "verification errors; reinstall the product option."
        exit 67
    fi

    # check if the locale exists.  If not, we'll set LANG to C for any
    # subprocesses.
    locale -a 2> /dev/null | grep '^'${locname?}'$' > /dev/null
    if [ $? -ne 0 ]
    then
        locname=C
    fi

    # if a language was passed in, set LANG - but if not, leave it alone.
    # (if it isn't a valid language, well, try resetting anyway)
    if [ ${LANG_CUSTOM?} -eq 0 ]
    then
        locale -a 2> /dev/null | grep '^'${LANG:-C}'$' > /dev/null
        if [ $? -eq 0 ]; then
            LANG=${locname?}
        else 
            # if locname is valid, set LANG to locname
            locale -a 2> /dev/null | grep '^'${locname:-C}'$' > /dev/null
            if [ $? -eq 0 ]; then
                LANG=${locname?}
            fi
        fi
    fi

    # Set LANG and NLSPATH variables for use by dspmsg command
    NLSPATH="${msgdir?}/%L/%N:${msgdir?}/${clangdir?}/%N"
    SHORTLANG=`${CMD_DB2LANGDIR?} ${LANG:-C} -short`

    export LANG NLSPATH
}

# Set the HASCRIPT_LEVEL for controlling the program
# flow of db2cptsa
get_hascript_level ()
{
    func_name="get_hascript_level"
    trac ${func_name?} $@
    set ${setopts?}
    get_hascript_level_rc=67

    if [ ! -f ${DB2HA_SPEC?} ]; then
       get_hascript_level_rc=0
    else
       grep 'VRMFIS=' ${DB2HA_SPEC?} 1>/dev/null 2>/dev/null
       if [ $? -eq ${TRUE?} ]; then
          vrmfis_sys=`grep 'VRMFIS=' ${DB2HA_SPEC?} |  awk '{print $2}' FS== OFS==`
          if [ "X${vrmfis_sys?}" != "X" ]; then
             validate_version ${vrmfis_sys?} 
             if [ $? -eq ${TRUE?} ]; then
                comp_version ${vrmfis_sys?} ${VRMFIS?}
                comp_version_rc=$?
                if [ ${comp_version_rc?} -eq 0 ]; then
                   get_hascript_level_rc=2
                elif [ ${comp_version_rc?} -eq 1 ]; then
                   get_hascript_level_rc=1
                elif [ ${comp_version_rc?} -eq 2 ]; then
                   get_hascript_level_rc=3
                   VRMFIS=${vrmfis_sys?}
                fi
             fi
          fi
       else
          get_hascript_level_rc=0
       fi
    fi 
    return ${get_hascript_level_rc?}
    trax ${func_name?}
}

# Reset the related LIB path to the original ones
reset_path ()
{
    func_name="reset_path"
    trac ${func_name?} $@
    set ${setopts?}

    if [ "X$LIBPATH_ORG" = "X" ]; then
      unset LIBPATH
    else
      LIBPATH=${LIBPATH_ORG?}
      export LIBPATH
    fi
    if [ "X$LD_LIBRARY_PATH_ORG" = "X" ]; then
      unset LD_LIBRARY_PATH
    else
      LD_LIBRARY_PATH=${LD_LIBRARY_PATH_ORG?}
      export LD_LIBRARY_PATH
    fi
    if [ "X$SHLIB_PATH_ORG" = "X" ]; then
      unset SHLIB_PATH
    else
      SHLIB_PATH=${SHLIB_PATH_ORG?}
      export SHLIB_PATH
    fi
    trax ${func_name?}
}

#-----------------------------------------------------------------------
# Name       - display_msg
# Function   - Displays a message from the message catalog
# Parameters - $1 - name of the message catalog
#            - $2 - message number
#            - $3 - default message string
#            - $4,$5,$6 - arguments to substitute in msg string, if needed
#-----------------------------------------------------------------------
display_msg()
{
    func_name="display_msg"
    trac ${func_name?} $@

    set ${setopts?}
    unset catname msgid deftmsg msgstr
    catname="$1"
    msgid=$2
    deftmsg="$3"

    shift;shift;shift

    msg_cmd="${DISP_MSG_DIR?}/disp_msg 1 ${msgid} ${catname}"
 
    ${msg_cmd?}  "${deftmsg}" $@ 
    trax ${func_name?}
} 

set_lang ()
{
    func_name="set_lang"
    trac ${func_name?} $@ 
    set ${setopts?}
    set_lang_internal ${BINDIR?} ${LOCALE_DIR?} $1
    trax ${func_name?}
}

# Display the level of DB2 HA Scripts on the system
show_hascript_level ()
{
   func_name="show_hascript_level"
   trac ${func_name?} $@
   set ${setopts?}

   hascript_level=$1
   case ${hascript_level?} in
      0)
        display_msg ${DB2CAT?} 114 \
            "DBI1114W The DB2 HA scripts for the SA MP Base Component \
were not found in /usr/sbin/rsct/sapolicies/db2."
       ;;
      1)  
        display_msg ${DB2CAT?} 116 \
            "DBI1116W The version of the DB2 HA scripts for the SA MP \
Base Component  found in /usr/sbin/rsct/sapolicies/db2 is a lower version \
than the version of the same scripts on the current DB2 install media."
       ;;
      2)
        display_msg ${DB2CAT?} 119 \
            "DBI1119I The version of the DB2 HA scripts for the SA MP \
Base Component found in /usr/sbin/rsct/sapolicies/db2 is the same version \
as the version of the scripts on the current DB2 install media."
       ;;
      3)
        display_msg ${DB2CAT?} 117 \
            "DBI1117I The version of the DB2 HA scripts for the SA MP \
Base Component found in /usr/sbin/rsct/sapolicies/db2 is a higher version \
than the version of the same scripts on the current DB2 install media."
       ;;
   esac
   unset hascript_level
   trax ${func_name?}
}

# Uninstall DB2 HA Scripts
uninstall_hascripts ()
{
   func_name="uninstall_hascripts"
   trac ${func_name?} $@
   set ${setopts?}

   update_flag=$1

   is_tsa_installed
   if [ $? -eq ${TRUE?} -a "${update_flag?}" != "U" ]; then
         display_msg ${DB2CAT?} 127 \
            "DBI1127E The DB2 HA scripts for the SA MP Base Componenta\
found in /usr/sbin/rsct/sapolicies/db2 were not removed because the SA MP \
Base Component was still installed."
         return ${db2cptsa_rc?}
   fi

   ha_rm_rc=${TRUE?}
   if [ -d "${DB2HA_TARGET?}" -a "X${DB2HA_TARGET?}" != "X" ]; then
      ha_files=`grep -v VRMFIS ${DB2HA_SPEC?}`
      for ha_f in ${ha_files}; do
          if [ -f "${DB2HA_TARGET?}/${ha_f?}" ]; then
                 rm -f "${DB2HA_TARGET?}/${ha_f?}"
                 if [ $? -ne ${TRUE?} -a "${update_flag?}" != "U" ]; then
                    ha_rm_rc=${FALSE?}
                    display_msg ${DB2CAT?} 83 \
                      "DBI1083E An attempt to remove %s failed.\n"  "${DB2HA_TARGET?}/${ha_f?}"
                 fi
          fi
      done
      rm -f ${DB2HA_SPEC?}
      if [ $? -ne ${TRUE?} -a "${update_flag?}" != "U" ]; then
         display_msg ${DB2CAT?} 83 \
                      "DBI1083E An attempt to remove %s failed.\n"  "${DB2HA_SPEC?}"
      fi 
   fi
   if [ ${ha_rm_rc?} -eq ${TRUE?} -a "${update_flag?}" != "U" ]; then
      display_msg ${DB2CAT?} 226 \
         "DBI1226I The DB2 HA scripts  for the SA MP Base Component were successfully uninstalled."
   fi
   trax ${func_name?}
   return ${ha_rm_rc?}
}

_restore_spec ()
{
   func_name="_restore_spec"
   trac ${func_name?} $@
   set ${setopts?}
   spec_org=$1
   spec_backup=$2
   spec_restore=$3
   if [ $# -ne 3 ]; then
      echo "ERROR: Usage - $0 <original spec> <backed-up spec> <0|1>(0 for doing restore"
   fi
   if [ ${spec_restore?} -eq ${TRUE?} -a -f ${spec_backup?} ]; then
      mv ${spec_backup?} ${spec_org?} 
   else
      rm -f ${spec_org?}
   fi
   trax  ${func_name?}
}

_install_hascripts ()
{
   func_name="_install_hascripts"
   trac ${func_name?} $@
   set ${setopts?}
   restore_spec=${FALSE?}  
 
   cd ${DB2HA_TARGET?}
   if [ -f "${DB2HA_SPEC?}" ]; then
      mv ${DB2HA_SPEC?} ${DB2HA_SPEC_BK?}
      restore_spec=${TRUE?}
   fi
   echo "VRMFIS=${VRMFIS?}" > ${DB2HA_SPEC?}
   if [ $? -ne ${TRUE?} ]; then
      display_msg ${DB2CAT?} 84 \
         "DBI1084E  An attempt to create %s failed." ${DB2HA_SPEC?}
      _restore_spec ${DB2HA_SPEC?} ${DB2HA_SPEC_BK?} ${restore_spec?} 
      return ${FALSE?}
   fi
   if [ $? -ne ${TRUE?} ]; then
      display_msg ${DB2CAT?} 84 \
         "DBI1084E  An attempt to create %s failed." ${DB2HA_SPEC?}
      _restore_spec ${DB2HA_SPEC?} ${DB2HA_SPEC_BK?} ${restore_spec?}
      return ${FALSE?}
   fi 
   cp -p ${HATSASRC?}/* .
   if [ $? -ne ${TRUE?} ]; then
      display_msg ${DB2CAT?} 86 \
         "DBI1086E  An attempt to copy the file or directory '%s' to '%s' failed." \
         ${HATSASRC?} ${DB2HA_TARGET?}
      _restore_spec ${DB2HA_SPEC?} ${DB2HA_SPEC_BK?} ${restore_spec?}
      return ${FALSE}
   else
   (cd ${DB2HA_TARGET?}; ls | grep -v "spec") >> ${DB2HA_SPEC?}
      return ${TRUE?}
   fi
   trax  ${func_name?}
}

# Install the DB2 HA Scripts
install_hascripts ()
{
   func_name="install_hascripts"
   trac ${func_name?} $@
   set ${setopts?}
   ${DB2CHKTSA?} 2>/dev/null 1>/dev/null
   db2chktsa_rc=$?
   install_hascripts_rc=${TRUE?}
   if [ ${db2chktsa_rc?} -eq 0 -o ${db2chktsa_rc?} -eq 7 ]; then
      display_msg ${DB2CAT?} 134 \
         "DBI1134W The DB2 HA scripts for the SA MP Base Component were not installed \
          because the SA MP Base Component was not installed."
      return ${install_hascripts_rc?}
   fi
   case ${HASCRIPT_LEVEL?} in
     0)
        if [ ! -d ${DB2HA_TARGET?} ]; then
           mkdir -p ${DB2HA_TARGET?}
           if [ $? -ne ${TRUE?} ]; then
              display_msg ${DB2CAT?} 84 \
              "DBI1084E  An attempt to create %s failed." ${DB2HA_TARGET?} 
              return ${FALSE?}
           fi
        fi
        _install_hascripts
        install_hascripts_rc=$?
        if [ ${install_hascripts_rc?} -eq ${TRUE?} ]; then
           display_msg ${DB2CAT?} 99 \
              "DBI1099I  The DB2 HA scripts for the SA MP Base Component were \
               successfully installed in /usr/sbin/rsct/sapolicies/db2."
        fi
        ;;
     1|2|3)
        do_update=${FALSE?}
        if [ ${HASCRIPT_LEVEL?} -eq 1 ]; then
           do_update=${TRUE?} 
        elif [ ${HASCRIPT_LEVEL?} -eq 2 -o ${HASCRIPT_LEVEL?} -eq 3 ]; then
           if [ ${OPTION_FORCE?} -eq ${TRUE?} ]; then
              do_update=${TRUE?}
           elif [ ${HASCRIPT_LEVEL?} -eq 2 ]; then
                display_msg ${DB2CAT?} 119 \
                "DBI1119I The version of the DB2 HA scripts for the SA MP \
Base Component found in /usr/sbin/rsct/sapolicies/db2 is the same version \
as the version of the scripts on the current DB2 install media."
           elif [ ${HASCRIPT_LEVEL?} -eq 3 ]; then
               do_update=${TRUE?}
           fi
        fi
        if [ ${do_update?} -eq ${TRUE?} ]; then
          _install_hascripts
          install_hascripts_rc=$?
          if [ ${install_hascripts_rc?} -eq ${TRUE?} ]; then
             display_msg ${DB2CAT?} 110 \
               "DBI1110I The DB2 HA scripts  for the SA MP Base Component  \
                  were successfully updated in /usr/sbin/rsct/sapolicies/db2."
               rm -rf ${DB2HA_SPEC_BK?}
          fi
        fi
        ;;
   esac
   return ${install_hascripts_rc?}
   trax ${func_name?}
}

#-----------------------------------------------------------------------#
#                End of function definitions
#-----------------------------------------------------------------------#

#-----------------------------------------------------------------------#
#                             Main program
#-----------------------------------------------------------------------#

set ${setopts?}

initialization # Set up environment for DB2 Installer

# Process command-line options
while getopts :dcfr optchar; do
            case ${optchar?} in
                c)  # Check the DB2 HA scripts on the system.
                    OPTION_CHK=${TRUE?}
                    ;;
                f)  # 
                    OPTION_FORCE=${TRUE?}
                    ;;
                r)  #
                    OPTION_DELETE=${TRUE?}
                    ;;
                d)  # debug mode used for IBM Support
                    DEBUG=${TRUE?}
                    setopts="-x"
                    ;;
                h|?)  # Display help
                    syntax 
                    ;;
            esac
done
shift `expr $OPTIND - 1` # Detele all option args

set ${setopts?}

if [ ${OPTION_CHK?} -eq ${TRUE?} -a ${OPTION_DELETE?} -eq ${TRUE?} ]; then
   syntax
fi

get_hascript_level
HASCRIPT_LEVEL=$?

if [ ${OPTION_CHK?} -eq ${TRUE?} ]; then
   show_hascript_level ${HASCRIPT_LEVEL?}
   exit ${HASCRIPT_LEVEL?}
fi

if [ ${OPTION_DELETE?} -eq ${TRUE?} -a ${HASCRIPT_LEVEL?} -eq ${TRUE?} ]; then
      display_msg ${DB2CAT?} 114 \
            "DBI1114W The DB2 HA scripts for the SA MP Base Component \
were not found in /usr/sbin/rsct/sapolicies/db2."
      exit ${db2cptsa_rc?}
fi

if [ ${OPTION_DELETE?} -eq ${TRUE?} ]; then
   uninstall_hascripts
   exit $?
fi

if [ ${OPTION_CHK?} -ne ${TRUE?} -a ${OPTION_DELETE?} -ne ${TRUE?} ]; then
   install_hascripts
   exit $?
fi

