#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72Q src/bos/usr/lib/nim/methods/c_showres.sh 1.25.1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1995,2019 
# 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 
# @(#)69 1.25.1.3 src/bos/usr/lib/nim/methods/c_showres.sh, cmdnim, bos72Q, q2019_31A8 7/27/19 17:53:20

#
#   COMPONENT_NAME: cmdnim
#
#   FUNCTIONS: prep_catfile
#		prep_cd
#		prep_dir
#		prep_lpp_source
#		prep_tape
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1995
#   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.
#

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

#---------------------------- local defines     --------------------------------

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="location"
OPTIONAL_ATTRS="reference installp_flags sm_inst_flags instfix_flags \
                script bosinst_data image_data resolv_conf installp_bundle \
                fix_bundle lpp_source filesets spot lslpp_flags \
                exclude_files fb_script mksysb lsmksysb_flags savevg vg_data \
                devexports secattrs wpar_spec savewpar \
                lssavevg_flags lssavewpar_flags adapter_def client \
		live_update_data certificate"
typeset -i version=0
installp_flags=""
sm_inst_flags=""
instfix_flags=""
lsmksysb_flags=""
catfile=""
script=""
bosinst_data=""
image_data=""
resolv_conf=""
installp_bundle=""
fix_bundle=""
lpp_source=""
filesets=""
mksysb=""
mount_dir=""
geninstall_cmd=""
fb_script=""
savevg=""
vg_data=""
lssavevg_flags=""
lssavewpar_flags=""
adapter_def=""
client=""
live_update_data=""
certificate=""

#---------------------------- prep_catfile    --------------------------------
#
# NAME: prep_catfile
#
# FUNCTION:
#   prepares environment to use a script file
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#   calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#   parameters:
#   global:
#                script = pathname of script file
#                script_access   = local access point for the script file
#
# RETURNS: (int)
#               0 = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function prep_catfile {

  [[ -z "${catfile}" ]] && return 0

  catfile_access=${TMPDIR}/catfile

  # local or remote file?
  if [[ ${catfile} = ?*:/* ]]
  then

    # remote - setup local access
    nim_mount ${catfile}

    # copy the script into /tmp so the command will have access to it
    ${CP} ${access_pnt} ${catfile_access} 2>${ERR} || \
    err_from_cmd "${CP} ${access_pnt} ${catfile_access}"

    # unmount
    nim_unmount ${access_pnt}

  else

    # already local - just copy it
    ${CP} ${catfile} ${catfile_access} 2>${ERR} || \
    err_from_cmd "${CP} ${catfile} ${catfile_access}"

  fi

} # end of prep_catfile


#---------------------------- prep_tape         --------------------------------
#
# NAME: prep_tape  
#
# FUNCTION:
#    prepares the specified tape device for use
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#    calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#    parameters:
#    global:
#      lpp_source      = source device name
#      src_access      = local access point for the source device
#
# RETURNS: (int)
#    0              = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function prep_tape {

  # tape needs a "no-rewind-on-close" extension 
  # was one provided?
  if [[ ${lpp_source} = /dev/rmt[0-9].* ]]
  then

    [[ ${lpp_source} != /dev/rmt[0-9].1 ]] && \
      [[ ${lpp_source} != /dev/rmt[0-9].5 ]] && \
      error ${ERR_SOURCE} ${lpp_source}

    src_access=${lpp_source}

  else

    # append the extension
    src_access="${lpp_source}.5"

  fi

  # rewind the tape
  ${TCTL} -f ${src_access} rewind 2>${ERR} || \
    err_from_cmd "${TCTL} -f ${src_access} rewind"

} # end of prep_tape

#---------------------------- prep_cd           --------------------------------
#
# NAME: prep_cd
#
# FUNCTION:
#    prepares a CDROM device as the source for a SPOT
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#    calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#    parameters:
#      1            = local mount point
#    global:
#      lpp_source      = source device name
#      src_access      = local access point for the source device
#      access_pnt      = local access point returned from nim_mount
#
# RETURNS: (int)
#    0              = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function prep_cd {

  typeset local_mntpnt=${1}

  # mount the CDROM at a specific offset
  nim_mount ${lpp_source} ${local_mntpnt}

  # images are actually at another offset
  [[ ! -d "${access_pnt}${OFFSET_FOR_CDROM}" ]] && \
		OFFSET_FOR_CDROM=${OFFSET_FOR_CDROM_OLD}
  src_access=${access_pnt}${OFFSET_FOR_CDROM}

} # end of prep_cd

#---------------------------- prep_dir         --------------------------------
#
# NAME: prep_dir       
#
# FUNCTION:
#    prepares environment to use a directory as the source for installp images
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#    calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#    parameters:
#      1            = local mount point
#    global:
#      src_access      = local access point for the source device
#      access_pnt      = local access point returned from nim_mount
#
# RETURNS: (int)
#    0              = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function prep_dir {

  # mount images at a specific offset
  nim_mount ${lpp_source} ${1}
  src_access=${access_pnt}

} # end of prep_dir

#---------------------------- prep_lpp_source       ----------------------------
#
# NAME: prep_lpp_source
#
# FUNCTION:
#    prepares the specified LPP source device for use
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#    calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#    parameters:
#    global:
#      lpp_source      = source device name
#
# RETURNS: (int)
#    0              = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function prep_lpp_source {

  # what kind of source?
  case ${lpp_source} in

    /dev/*)  # tape or CDROM?
          if [[ ${lpp_source} = /dev/rmt[0-9]* ]]
          then
            prep_tape
          elif [[ ${lpp_source} = /dev/cd[0-9]* ]]
          then
            prep_cd
          else
            error ${ERR_SOURCE} ${lpp_source}
          fi
          ;;

    */*)    # source is a directory
          # prep_dir
          if [[ ${lpp_source} != ?*:?* ]] 
          then
            mount_dir="${lpp_source}"
          else
            mount_dir=`echo "${lpp_source}" | ${AWK} 'BEGIN { FS=":"} {print $2}'`
          fi

          #prep_dir "${mount_dir}"

          #If mounting on a spot, then need to prep_dir to where
          #the new root will be set up by setup_chroot.  Otherwise
          #mount it in the regular location
          if [[ ${location} != /usr ]]
          then
            #prep_dir "${location}/${INST_ROOT}${mount_dir}"
            prep_dir "${location}/${INST_ROOT}/mount$$"
          else
            #prep_dir "${mount_dir}"
            prep_dir 
          fi

          ;;

    *)      # unknown type
          error ${ERR_SYS} "unknown lpp_source type - \"${lpp_source}\""
          ;;
  esac

} # end of prep_lpp_source

#*---------------------------- c_showres           ----------------------------
#
# NAME: c_showres
#
# FUNCTION:
#    sets up remote access to resources, then runs a command to show
#    information about the resource
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#    parameters:
#    global:
#
# RETURNS: (int)
#    0              = no errors
#    >0              = failure
#
# OUTPUT:
#-----------------------------------------------------------------------------*/

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
typeset c=""

# set parameters from command line
while getopts :a:qv c
do
  case ${c} in

    a)    # validate the attr ass
        parse_attr_ass "${OPTARG}"

        # include the assignment for use in this environment
        eval ${variable}=\"${value}\"
    ;;

    q)    # show attr info
        cmd_what
        exit 0
    ;;

    v)    # verbose mode (for debugging)
        set -x
        for i in $(typeset +f)
        do
          typeset -ft $i
        done
    ;;

    \?)   # unknown option
        error ${ERR_BAD_OPT} ${OPTARG}
    ;;

  esac
done

# setup the chroot environment
chroot=""
if [[ $location != /usr ]]
then
  setup_chroot_env

  # Now ensure that the server's libraries mounted into the SPOT 
  # will be used.
  ${SET_CHROOT_LIBPATH}
fi

# The operation to be performed is defined by the flags that are set.
# If none of these flags are set, then default operations will be
# determined and used.
if [[ -n ${sm_inst_flags} ]]
then
  show_command="sm_inst"
elif [[ -n ${instfix_flags} ]]
then
  if [[ -n "${spot}" ]]
  then
    error ${ERR_BAD_RESOURCE_TYPE} "spot"
  else
    show_command="instfix"
  fi
elif [[ -n ${lslpp_flags} && -n ${lpp_source} ]]
then
  error ${ERR_SOURCE_ATTR_CONFLICT} "lslpp_flags" "showres" "lpp_source"
elif [[ -n ${lslpp_flags} ]]
then
  show_command="lslpp"
else
  show_command="installp"
fi

# use an lpp_source?
if [[ -n "${lpp_source}" ]]
then
  # setup local access
  prep_lpp_source

  # If using a reference machine/spot for fileset comparison, and no flags
  # are set, then setup the default sm_inst operation.
  if [[ -n ${reference} ]]
  then
    if [[ -z ${sm_inst_flags} && -z ${installp_flags} && -z ${instfix_flags} ]]
    then
      show_command="sm_inst"
    fi
  fi
fi

# Use a spot?  If so, then the operation would have to be "installp" or
# "lslpp".  If installp_flags are not set, then default the operation to
# lslpp.
if [[ -n "${spot}" ]]
then
  if [[ -z ${installp_flags} ]]
  then
    show_command="lslpp"
  fi
fi

if [[ -n "${mksysb}" ]] 
then
  show_command="lsmksysb"
fi

if [[ -n "${savevg}" ]]
then
  show_command="lssavevg"
fi

if [[ -n "${savewpar}" ]]
then
  show_command="lssavewpar"
fi

if [[ -n "${certificate}" ]]
then
  show_command="certview"
fi

if [[ -n "${script}" ]]                   # use a script ?
then
  catfile=${script}
elif [[ -n "${bosinst_data}" ]]           # use a bosinst_data file ?
then
  catfile=${bosinst_data}
elif [[ -n "${image_data}" ]]             # use a image_data file ?
then
  catfile=${image_data}
elif [[ -n "${resolv_conf}" ]]            # use a resolv_conf file ?
then
  catfile=${resolv_conf}
elif [[ -n "${installp_bundle}" ]]        # use an installp_bundle ?
then
  catfile=${installp_bundle}
elif [[ -n "${fix_bundle}" ]]             # use a fix_bundle ?
then
  catfile=${fix_bundle}
elif [[ -n "${exclude_files}" ]]          # use an exclude_files resource ?
then
  catfile=${exclude_files}
elif [[ -n "${fb_script}" ]]              # use an fb_script ?
then
  catfile=${fb_script}
elif [[ -n "${vg_data}" ]]                # use an vg_data ?
then
  catfile=${vg_data}
elif [[ -n "${devexports}" ]]             # use a devexports ?
then
  catfile=${devexports}
elif [[ -n "${secattrs}" ]]               # use a secattrs ?
then
  catfile=${secattrs}
elif [[ -n "${wpar_spec}" ]]              # use a wpar_spec ?
then
  catfile=${wpar_spec}
elif [[ -n "${adapter_def}" ]]            # use an adapter_def ?
then
  # if a client was specified, print only that client's file
  if [[ -n "${client}" ]]; then
    catfile="${adapter_def}/${client}.adapters"
  else
    show_command="lsadapters"
  fi
elif [[ -n "${live_update_data}" ]]       # use a live_update_data ?
then
  catfile=${live_update_data}
fi

if [[ -n ${catfile} ]]
then
  # setup local access
  prep_catfile

  show_command="cat"
fi


# invoke the command

case ${show_command} in

  cat)  # cat something out
    ${CAT} ${catfile_access} 2>${ERR} || err_from_cmd ${CAT}
  ;;

  certview)

    # Print the SSL certificate information
    OPENSSL="/usr/bin/openssl"
    CERTVIEW="${OPENSSL} x509 -text -noout -in"

    # mount the certificate resouce
    nim_mount ${certificate}

    # run the command.
    ${CERTVIEW} "${access_pnt}/server.pem" 2>${ERR} || err_from_cmd ${OPENSSL}

    # unmount the mksysb resouce
    nim_unmount ${certificate}
  ;;

  sm_inst)  #  run sm_inst on the root, using the lpp_source

    # If sm_inst_flags are not specified, then use the default value for
    # listing software on the media.
    if [[ -z ${sm_inst_flags} ]]
    then
      sm_inst_flags="list_filesets -f all"
    fi

    # Check to see if the -d flag needs to be added for the sm_inst
    #   command.
    if [[ ( ${sm_inst_flags} != *"instfix_list"* ) && \
          ( ${sm_inst_flags} != *"list_packages"* )&& \
          ( ${sm_inst_flags} != *"list_filesets"* ) ]]
    then
      sm_inst_flags="${sm_inst_flags} -d"
    fi

    if [[ ${sm_inst_flags} = *"list_filesets"* ]]
    then
      #  The list_filesets operation of sm_inst expects the list of
      #  products to be the last parameter specified.  Therefore, use
      #  the filesets attribute to contain the list since it can't be
      #  pulled out separately from the sm_inst_flags attribute.
      ${chroot} ${SM_INST} ${sm_inst_flags} -d ${src_access} -p ${filesets} \
           2>${ERR} || err_from_cmd ${SM_INST}
    else
      ${chroot} ${SM_INST} ${sm_inst_flags} ${src_access} 2>${ERR} || \
          err_from_cmd ${SM_INST}
    fi

  ;;

  instfix)

    # if the dash is missing from the list of flags, then add it now.
    if [[ ${instfix_flags} != "-"* ]]
    then
      instfix_flags="-${instfix_flags}"
    fi

    # run the command
    ${chroot} ${INSTFIX} ${instfix_flags} ${filesets} -d ${src_access} \
          2>${ERR} || err_from_cmd ${INSTFIX}

  ;;

  installp)

    # if installp_flags were not specified, then use the default to just
    # list the installable software on the media.
    if [[ -z ${installp_flags} ]]
    then
	  use_installp_cmd="on"
      installp_flags="l"
	elif [[ ${installp_flags} = *l* ]]
	then
	  use_installp_cmd="on"
    fi

    # if the dash is missing from the list of flags, then add it now.
    if [[ ${installp_flags} != "-"* ]]
    then
      installp_flags="-${installp_flags}"
    fi

    # make sure that only list flags are passed.
    echo ${installp_flags} 2>/dev/null | ${GREP} -v "[-,l,d,q,i,s]" > /dev/null

    if [[ $? -eq 0 ]]
    then
      error ${ERR_COMMAND} "The showres command only accepts installp flags for list and show operations."
    fi

    # if src_access is defined, then make sure it has a -d flag for the
    # installp command.
    if ( [[ -n ${src_access} ]] && [[ ${src_access} != "-d"* ]] )
    then
      src_access="-d ${src_access}"
    fi

   # New versions ( > 4.x.x.x ) of bos.sysmgt use the GENINSTALL
   # command as opposed to INSTALLP
   # Get the version of the bos.rte.install fileset in
   # the SPOT
   lslpp_fileset=`${chroot} ${LSLPP} -lqcOu bos.rte.install` || \
	  err_from_cmd "${LSLPP} -lqcOu ${NIM_CLIENT_PACKAGE}"

   vrmf=`echo $lslpp_fileset | ${CUT} -d":" -f3`
   version=`echo ${vrmf} | ${CUT} -d"." -f1`


   # geninstall variable may (not) have a value, see c_sh_lib
   # GENINSTALL is pulled from 5.0 release
   (( ${version} > 4 )) && [[ -z use_installp_cmd ]] && geninstall_cmd="${GENINSTALL}"

    # run the command.
    ${chroot} ${geninstall_cmd:-$INSTALLP $installp_flags} \
		${geninstall_cmd:+ -I "$installp_flags"} \
		${src_access} ${filesets} 2>${ERR} ||  err_from_cmd ${INSTALLP}
  ;;

  lslpp)

    # if no lslpp_flags were specified, then use the default to list the
    # installed software.
    if [[ -z ${lslpp_flags} ]]
    then
      lslpp_flags="-L"
    fi

    # if the lslpp_flags variable is missing a dash, then add it now.
    if [[ ${lslpp_flags} != "-"* ]]
    then
      lslpp_flags="-${lslpp_flags}"
    fi

    # run the command.
    ${chroot} ${LSLPP} ${lslpp_flags} 2>${ERR} || err_from_cmd ${LSLPP}

  ;;

  lsmksysb)

    # if no lsmksysb_flags were specified, then use the default to list
    # the software installed in the mksysb image.
    if [[ -z ${lsmksysb_flags} ]]
    then
      lsmksysb_flags="-L"
    fi

    # if the lsmksysb_flags variable is missing a dash, add it now.
    if [[ ${lsmksysb_flags} != "-"* ]]
    then
      lsmksysb_flags="-${lsmksysb_flags}"
    fi

    # mount the mksysb resouce
    nim_mount ${mksysb}

    # run the command.
    ${LSMKSYSB} ${lsmksysb_flags} -f ${access_pnt} 2>${ERR} || err_from_cmd ${LSMKSYSB}

    # unmount the mksysb resouce
    nim_unmount ${mksysb}
  ;;

  lssavevg)
    # if no lssavevg_flags were specified, then use the default to list
    # the software installed in the savevg image.
    if [[ -z ${lssavevg_flags} ]]
    then
      lssavevg_flags="-l"
    fi

    # if the lssavevg_flags variable is missing a dash, add it now.
    if [[ ${lssavevg_flags} != "-"* ]]
    then
      lssavevg_flags="-${lssavevg_flags}"
    fi

    # mount the savevg resouce
    nim_mount ${savevg}

    # run the command.
    ${LSSAVEVG} ${lssavevg_flags} -f ${access_pnt} 2>${ERR} || err_from_cmd ${LSSAVEVG}

    # unmount the savevg resouce
    nim_unmount ${savevg}
  ;;

  lssavewpar)
    # if no lssavewpar_flags were specified, then use the default to list
    # the software installed in the savevg image.
    if [[ -z ${lssavewpar_flags} ]]
    then
      lssavewpar_flags="-l"
    fi

    # if the lssavevg_flags variable is missing a dash, add it now.
    if [[ ${lssavewpar_flags} != "-"* ]]
    then
      lssavewpar_flags="-${lssavewpar_flags}"
    fi

    # mount the savewpar resouce
    nim_mount ${savewpar}

    # run the command.
    ${LSSAVEWPAR} ${lssavewpar_flags} -f ${access_pnt} 2>${ERR} || err_from_cmd ${LSSAVEWPAR}

    # unmount the savewpar resouce
    nim_unmount ${savewpar}
  ;;

  lsadapters)  # loop through and cat each file in the adapter_def directory
	# mount the adapter_def location
	nim_mount ${adapter_def}

	for file in $( ${LS} ${access_pnt} )
	do
		echo "********** Output for ${file} **********"
		${CAT} "${access_pnt}/${file}" 2>${ERR} || err_from_cmd ${CAT}
		echo "**********   End of ${file} **********"
	done
  ;;

  \?)   # unknown option
      error ${ERR_BAD_OPT} ${OPTARG}
  ;;

  esac

# success
exit 0
