#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72D src/bos/usr/lib/nim/methods/c_sm_nim.sh 1.8.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1996,2016 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)80      1.8.1.1  src/bos/usr/lib/nim/methods/c_sm_nim.sh, cmdnim, bos72D, d2016_12A0 3/10/16 11:39:10
#
#   COMPONENT_NAME: cmdnim
#
#   FUNCTIONS: cust
#		chk_installp_flags_smit
#		chk_installp_flags_list
#		inst_bundle
#		update_all
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1996
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

############################################################################
#
#  Name:        chk_installp_flags_smit
#
#  Function:    Performs some basic initial checking of installp flags.
#               Prints an error message suitable for SMIT, which often
#               hides the flag behind a more meaningful description.
#
#  Parameters:  $1 - list of installp flags.
#
#  Returns:      0  if no errors found.
#               -1  if a bad combination of flags was found.
#
#  Note:        This function does not check for all bad combinations of
#               flags; it just checks for the most common.
#
############################################################################
chk_installp_flags_smit()
{
  RC=0

  # Was the "N" (NO SAVE) flag specified without the "c" (COMMIT) flag?
  [[ $1 = *"N"* ]] && [[ $1 != *"c"* ]] && /usr/bin/dspmsg -s 5 cmdinstl_e.cat 3 \
'0503-402 installp:  The NO SAVE (-N) option is invalid without \n\tthe COMMIT (-c) option. \n' && RC=-1

  # Was the "F" (OVERWRITE) flag specified with the "g" (INSTALL REQUISITES)
  # flag?
  [[ $1 = *"F"* ]] && [[ $1 = *"g"* ]] && /usr/bin/dspmsg -s 5 cmdinstl_e.cat 236 \
'0503-496 installp:  The \"AUTOMATICALLY install requisite software\" option \n\tand the \"OVERWRITE same or newer versions\" option from the \n\tprevious menu may not be selected together.\n' && RC=-1

  return $RC
}


############################################################################
#
#  Name:        chk_installp_flags_list
#
#  Function:    Performs some basic initial checking of installp flags.
#               Prints an error suitable for command line lists of
#               flags (instead of lists generated by SMIT).
#
#  Parameters:  $1 - list of installp flags.
#
#  Returns:      0  if no errors found.
#               -1  if a bad combination of flags was found.
#
#  Note:        This function does not check for all bad combinations of
#               flags; it just checks for the most common.
#
############################################################################
chk_installp_flags_list()
{
  RC=0

  # Was the "N" (NO SAVE) flag specified without the "c" (COMMIT) flag?
  [[ $1 = *"N"* ]] && [[ $1 != *"c"* ]] && /usr/bin/dspmsg -s 5 cmdinstl_e.cat 3 \
'0503-402 installp:  The NO SAVE (-N) option is invalid without \n\tthe COMMIT (-c) option. \n' && RC=-1

  # Was the "F" (OVERWRITE) flag specified with the "g" (INSTALL REQUISITES)
  # flag?
  [[ $1 = *"F"* ]] && [[ $1 = *"g"* ]] && /usr/bin/dspmsg -s 5 cmdinstl_e.cat 26 \
'0503-416 installp:  The -%s and -%s flags are mutually exclusive.\n' "F" "g" \
 && RC=-1

  return $RC
}


############################################################################
#
#  Name:        cust
#
#  Function:    Performs a NIM cust operation.
#
#  Parameters:  l - lpp_source name
#               o - fileset names (file created by smit)
#               s - script resource
#               f - installp flags
#
#  Returns:     The result of the cust operation.
#
############################################################################
cust() 
{
  RC=0
  FLAGS=
  INCLUDE_LANGS=
  PROG_NAME="c_sm_nim"
  TMP_LIST="/tmp/_cust_$$"
  while getopts l:o:s:f:GL:u option
  do
    case $option in
      l) LPP_SOURCE=$OPTARG;;
      o) FILESETS=$OPTARG;;
      s) SCRIPT=$OPTARG;;
      f) FLAGS=$FLAGS$OPTARG;;
      G) INCLUDE_LANGS="-G";;
      L) LIVE_UPDATE_DATA=$OPTARG;;
      u) LIVE_UPDATE=yes;;
    esac
  done

  [[ -n ${FLAGS} ]] &&  FLAGS=a${FLAGS}

  #  Check for simple errors in installp flag combinations.
  chk_installp_flags_smit ${FLAGS}
  if [[ $? -ne 0 ]]
  then
    return -1
  fi

  # Create a formatted list of software to install.
  OPFLAGS="installp_cmd ${INCLUDE_LANGS} -f $FILESETS -L -l"
  /usr/sbin/nimclient -o showres -a sm_inst_flags="$OPFLAGS" -a resource=$LPP_SOURCE \
    > $TMP_LIST
  if [[ $? -ne 0 ]]
  then
    /usr/bin/dspmsg -s 1 cmdnim.cat 231 '0042-231 %s:  Unable to generate the formatted list of software to install.\n' ${PROG_NAME}
    /usr/bin/rm -f ${TMP_LIST}
    return -1
  fi

  # Install the software.
  INST_LIST="-f${TMP_LIST}"
  /usr/sbin/nimclient -o cust -a lpp_source=${LPP_SOURCE} \
            -a filesets="${INST_LIST}" \
            ${SCRIPT:+-a script=$SCRIPT} \
            ${FLAGS:+-a installp_flags=-$FLAGS} \
            ${LIVE_UPDATE:+-a live_update=yes} \
            ${LIVE_UPDATE_DATA:+-a live_update_data=$LIVE_UPDATE_DATA}
  RC=$?

  /usr/bin/rm ${TMP_LIST}

  return $RC
}


############################################################################
#
#  Name:        inst_bundle
#
#  Function:    Uses an installp_bundle resource to perform a NIM cust
#               operation.
#
#  Parameters:  l - lpp_source name
#               b - installp_bundle resource
#               s - script resource
#               f - installp flags
#
#  Returns:     The result of the cust operation.
#
############################################################################
inst_bundle()
{
  FLAGS=
  while getopts l:b:s:f:L:u option
  do
    case $option in
      l) LPP_SOURCE=$OPTARG;;
      b) INSTALLP_BUNDLE=$OPTARG;;
      s) SCRIPT=$OPTARG;;
      f) FLAGS=$FLAGS$OPTARG;;
      L) LIVE_UPDATE_DATA=$OPTARG;;
      u) LIVE_UPDATE=yes;;
    esac
  done

  [[ -n ${FLAGS} ]] &&  FLAGS=a${FLAGS}

  #  Check for simple errors in installp flag combinations.
  chk_installp_flags_smit ${FLAGS}
  if [[ $? -ne 0 ]]
  then
    return -1
  fi

  BUNDLE_STRING=`echo $INSTALLP_BUNDLE | \
  /usr/bin/awk '{
         bundles = ""

         for ( i = 1; i <= NF; i++ )
         {
           bundles = bundles " -a installp_bundle=" $i
         }

         print bundles
       }'`

  /usr/sbin/nimclient -o cust -a lpp_source=${LPP_SOURCE} \
             ${BUNDLE_STRING} \
             ${SCRIPT:+-a script=$SCRIPT} \
             ${FLAGS:+-a installp_flags=-$FLAGS} \
             ${LIVE_UPDATE:+-a live_update=yes} \
             ${LIVE_UPDATE_DATA:+-a live_update_data=$LIVE_UPDATE_DATA}

  return $?
}

############################################################################
#
#  Name:        update_all
#
#  Function:    Updates the currently installed software using updates from
#               an lpp_source.
#
#  Parameters:  l - lpp_source name
#               s - script resource
#               f - installp flags
#
#  Returns:     The result of the cust operation.
#
############################################################################
update_all()
{
  FLAGS=
  while getopts l:s:f:L:u option
  do
    case $option in
      l) LPP_SOURCE=$OPTARG;;
      s) SCRIPT=$OPTARG;;
      f) FLAGS=$FLAGS$OPTARG;;
      L) LIVE_UPDATE_DATA=$OPTARG;;
      u) LIVE_UPDATE=yes;;
    esac
  done

  [[ -n ${FLAGS} ]] &&  FLAGS=a${FLAGS}

  #  Check for simple errors in installp flag combinations.
  chk_installp_flags_smit ${FLAGS}
  if [[ $? -ne 0 ]]
  then
    return -1
  fi

  /usr/sbin/nimclient -o cust -a lpp_source=${LPP_SOURCE} \
            -a fixes="update_all" \
            ${SCRIPT:+-a script=$SCRIPT} \
            ${FLAGS:+-a installp_flags=-$FLAGS} \
            ${LIVE_UPDATE:+-a live_update=yes} \
            ${LIVE_UPDATE_DATA:+-a live_update_data=$LIVE_UPDATE_DATA}

  return $?
}


#=============================================================================
# MAIN
#=============================================================================

export PATH=/usr/lpp/bos.sysmgt/nim/methods:$PATH

# Exit if no command line parameters
[ -z "$1" ] && exit 1

FUNCTION=$1
shift

$FUNCTION "$@"

exit $?

