# @(#)13	1.1  src/sysmgt/cfgassist/com/ibm/cfgassist/apps/wsmconfig/inst_panel_exits.sh, cfgassist, websm720 7/15/11 02:53:02
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# websm720 src/sysmgt/cfgassist/com/ibm/cfgassist/apps/wsmconfig/inst_panel_exits.sh 1.1 
#  
#                  
#  
# OBJECT CODE ONLY SOURCE MATERIALS 
#  
# COPYRIGHT International Business Machines Corp. 2000,2011 
# All Rights Reserved 
#  
# The source code for this program is not published or otherwise 
# divested of its trade secrets, irrespective of what has been 
# deposited with the U.S. Copyright Office. 
#  
# IBM_PROLOG_END_TAG 
. winstsh_msg.shh

DSPMSG=/usr/bin/dspmsg
OUTPUTFILE="/var/adm/ras/nim.installp"
INSTALLP="/usr/sbin/installp"
GENINSTALL="/usr/sbin/geninstall"
NIM="/usr/sbin/nim" 
NIMCLIENT="/usr/sbin/nimclient" 
SM_INST="/usr/lib/instl/sm_inst"
M_SM_NIM="/usr/lpp/bos.sysmgt/nim/methods/m_sm_nim"
C_SM_NIM="/usr/lpp/bos.sysmgt/nim/methods/c_sm_nim"

#NIM variables
NEW_TARGET=""
GRP_PREFIX="wsm_Tmp_Multiple_Targets@"
ERR_FILE="/tmp/.nim.err.$$"
DOING_MULT_NON_GRP=""
MULT_NON_GRP_COUNT=0
MULT_NON_GRP_MAX=0

#---------------------------------------------------------------------------
#
# NAME:  ck_mult_targets (NIM MASTER FUNCTION)
#
# CALLED FROM: inst_panel_exits
#
# Checks the "target" variable specified to see if it is a list of machines.
# If more than one machine specified, AND if the types are the same,
# creates a temporary group using list elements as members.  
# Sets the global NEW_TARGET variable with the input target if 
# no group created, OR the name of the temporary group.
#---------------------------------------------------------------------------

function ck_mult_targets {

target="${1}"
create_group="${2}"

	RC=0
	count=0
	# Loop through the target(s) passed-in ..
	MEMBERS=""
	for tgt in ${target}
	do
		((count+=1))
		tmp=`lsnim ${tgt}`
		type=`echo ${tmp} | cut -f3 -d' '`
		[ ${count} -eq 1 ] && first_type=${type}

		[ ${count} -gt 1 ] && DOING_MULT_NON_GROUP=true

		if [[ ${type} != ${first_type} ]]
		then
			create_group=false
			
		elif 	[[ ${type} != standalone ]] &&
			[[ ${type} != diskless ]] &&
			[[ ${type} != dataless ]]
		then 
			create_group=false
		fi
		[[ ${create_group} = true ]] && \
			MEMBERS="${MEMBERS} -a add_member=${tgt}"
	done

	# create a group if applicable
	if [[ ${count} -gt 1 ]] && [[ ${create_group} = true ]]
	then
		name=`/usr/lpp/bos.sysmgt/nim/methods/m_gen_nim_name \
								${GRP_PREFIX}`
		NEW_TARGET=${name}
		nim -o define -t mac_group ${MEMBERS} ${name} 2>${ERR_FILE}
		RC=$?
		if [[ $RC -ne 0 ]]
		then
			# send output to stderr
			cat ${ERR_FILE} 1>&2

			# remove the group if we created it	
			nim -o remove ${name} > /dev/null 2>&1
		fi
	else
		NEW_TARGET="${target}"
		if [[ ${count} -gt 1 ]]
		then
			DOING_MULT_NON_GRP=1
			MULT_NON_GRP_COUNT=1
			MULT_NON_GRP_MAX=${count}
		fi
	fi

	return ${RC}
}

#---------------------------------------------------------------------------
#
# NAME:  rm_tmp_grps (NIM MASTER FUNCTION)
#
# CALLED FROM: inst_panel_exits
#
# Removes group targets conforming to the naming convention of the temporary
# groups IF the "target" variable differs from the "new_target" variable
#---------------------------------------------------------------------------

function rm_tmp_grp {

rm_tgt="${1}"

	[[ ${rm_tgt} = ${NEW_TARGET} ]] && return

	for tgt in ${NEW_TARGET}
	do
		if [[ ${tgt} = ${GRP_PREFIX}* ]]
		then
			nim -o remove ${tgt} > /dev/null 2>&1
		fi
	done
}

#---------------------------------------------------------------------------
#
# NAME:  print_mult_non_grp_progress (NIM MASTER FUNCTION)
#
# CALLED FROM: inst_panel_exits 
#
# Prints a progress msg when processing multi-selected targets that aren't
# handled by a group (groups do their own progress reporting).
# NOTE: Uses globals: DOING_MULT_NON_GRP, MULT_NON_GRP_MAX, and
# MULT_NON_GRP_COUNT.  Increments the latter.
#---------------------------------------------------------------------------

function print_mult_non_grp_progress {

print_tgt="${1}"

	[[ -z ${DOING_MULT_NON_GRP} ]] && return

	MSG_PROCESSING=20
        print $($DSPMSG -s ${MSG} wnimsh.cat ${MSG_PROCESSING} "Processing target %s of %s: %s..." ${MULT_NON_GRP_COUNT} ${MULT_NON_GRP_MAX} ${print_tgt})
	print

	((MULT_NON_GRP_COUNT+=1))
}

#---------------------------------------------------------------------------
#
# NAME:  display_log
#
# CALLED FROM: inst_panel_exits
#
# PARAMETERS:  $1 - the log file
#              $2 - IF set to "true" THEN it prints the last entry in a log 
#                      file containing standard BEGIN/END timestamps.
#                   ELSE IF set to "alog" THEN it displays the log file using 
#                      alog command.
#---------------------------------------------------------------------------

function display_log {

   ERR=/dev/null

   log_file=$1
   show_last_entry=

   if [ ! -s $log_file ]; then
      $DSPMSG -s ${MSG} winstsh.cat ${EMPTY} "<EMPTY>"
      return $?
   fi

   if [ $# -gt 1 ]; then
      if [ "$2" = "alog" ]; then
         alog -f $log_file -o
         return $?
      else
         show_last_entry=$2
      fi
   fi

   if [ "$show_last_entry" = "true" ]; then
      begin_time=`egrep "^BEGIN" $log_file | \
                    awk 'BEGIN {FS=":"} {\
                            split($4, sec_yr," ");\
                            printf ("%s %s\n", sec_yr[2], $5)\
                         }' | \
                    sort -u | tail -1 | awk '{print $2}'`

      if [ ! "$begin_time" ]; then
         cat $log_file 2>${ERR}
      else
         awk -v begin_time=${begin_time}  \
                              'BEGIN {  \
                                  FS=":"; \
                                  print_line = 0; \
                               } \
                               ($1 == "BEGIN" && $5 == begin_time) { \
                                       print_line = 1; \
                               } \
                               (print_line == 1) { \
                                       print; \
                                       if ($1 == "END") \
                                          print_line = 0;\
                               }' $log_file
      fi

   else
      cat $log_file 2>${ERR}
   fi

   return $?
}

# -----------------------------------------------------------------------
# NAME: DeinstallPanel
#
# PURPOSE: To remove individually selected software.
#
# CALLED FROM: wsminstall.res (see the DeinstallPanel interaction)
# -----------------------------------------------------------------------
DeinstallPanel()
{
       remove_depend="${1}"
       extend_fs="${2}"
       detail_output="${3}"
       software="${4}"
       preview="${5}"

       INSTALLP_OPTIONS=""
       OUTPUTFILE="/var/adm/ras/nim.installp"

       if [ "${remove_depend}" = "true" ] ; then
           INSTALLP_OPTIONS="-g"
       fi

       if [ "${extend_fs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -X"
       fi

       if [ "${detail_output}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -V2"
       fi

       if [ "${preview}" = "true" ] ; then
	   INSTALLP_OPTIONS="$INSTALLP_OPTIONS -e $OUTPUTFILE"
           wsmexec ${GENINSTALL} 2>&1 -pu \
                   -I "$INSTALLP_OPTIONS" $software
       else
	   INSTALLP_OPTIONS="$INSTALLP_OPTIONS -e $OUTPUTFILE"
           wsmexec ${GENINSTALL} 2>&1 -u \
	           -I "$INSTALLP_OPTIONS" $software
       fi
}

# -----------------------------------------------------------------------
# NAME: ShowProperties
#
# PURPOSE: To perform one of the following functions against a selected
#          software object in the main container:
#
#              Description                    $MODE (see below)
#              -----------                    -----
#              a) Show Installation History   "one"
#              b) Show Requisites             "two"
#              c) Show Dependents             "three"
#              c) Show Files                  "four"
#
# CALLED FROM: wsminstall.res (see the ShowHistory, ShowReqs, ShowDeps,
#                              and ShowFiles interactions)
# -----------------------------------------------------------------------
ShowProperties()
{
       LIST="${1}"
       MODE="${2}"
       NEW_LIST=""
       EMPTY_DIR=/tmp/.empty.$$

       # ------------------------------------------------------
       # cd to an empty directory to avoid unwanted wildcarding
       # ------------------------------------------------------
       CWD=`pwd`
       mkdir -p $EMPTY_DIR 2>/dev/null && cd $EMPTY_DIR

       # -----------------------------------------------------
       # replace with NULL:
       # a) everything in parens including the parens
       # b) every occurence of v.r.m.f.* (for 3.2 ptfs)
       # c) every occurence of v.r.m.f
       # -----------------------------------------------------
       TMP_LIST=$(echo $LIST | sed  \
              -e 's/([^)]*)//g' \
              -e 's/ [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.[a-z]*[A-Z]*[0-9]*//g' \
              -e 's/ [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*//g')

       # --------------------------------------
       # Add a * to each name in the new list.
       # --------------------------------------
       for name in ${TMP_LIST}
       do
            NEW_LIST="${NEW_LIST} ${name}*"
       done

       case "${MODE}" in
           one)   lslpp -h ${NEW_LIST}
                  ;;
           two)   lslpp -p ${NEW_LIST} 
                  ;;
           three) lslpp -d ${NEW_LIST} 
                  ;;
           four)  lslpp -f ${NEW_LIST} 
                  ;;
       esac

       RC=$?
       cd $CWD
       rm $EMPTY_DIR 2>/dev/null
       return $RC
}

# -----------------------------------------------------------------------
# NAME: Commit_or_RejectAllPanel
#
# PURPOSE: To commit or reject individually selected software updates or all
#          applied updates.
#
# CALLED FROM: wsminstall.cpp (see Commit_or_RejectAllPanel interaction)
#
# NOTES: This is panel is used by the NIM application as well.
# -----------------------------------------------------------------------
Commit_or_RejectAllPanel()
{
       calling_app="${1}"
       target="${2}"
       commit_reqs="${3}"
       detail_output="${4}"
       software="${5}"
       commit_all="${6}"
       force="${7}"
       reject_depend="${8}"
       extend_fs="${9}"
       reject_all="${10}"
       which_dialog="${11}"
       preview="${12}"

       NIM_FORCE=""
       SOFTWARE=""

       if [[ "${which_dialog}" = "reject_dialog" ]] ; then
            INSTALLP_OPTIONS="-rB"
       else
            INSTALLP_OPTIONS="-c"
       fi

       if [ "${commit_reqs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -g"
       fi

       if [ "${preview}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -p"
       fi

       if [ "${detail_output}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -V2"
       fi

       if [ -n "${software}" ] ; then
           SOFTWARE="${software}"
       fi

       if [ "${commit_all}" = "true" ] ; then
           SOFTWARE=all
       fi

       if [ "${force}" = "true" ] ; then
           NIM_FORCE="F"
       fi

       if [ "${extend_fs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -X"
       fi

       if [ "${reject_depend}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -g"
       fi


       if [[ ${calling_app} = NIM ]] ;then
          # Convert a list of targets to a machine group (if applicable)
          # Note: We'll build a machine group, if necessary and if we can,
          # if we aren't doing a "reject all" If doing a reject all, we need
          # to look at each target individually.
          [[ "${reject_all}" = "true" ]] && \
			build_mac_group=false || build_mac_group=true
          ck_mult_targets "${target}" ${build_mac_group} 
          RC=$?; [[ $RC -ne 0 ]] && return ${RC}
       else  
          NEW_TARGET=junk
          RC=0
       fi

       for tgt in ${NEW_TARGET}
       do
	  print_mult_non_grp_progress ${tgt}

          if [ "${reject_all}" = "true" ] ; then
             TMPFILE=/tmp/.inst.exit.$$
             OLDLANG=${LANG}
             export LANG=C
             if [[ ${calling_app} = NIM ]] ; then
                nim -o lslpp -a lslpp_flags="qlac" ${tgt} > ${TMPFILE}
             else
                lslpp -qlac > ${TMPFILE}
             fi
             export LANG=${OLDLANG}

             grep APPLIED ${TMPFILE} | \
                awk 'BEGIN {FS=":"} ($6 == "F") {print $2 " " $3 }' | \
                sort -u | while read Name
                          do
                                SOFTWARE="${SOFTWARE} ${Name}"
                          done

             # NOTE!  This is a hack.  If user selected "reject all"
             # and there was no applied updates, we'll send the list of all
             # products through and let the resulting failure message suffice.
             if [[ -z "${SOFTWARE}" ]] ; then
                cat ${TMPFILE} | grep -v APPLIED | \
                   awk 'BEGIN {FS=":"} {print $2}' | cut -d. -f1 | sort -u | \
                   while read prod
                   do
                        SOFTWARE="${SOFTWARE} ${prod}"
                   done
             fi
             rm ${TMPFILE}
         fi

         if [[ "${calling_app}" = NIM ]] ; then
            wsmexec ${NIM} -${NIM_FORCE}o maint \
                        -a filesets="${SOFTWARE}" \
                        -a installp_flags="${INSTALLP_OPTIONS}" \
                        ${tgt}
         else
            wsmexec ${INSTALLP} -e ${OUTPUTFILE} ${INSTALLP_OPTIONS} ${SOFTWARE}
         fi
         ((RC+=$?))

       done

       [[ ${calling_app} = NIM ]] && \
          # Remove any group target we may have created above
          rm_tmp_grp "${target}"
       return ${RC}
}

# -----------------------------------------------------------------------
# NAME: Commit_or_RejectPanel
#
# PURPOSE: To commit or reject individually selected software updates.
#
# CALLED FROM: wsminstall.res (see Commit_or_RejectPanel interaction)
# -----------------------------------------------------------------------
Commit_or_RejectPanel()
{
       commit_reqs="${1}"
       reject_depend="${2}"
       extend_fs="${3}"
       detail_output="${4}"
       software="${5}"
       reject_dialog="${6}"
       preview="${7}"

       INSTALLP_OPTIONS=""
       OUTPUTFILE="/var/adm/ras/nim.installp"

       if [ "${commit_reqs}" = "true" ] ; then
           INSTALLP_OPTIONS="-g"
       fi

       if [ "${reject_depend}" = "true" ] ; then
           INSTALLP_OPTIONS="-g"
       fi

       if [ "${preview}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -p"
       fi

       if [ "${extend_fs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -X"
       fi

       if [ "${detail_output}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -V2"
       fi

       if [[ "${reject_dialog}" = "true" ]] ; then
           wsmexec /bin/ksh ${SM_INST} installp_cmd -r -B -f "${software}" \
                      $INSTALLP_OPTIONS -e ${OUTPUTFILE}
       else
           wsmexec /bin/ksh ${SM_INST} installp_cmd -c -f "${software}" \
                      $INSTALLP_OPTIONS -e ${OUTPUTFILE}
       fi
}

# -----------------------------------------------------------------------
# NAME: VerifySWPanel
#
# PURPOSE: To perform a lppchk operation against selected software.
#
# CALLED FROM: wsminstall.res (see VerifySWPanel and VerifyAllSWPanel 
#                              interactions)
# NOTES:   If $software is not set, this was called from VerifyAllSWPanel
# -----------------------------------------------------------------------
VerifySWPanel()
{
       verify_type="${1}"
       update_sw="${2}"
       detail_output="${3}"
       software="${4}"

       case "${verify_type}" in
           one)    COMMAND="lppchk -f "
                   ;;
           two)    COMMAND="lppchk -v "
                   ;;
           three)  if [[ "${update_sw}" = "true" ]] ; then
                       COMMAND="lppchk -c -u "
                   else
                       COMMAND="lppchk -c "
                   fi
                   ;;
           four)   if [[ "${update_sw}" = "true" ]] ; then
                       COMMAND="lppchk -l -u "
                   else
                       COMMAND="lppchk -l "
                   fi
                   ;;
       esac

       if [[ "${detail_output}" = "true" ]] ; then
           COMMAND="$COMMAND -m3"
       fi

       if [[ -n "${software}" ]] ; then
           for name in $software
           do
               #echo ">>>>> Verifying/Checking $name"
               $COMMAND ${name}\*
           done
       else
           # Verify all installed software
           $COMMAND
       fi
}

# -----------------------------------------------------------------------
# NAME: ListFilesetPanel
#
# PURPOSE: Searches for files and lists the fileset it is associated with.
#
# CALLED FROM: wsminstall.res (see ListFilesetPanel interaction)
#
# NOTES:   This is the "List Fileset Containing File" function.
# -----------------------------------------------------------------------
ListFilesetPanel()
{
	caller="${1}"
	target="${2}"
	search_type="${3}"
	filename="${4}"
	exact_match="${5}"

	RC=0
	if [[ ${caller} = NIM ]]
	then
		# Convert a list of targets to a machine group (if applicable)
		ck_mult_targets "${target}" true 
		RC=$?; [[ $RC -ne 0 ]] && return ${RC}
	else
		NEW_TARGET=junk
	fi

	for tgt in ${NEW_TARGET}
	do
		print_mult_non_grp_progress ${tgt}

		case "${search_type}" in
		one)    if [[ "${exact_match}" = "true" ]] 
			then
			   [[ ${caller} = NIM ]] && \
			   	nim -o lslpp \
			       	   -a lslpp_flags="-Jw ${filename}" ${tgt} || \
                                lslpp -Jw "${filename}"
			   ((RC+=$?))
			else
		           [[ ${caller} = NIM ]] && \
			     nim -o lslpp \
			        -a lslpp_flags="-Jw *${filename}*" ${tgt} || \
                             lslpp -Jw "*${filename}*"
			   ((RC+=$?))
			fi
                   ;;
		two)    [[ ${caller} = NIM ]] && \
			    nim -o lslpp -a lslpp_flags="-Jw" ${tgt} || \
			    lslpp -Jw 
			((RC+=$?))
                   ;;
		esac
	done

	[[ ${caller} = NIM ]] && \
		# Remove any group target we may have created above
		rm_tmp_grp "${target}"

	exit $RC
}

# -----------------------------------------------------------------------
# NAME: OSLevelPanel
#
# PURPOSE: Invokes more advanced oslevel commands
#
# CALLED FROM: wsminstall.res (see OSLevelPanel interaction)
# -----------------------------------------------------------------------
OSLevelPanel()
{
       more_info="${1}"
       more_info_type="${2}"
       maint_level="${3}"

       if [[ "${more_info}" = "true" ]] ; then
          case "${more_info_type}" in
              one)    oslevel -g
                      ;;
              two)    oslevel -l $maint_level
                      ;;
          esac
       fi
}

# -----------------------------------------------------------------------
# NAME: Install_UpdatePanel
#
# PURPOSE: To install/update software as specified by the user.
#
# CALLED FROM: wsminstall.cpp
#
# NOTES: This panel is used by the NIM application as well.
# -----------------------------------------------------------------------

Install_UpdatePanel()
{
       caller="${1}"
       target="${2}"
       force="${3}"
       input_choice="${4}"
       detail_output="${5}"
       inst_updt_all="${6}"
       commit="${7}"
       save_replaced="${8}"
       install_reqs="${9}"
       extend_fs="${10}"
       overwrite_files="${11}"
       verify_sizes="${12}"
       include_langs="${13}"
       multi_volume="${14}"
       file_name="${15}"
       device_type="${16}"
       which_dialog="${17}"
       packages="${18}"
       script="${19}"
       preview="${20}"
       max_installs="${21}"
       max_time="${22}"
       acceptlicense="${23}"

       INSTALLP_OPTIONS=""
       INCLUDE_LANGS=""
       FLAGS=""
       OUTPUTFILE="/var/adm/ras/nim.installp"
       SM_NIM_OPTION=""
       SM_TOC=/tmp/.list_filesets.toc
       SM_LIST_FILESETS_OUT=/tmp/.list_filesets.out
       mid_file_name=/tmp/.midfilename

       # Make sure this filesets do not exist before calling installp
       rm -f $SM_TOC $SM_LIST_FILESETS_OUT

       if [ -n "${input_choice}" ] ; then
           INSTALL_DEVICE="${input_choice}"
       fi

       if [ "${commit}" = "true" ] ; then
           INSTALLP_OPTIONS="-c"

           if [ "${save_replaced}" = "false" ] ; then
               INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -N"
           fi
       fi

       if [ "${preview}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -p"
           script=""
       fi
       
       if [ "${acceptlicense}" = "true" ] ; then
               INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -Y"
       fi

       if [ "${install_reqs}" = "true" ] ; then
           # -------------------------------------------------------
           # Don't add the -g flag if this is a "update all".
           # The sm_inst instfix_cmd code adds this flag by default.
           # -------------------------------------------------------
           if [[ "${which_dialog}" != "update_dialog" || \
                 "${inst_updt_all}" = "true" ]] ; then
               INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -g"
           fi
       fi

       if [ "${extend_fs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -X"
       fi

       if [ "${overwrite_files}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -F"
       fi

       if [ "${verify_sizes}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -v"
       fi

       if [ "${include_langs}" = "true" ] ; then
           # Because of the way the c/m_sm_nim commands process flags
           # make sure the -G flag does not look like a true installp
           # flag.
           if [[ ${caller} = NIM ]] || \
              [[ ${device_type} = NETWORK ]]
           then
              INCLUDE_LANGS="-G"
           else
              INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -G"
           fi
           # Associate the -Q flag (hiding instreq failures)
           # with the decision to include languages.
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -Q"
       fi

       if [ "${multi_volume}" = "false" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -S"
       fi

       if [ "${detail_output}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -V2"
       fi

       # -------------------------------------------------------------
       # Modify the installp flags if we are going to be invoking NIM
       # commands.  (strip off "-" and prepend with -f)
       # -------------------------------------------------------------
       if [[ "${device_type}" = "NETWORK" ]] || [[ "${caller}" = "NIM" ]]
       then
           index=0
           echo $INSTALLP_OPTIONS | awk '{ i = 1
                                           while (i <= NF) {
                                              print $i
                                              i++
                                           }
                                         }' | while read option
                                       do
                                          option=$(echo $option | cut -f2 -d"-")
                                          if [[ $index -eq 0 ]]
                                          then
                                               FLAGS="-f $option"
                                               index=1
                                          else
                                               FLAGS="${FLAGS} -f $option"
                                          fi
                                       done
       fi

       if [[ ${force} = true ]]
       then
          NIM_FORCE=-F
       else
          NIM_FORCE=""
       fi

       # -------------------------------------------------------
       # Set filters based on inst_updt_all flag & which_dialog
       # -------------------------------------------------------
       if [[ ${inst_updt_all} = true ]]
       then
          if [[ ${which_dialog} = install_dialog ]]
          then
             echo "_all_latest" > ${file_name}
          elif [[ ${which_dialog} = update_dialog ]]
          then
             echo "_update_all" > ${file_name}
          elif [[ ${which_dialog} = install_packages_dialog ]]
          then
             pkg_list=""
             for pkg in `echo ${packages} | sed 's/\// /g'`
             do

                [[ -z ${pkg_list} ]] && \
                        pkg_list=${pkg} || \
                        pkg_list="${pkg_list},${pkg}"
             done
             echo ${pkg_list} > ${file_name}
          fi
       fi

       # -----------------------------
       # Call the appropriate command
       # -----------------------------
       if [[ "${caller}" = NIM ]]
       then
           # Convert a list of targets to a machine group (if applicable)
           ck_mult_targets "${target}" true 
           RC=$?; [[ $RC -ne 0 ]] && return ${RC}

           # For multiple NIM targets, the software file might get removed, 
           # by NIM code, between iterations of the loop below.  Make a copy 
           # of the file just in case.  We'll use it and remove it later.
           tmp_file_name=/tmp/.nim.inst.list.$$
           cp ${file_name} ${tmp_file_name} > /dev/null 2>&1

           for tgt in ${NEW_TARGET}
           do
		print_mult_non_grp_progress ${tgt}

                #------------------------------
                # Doing install from NIM master
                #------------------------------
                if [[ "${which_dialog}" = update_dialog ]] && \
                   [[ "${inst_updt_all}" != true ]]
                then
                   #--------------------------------
                   # install fixes
                   #--------------------------------
                   # NOTE: We use installp_options here and NOT FLAGS like
                   # when calling c/m_sm_nim
                   FIXES=`cat ${file_name}`
                   wsmexec ${NIM} ${NIM_FORCE} -o cust -a fixes="${FIXES}" \
                    -a lpp_source=$INSTALL_DEVICE \
                    ${max_installs:+ -a concurrent=${max_installs}} \
                    ${max_time:+ -a time_limit=${max_time}} \
               ${INSTALLP_OPTIONS:+-a installp_flags="-a ${INSTALLP_OPTIONS}"} \
                    ${script:+ -a script=${script}} \
                    ${tgt}
                else
                   #--------------------------------
                   # update_all, install all_latest,
                   # OR install individual filesets
                   #--------------------------------
                   if [[ ${which_dialog} = update_dialog ]] && \
                      [[ ${inst_updt_all} = true ]]
                   then
                      FILE=""
                      sm_nim_cmd=update_all
                   else
                      # restore the contents of the software list just in
                      # case it was (intentionally) removed by NIM.
                      # Convert commas to newlines so 'while read' works.
		      tr "," "\n" < $tmp_file_name > $file_name # This line replaces 'cp ${tmp_file_name} ${file_name}'

                      listofsw=""
                      cat ${file_name} | \
                      while read fileset version
                      do
                                listofsw="${fileset}, ${listofsw}"
                      done
                      echo "${listofsw}">${file_name}
  

                      FILE="-o ${file_name}"
                      sm_nim_cmd=cust
                   fi
                   wsmexec /bin/ksh ${M_SM_NIM} ${sm_nim_cmd} ${INCLUDE_LANGS} \
                               ${NIM_FORCE} \
                               -l $INSTALL_DEVICE \
                               ${FILE} \
                               ${max_installs:+ -C ${max_installs}} \
                               ${max_time:+ -T ${max_time}} \
                               ${FLAGS} \
                               ${script:+ -s ${script}} \
                               -t ${tgt}
                fi

                ((RC+=$?))
           done
           rm -f ${tmp_file_name}

           # Remove any group target we may have created above
           rm_tmp_grp "${target}"

       elif [[ "${device_type}" = "NETWORK" ]]
       then
            # --------------------------
            # Doing a NIM client install
            # --------------------------
            if [[ "${which_dialog}" = update_dialog ]] && \
               [[ "${inst_updt_all}" != true ]]
            then
               # -----------------------------------------------------
               # install fixes
               #
               # NOTE: We use installp_options here and NOT FLAGS like
               # when calling c/m_sm_nim.
               # -----------------------------------------------------
               FIXES=`cat ${file_name}`
               wsmexec ${NIMCLIENT} -o cust -a fixes="${FIXES}" \
                 -a lpp_source=$INSTALL_DEVICE \
                 ${INSTALLP_OPTIONS:+-a installp_flags="-a ${INSTALLP_OPTIONS}"}
               RC=$?
            else
               # -------------------------------
               # update_all, install all_latest,
               # OR install individual filesets
               # -------------------------------
               if [[ ${which_dialog} = update_dialog ]] &&
                  [[ ${inst_updt_all} = true ]]
               then
                  FILE=""
                  sm_nim_cmd=update_all
               else
                  FILE="-o ${file_name}"
                  sm_nim_cmd=cust
               fi
               wsmexec /bin/ksh ${C_SM_NIM} ${sm_nim_cmd} -l $INSTALL_DEVICE \
                                  ${FILE} ${INCLUDE_LANGS} ${FLAGS}
               RC=$?
            fi
       else
            # ---------------------------------------
            # Set up appropriate sm_inst command for
            # update_all, install fixes, install
            # all_latest or individual filesets
            # ---------------------------------------
            if [[ "${which_dialog}" != update_dialog ]] || \
               [[ "${inst_updt_all}" = true ]]
            then
               sm_inst_cmd=installp_cmd
               INSTALLP_OPTIONS="-a $INSTALLP_OPTIONS"
	       if [[ "${caller}" != "NIM" ]]; then
		  INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -W"
	       fi
            else
               sm_inst_cmd=instfix_cmd
            fi

	# replace commas by newlines
	tr "," "\n" < $file_name > /tmp/.midfile 

	#put it back in original file name
	cat /tmp/.midfile>${file_name}

        wsmexec /bin/ksh ${SM_INST} ${sm_inst_cmd} \
                  -d $INSTALL_DEVICE -f ${file_name} $INSTALLP_OPTIONS \
                  -e ${OUTPUTFILE}
        RC=$?

       	fi
       	rm -f ${file_name} ${mid_file_name} 
       	return $RC
}

# -----------------------------------------------------------------------
# NAME: InstallBundlePanel
#
# PURPOSE: To install from a software bundle.
#
# CALLED FROM: wsminstall.cpp
#
# NOTES: This panel is used by the NIM application as well.
# -----------------------------------------------------------------------

InstallBundlePanel()
{

       caller="${1}"
       target="${2}"
       force="${3}"
       input_choice="${4}"
       bundle="${5}"
       inst_all_lic="${6}"
       commit="${7}"
       save_replaced="${8}"
       install_reqs="${9}"
       extend_fs="${10}"
       verify_sizes="${11}"
       include_langs="${12}"
       detail_output="${13}"
       multi_volume="${14}"
       script="${15}"
       preview="${16}"
       max_installs="${17}"
       max_time="${18}"
       acceptlicense="${19}"

       INSTALLP_OPTIONS=""
       SYS_BUNDLE_DIR="/usr/sys/inst.data/sys_bundles"
       USER_BUNDLE_DIR="/usr/sys/inst.data/user_bundles"
       OUTPUTFILE="/var/adm/ras/nim.installp"
       RC=0

       if [ "${commit}" = "true" ] ; then
           INSTALLP_OPTIONS="-c"

           if [ "${save_replaced}" = "false" ] ; then
               INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -N"
           fi
       fi

       if [ "${preview}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -p"
           script=""
       fi

       if [ "${install_reqs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -g"
       fi

       if [ "${extend_fs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -X"
       fi

       if [ "${verify_sizes}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -v"
       fi

       if [ "${include_langs}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -G"
       fi

       if [ "${detail_output}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -V2"
       fi

       if [ "${multi_volume}" = "false" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -S"
       fi
       
       if [ "${acceptlicense}" = "true" ] ; then
           INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -Y"
       fi
       
       # -------------------------------------------------------------
       # Call the appropriate command.
       # -------------------------------------------------------------
       if [[ "${caller}" = "NIM" ]]
       then
           # -------------------------------------------------------------
           # Modify the installp flags for NIM (remove "-" and prepend with -f)
           # -------------------------------------------------------------
           index=0
           echo $INSTALLP_OPTIONS | awk '{ i = 1
                                           while (i <= NF) {
                                              print $i
                                              i++
                                           }
                                         }' | while read option
                                       do
                                          option=$(echo $option | cut -f2 -d"-")
                                          if [[ $index -eq 0 ]]
                                          then
                                               FLAGS="-f $option"
                                               index=1
                                          else
                                               FLAGS="${FLAGS} -f $option"
                                          fi
                                       done


           [[ ${force} = true ]] && NIM_FORCE=-F || NIM_FORCE=""

           # If the script resource is <none available>, just
           # set it to an empty string. NIM resources cannot
           # contain the <> characters, so it must be <none
           # available> if they exist in the variable.
           echo $script | grep "<" > /dev/null 2>&1 
           [[ $? -eq 0 ]] && script=""

           # Convert a list of targets to a machine group (if applicable)
           ck_mult_targets "${target}" true 
           RC=$?; [[ $RC -ne 0 ]] && return ${RC}

           for tgt in ${NEW_TARGET}
           do
		print_mult_non_grp_progress ${tgt}

                wsmexec /bin/ksh ${M_SM_NIM} inst_bundle -t ${tgt} \
                    -l ${input_choice} -b "${bundle}" \
                    ${max_installs:+ -C ${max_installs}} \
                    ${max_time:+ -T ${max_time}} \
                    ${script:+ -s ${script}} \
                    ${NIM_FORCE} ${FLAGS}
                ((RC+=$?))
           done

           # Remove any group target we may have created above
           rm_tmp_grp "${target}"

       else # if caller is INSTALL

	  INSTALLP_OPTIONS="${INSTALLP_OPTIONS} -W"

          if [[ "${inst_all_lic}" = "true" ]] ; then
              wsmexec /bin/ksh ${SM_INST} installp_cmd -a -Q \
                       -d ${input_choice} -b ${bundle} \
                       -e ${OUTPUTFILE} \
                       -f _all_licensed $INSTALLP_OPTIONS
          RC=$?
          else
              wsmexec /bin/ksh ${SM_INST} installp_cmd -a -Q \
                       -d ${input_choice} -e ${OUTPUTFILE} \
                       -b ${bundle} -f 'all' $INSTALLP_OPTIONS
              RC=$?
         fi
      fi
      return $RC
}

# -----------------------------------------------------------------------
# NAME: CopyToDirPanel
#
# PURPOSE: Invokes 'sm_inst bffcreate_cmd' to copy software packages
#          to a specified directory.
#
#          If $SOFTWARE_FLAG == "all", the user requested to copy all software.
#          If $SOFTWARE_FLAG != "all", the FILENAME parameter will be used to
#                                 copy selected software to a directory.
#
# CALLED FROM: wsminstall.res (see CopyToDirPanel interaction)
# -----------------------------------------------------------------------

CopyToDirPanel()
{

       INPUT_SOURCE="${1}"
       OUTPUT_DIR="${2}"
       SOFTWARE_FLAG="${3}"
       TEMP_STORAGE="${4}"
       FILENAME="${5}"
       EXTEND_FS="${6}"
       MULTI_VOLUME="${7}"
       TARGET="${8}"
       CREATE_LPP_SOURCE="${9}"
       LPP_SOURCE_NAME="${10}"

       TEMP_DIR=""
       COPY_OPTIONS=""


       if [[ "${TEMP_STORAGE}" != "/tmp" ]] ; then
           TEMP_DIR="-w ${TEMP_STORAGE}"
       fi

       if [[ "${EXTEND_FS}" = "true" ]] ; then
           COPY_OPTIONS="-X"

       fi

       if [[ "${MULTI_VOLUME}" = "false" ]] ; then
           COPY_OPTIONS="${COPY_OPTIONS} -S"
       fi

       # Set a command prefix depending upon whether or not
       # we'll be running the command locally or on a remote
       # NIM server.
       cmd_prefix="/usr/bin/ksh -c"
       if [[ -n ${TARGET} ]]
       then
          # Must be working with a NIM lpp_source  We need to
          # rsh the commands to the lpp_source server if it isn't
          # the NIM master.
          server=`lsnim -Z -a server ${TARGET} | \
                     awk 'BEGIN {FS=":"} (NR > 1){print $2}'`
          if [[ ${server} != master ]]
          then
             server_host_name=`lsnim -Z -a if1 ${server} | \
                     awk 'BEGIN {FS=":"} (NR > 1){print $3}'`
             cmd_prefix="/usr/bin/rsh ${server_host_name} "


             # If the filesets to copy are in a file, rcp the file to
             # the target and remove the local copy
             if [[ ${SOFTWARE_FLAG} != all ]]
             then
                new_file=/tmp/wsm_copy_images.$$

                rcp ${FILENAME} ${server_host_name}:${new_file}
                rm ${FILENAME}
                FILENAME=${new_file}
             fi
          fi
       fi

       if [[ "${SOFTWARE_FLAG}" = "all" ]] ; then
           ${cmd_prefix} "/usr/lib/instl/sm_inst bffcreate_cmd \
                 -d $INPUT_SOURCE -f "all" \
                 -t $OUTPUT_DIR $TEMP_DIR $COPY_OPTIONS"
           rc=$?
       else

           # Before calling bffcreate, see if we need to expand any products
           # selected into packages because that is what bffcreate is expecting
           if [[ ${SOFTWARE_FLAG} = "expand" ]]
           then
              BFF_LIST=/tmp/.wsm.bff.list.$$
              MY_FILE=/tmp/myfile1

              # Get the list of bffs on the input source

               ${cmd_prefix} "gencopy -Ld ${INPUT_SOURCE} 2>/dev/null" > $MY_FILE

               OIFS=${IFS}
               IFS=:
               cat $MY_FILE | \
               while read name package fileset version format something label
               do
                 echo "${format}:${package}" >> ${BFF_LIST}
               done

                # clean duplicate entries from list of bff's on input source
                uniq ${BFF_LIST} ${MY_FILE}
                cat ${MY_FILE}>${BFF_LIST}

              WORKING_FILE=/tmp/.wsm.new.file.$$


              IFS=${OIFS}

              #Take entries from the file we sent in ( the one containing the
              #names of the packages we want to install ) and match them to
              #format/package combos for gencopy.

              ${cmd_prefix} "cat ${FILENAME}" | \
              while read name
              do
                   # Check if this name has a token added by the GUI.
                   # and expand to matching package list if so.
                   if [[ ${name%%__Xpand_7143_} != "${name}" ]]
                   then
                       #Get the list of matching entries (<pkg_name> <level>)
                      grep ":${name%%__Xpand_7143_}" ${BFF_LIST}>>${WORKING_FILE}
                   else
			if [[ "${name}" != "" ]]
			then
                      		echo "${name}">>${WORKING_FILE}
			fi
                   fi
              done

              new_list=`sort -u ${WORKING_FILE}`
              ${cmd_prefix} "echo \"${new_list}\">${FILENAME}"
	      cat ${WORKING_FILE}>${FILENAME}

              # remove any temporary files we created
              rm -f ${WORKING_FILE} ${BFF_LIST} ${MY_FILE}

           fi

           ${cmd_prefix} "gencopy -d $INPUT_SOURCE -f ${FILENAME} \
                                  -t $OUTPUT_DIR $TEMP_DIR $COPY_OPTIONS"
           rc=$?
       fi

       # remove temporary files we needed until last function call
       rm -f ${FILENAME}

       if [[ -n ${TARGET} ]]
       then
            # Since we just added software to an existing lpp_source,
            # issue a check operation and let its output, if any,
            # be sent to the working dialog.
            nim -Fo check ${TARGET}
       fi

       if [[ rc -eq 0 ]] && [[ ${CREATE_LPP_SOURCE} = "true" ]]
       then

           nim -o define -t lpp_source -a location=${OUTPUT_DIR} \
                        -a server=master ${LPP_SOURCE_NAME}
           ((rc+=$?))
       fi

       return $rc
}

# -----------------------------------------------------------------------
# NAME:    ShowLogsPanel
#
# PURPOSE: To show the contents of one or more of the following log files
#          located in /var/adm/ras.
#
#              installp.log (nim.installp)
#              devinst.log
#              bosinstlog
#              bootlog
#
# CALLED FROM: wsminstall.cpp
# -----------------------------------------------------------------------
ShowLogsPanel()
{
   show_installp_log="${1}"
   show_devinst_log="${2}"
   show_bosinst_log="${3}"
   show_boot_log="${4}"
   only_show_last_entry="${5}"

   INSTALLP_LOG=/var/adm/ras/nim.installp
   DEVINST_LOG=/var/adm/ras/devinst.log
   BOSINST_LOG=/var/adm/ras/bosinstlog
   BOOT_LOG=/var/adm/ras/bootlog

   SEPARATOR="_____________________________________________________________________________\n"
   RC=0

   #-----------------------------------------------------------------
   # If it hasn't already been done, link the nim.installp log to the 
   # installp.log file in /var/adm/ras.
   #-----------------------------------------------------------------
   if [ -f /var/adm/ras/nim.installp -a ! -f /var/adm/ras/installp.log ]; then
      ln /var/adm/ras/nim.installp /var/adm/ras/installp.log && \
         INSTALLP_LOG=/var/adm/ras/installp.log
   fi

   if [[ "$show_installp_log" = "true" ]]; then
      echo $SEPARATOR
      $DSPMSG -s $MSG winstsh.cat $INSTALLP_LOG_MSG "Installation and maintenance log  (/var/adm/ras/installp.log)\n"
      echo $SEPARATOR
      display_log $INSTALLP_LOG $only_show_last_entry
      ((RC+=$?))
      echo "\n\n\n"
   fi

   if [ "$show_devinst_log" = "true" ]; then
      echo $SEPARATOR
      $DSPMSG -s $MSG winstsh.cat $DEVINST_LOG_MSG "Device install log  (/var/adm/ras/devinst.log)\n"
      echo $SEPARATOR
      display_log $DEVINST_LOG $only_show_last_entry
      ((RC+=$?))
      echo "\n\n\n"
   fi

   if [ "$show_bosinst_log" = "true" ]; then
      echo $SEPARATOR
      $DSPMSG -s $MSG winstsh.cat $BOSINST_LOG_MSG "BOS install log  (/var/adm/ras/bosinstlog)\n"
      echo $SEPARATOR
      display_log $BOSINST_LOG alog
      ((RC+=$?))
      echo "\n\n\n"
   fi

   if [ "$show_boot_log" = "true" ]; then
      echo $SEPARATOR
      $DSPMSG -s $MSG winstsh.cat $BOOT_LOG_MSG "Boot log  (/var/adm/ras/bootlog)\n"
      echo $SEPARATOR
      display_log $BOOT_LOG alog
      ((RC+=$?))
   fi

   return $RC
}

# -----------------------------------------------------------------------
# NAME:    NIMinitPanel
#
# PURPOSE: To initialize a machine as a NIM client.
#
# CALLED FROM: wsminstall.cpp
# -----------------------------------------------------------------------
NIMinitPanel()
{
      machine_name="${1}"
      primary_if="${2}"
      master_hostname="${3}"
      comments="${4}"
      iplrom="${5}"
      rm_niminfo="${6}"
      nimreg_port="${7}"
      nimclient_port="${8}"

       #----------------------------------------------------------------
       # Remove /etc/niminfo if joining a different NIM environment.
       #----------------------------------------------------------------
       if [[ "${rm_niminfo}" = "true" ]] ; then
          rm /etc/niminfo
       fi

       LINE="-aname=${machine_name}"
       LINE="$LINE -amaster=${master_hostname}"

       # ----------------------------------------------------
       # Discover the appropriate Hardware Platform Type and
       # Network Boot Kernel for this client...
       # ----------------------------------------------------
       PLATFORM=`bootinfo -p`
       LINE="$LINE -aplatform=${PLATFORM}"

       if [[ `bootinfo -z` -eq 0 ]] ; then
          NETBOOT_KERNEL=up
       else
          NETBOOT_KERNEL=mp
       fi
       LINE="$LINE -anetboot_kernel=${NETBOOT_KERNEL}"

       # ------------------------------------------------------------
       # Strip off Description; "Token Ring Network Interface (tr0)"
       # ------------------------------------------------------------
       PIF_NAME=`echo ${primary_if} | cut -f2 -d"(" | cut -f1 -d")"`
       LINE="$LINE -apif_name=${PIF_NAME}"

       if [[ $PIF_NAME = tr* ]] ; then
          RING_SPEED=`mktcpip -S ${PIF_NAME} 2>&1 |
               awk 'BEGIN { RS="\n"; FS=":" }
               {
                 if (NR==2) print $10
               }'`
          LINE="$LINE -a ring_speed1=$RING_SPEED"
       else
          if [[ $PIF_NAME = e[nt]* ]] ; then
             CABLE_TYPE=`mktcpip -S ${PIF_NAME} 2>&1 | awk 'BEGIN { RS="\n"; FS=":" }
               {
                 if (NR==2) print $10
               }'`
             LINE="$LINE -acable_type1=$CABLE_TYPE"
          fi
       fi

       # ----------------------------------
       # Did they specify a IPL ROM device?
       # ----------------------------------
       if [[ -n "${iplrom}" ]] ; then
            ROM=`echo "${iplrom}" | cut -f2 -d"("`
            ROM=`echo "${ROM}" | cut -f1 -d")"`
            LINE="$LINE -aiplrom_emu=/dev/${ROM}"
       fi

       # -------------------------------------------------------------
       # If ${nimreg_port} and ${nimclient_port} are not set, the user 
       # didnt change the default port values.
       # -------------------------------------------------------------
       if [ "${nimreg_port}" != "1059" ] ; then
           LINE="$LINE -aregistration_port=${nimreg_port}"
       fi

       if [ "${nimclient_port}" != "1058" ] ; then
           LINE="$LINE -amaster_port=${nimclient_port}"
       fi

       # ---------------
       # Invoke niminit
       # ---------------
       niminit $LINE ${comments:+-a comments="${comments}"}
       return $?
}

# -----------------------------------------------------------------------
# NAME:    Diag_MaintBootPanel
#
# PURPOSE: To enable a machine for a NIM diagnostic or maintenance boot. 
#
# CALLED FROM: wsminstall.cpp
# -----------------------------------------------------------------------
Diag_MaintBootPanel()
{
      caller="${1}"
      target="${2}"
      spot="${3}"
      diag_boot="${4}"

      if [[ "${caller}" =  NIM ]] 
      then
         # Convert a list of targets to a machine group (if applicable)
         ck_mult_targets "${target}" true 
         RC=$?; [[ $RC -ne 0 ]] && return ${RC}

      else
	 NEW_TARGET=junk
         RC=0
      fi

      for tgt in ${NEW_TARGET}
      do
          print_mult_non_grp_progress ${tgt}
          if [[ "${diag_boot}" = "true" ]] 
          then
             if [[ ${caller} = NIM ]]
             then
                ${NIM} -o diag -a spot=${spot}  ${tgt}
             else
                ${NIMCLIENT} -o diag -a spot=${spot}
             fi
          else
             if [[ ${caller} = NIM ]]
             then
                ${NIM} -o maint_boot -a spot=${spot}  ${tgt}
             else
                ${NIMCLIENT} -o maint_boot -a spot=${spot}
             fi
          fi
          ((RC+=$?))
      done

      [[ ${caller} = NIM ]] && \
         # Remove any group target we may have created above
         rm_tmp_grp "${target}"

      return ${RC}
}

# -----------------------------------------------------------------------
# NAME:    NIMBosInstallPanel
#
# PURPOSE: To re-install the Base Operating System on the client system.
#          This is NOT called from the NIM application.
#
# CALLED FROM: wsminstall.cpp
# -----------------------------------------------------------------------
NIMBosInstallPanel()
{
      install_type="${1}"
      spot="${2}"
      lpp_source="${3}"
      mksysb="${4}"
      bosinst_data="${5}"
      image_data="${6}"
      script="${7}"
      resolv_conf="${8}"
      remain_client="${9}"
      preserve_res="${10}"
      boot_choice="${11}"
      install_software="${12}"
      bundles="${13}"
      filesets="${14}"
      commit="${15}"
      save_replaced="${16}"
      install_reqs="${17}"
      extend_fs="${18}"
      overwrite="${19}"
      verify="${20}"
      acceptlicense="${21}"

       if [ "${acceptlicense}" = "true" ]
       then
               acceptlicenses="yes"
       else
               acceptlicenses=""
       fi


      LINE="nimclient -o bos_inst -a source=$install_type -a spot=$spot -a lpp_source=$lpp_source"
      
      if [[ -n "${acceptlicenses}" ]] ; then
         LINE="$LINE -a accept_licenses=${acceptlicenses}"
      fi

      if [ "${install_type}" = "mksysb" ] ; then
         LINE="$LINE -a mksysb=$mksysb"
      fi

      if [[ -n "${bosinst_data}" ]] ; then
         LINE="$LINE -a bosinst_data=${bosinst_data}"
      fi

      if [[ -n "${image_data}" ]] ; then
         LINE="$LINE -a image_data=${image_data}"
      fi

      if [[ -n "${script}" ]] ; then
         LINE="$LINE -a script=${script}"
      fi

      if [[ -n "${resolv_conf}" ]] ; then
         LINE="$LINE -a resolv_conf=${resolv_conf}"
      fi

      if [[ "${remain_client}" = "false" ]] ; then
         LINE="$LINE -a no_nim_client=yes"
      fi

      if [[ "${preserve_res}" = "true" ]] ; then
         LINE="$LINE -a preserve_res=yes"
      fi

      # -------------------------------------------
      # Set the appropriate network boot setting...
      # -------------------------------------------
      case "${boot_choice}" in
          one)    # No options necessary
                  ;;
          two)    LINE="$LINE -a boot_client=no"
                  ;;
          three)  LINE="$LINE -a boot_client=no -a set_bootlist=yes"
                  ;;
      esac

      # ---------------------------------------------------------------
      # Did the user request to install additional software after the
      # BOS installation completes?  They can choose to install from
      # pre-configured system bundles or they can select from filesets.
      # ---------------------------------------------------------------
      if [[ ${install_software} != "no" ]] ; then
         METHOD=`echo ${install_software} | cut -f2 -d","`
         if [[ $METHOD = "bundle" ]] ; then
              LINE="$LINE ${bundles}"
         else
              FILESETS=${filesets}
         fi

	 FLAGS=
         # ----------------------------------
         # Set the appropriate installp flags
         # ----------------------------------
         if [ "${commit}" = "true" ] ; then
             FLAGS="c"

             if [ "${save_replaced}" = "false" ] ; then
                FLAGS="${FLAGS}N"
             fi
         fi

         if [ "${install_reqs}" = "true" ] ; then
             FLAGS="${FLAGS}g"
         fi

         if [ "${extend_fs}" = "true" ] ; then
             FLAGS="${FLAGS}X"
         fi

         if [ "${overwrite}" = "true" ] ; then
             FLAGS="${FLAGS}F"
         fi

         if [ "${verify}" = "true" ] ; then
             FLAGS="${FLAGS}v"
         fi


         LINE="$LINE -a installp_flags=-a${FLAGS}"
      fi

      # Run the command
      $LINE ${FILESETS:+-a filesets="${FILESETS}"}
      return $?
}

# -----------------------------------------------------------------------
# NAME:    NIMResetPanel
#
# PURPOSE: To reset the NIM state for the machine.
#
# CALLED FROM: wsminstall.cpp
# -----------------------------------------------------------------------
NIMResetPanel()
{
      deallocate_all="${1}"

      rc=0
      ${NIMCLIENT} -Fo reset
      (( rc += $? ))

      if [[ "${deallocate_all}" = "true" && ${rc} -eq 0 ]] ; then
          ${NIMCLIENT} -o deallocate -a subclass=all
          (( rc += $? ))
      fi
      return $rc
}

# -----------------------------------------------------------------------
# NAME:    ListSupplementalInfoPanel
#
# PURPOSE: To list any supplemental information given for a selected
#          product or package.
#
# CALLED FROM: wsminstall.cpp
# -----------------------------------------------------------------------
ListSupplementalInfoPanel()
{
      input_choice="${1}"
      list_all="${2}"
      file_name="${3}"
      device_type="${4}"

      if [[ "${device_type}" = "NETWORK" ]]
      then 
           if [[ "${list_all}" = "true" ]] ; then
                FILESETS="all"
           else
                FILESETS=`cat ${file_name}`
           fi

           ${NIMCLIENT} -o showres -a installp_flags=qi \
                        -a filesets="${FILESETS}" \
                        -a resource=${input_choice}
      else
           if [[ "${list_all}" = "true" ]] ; then
                 file_name="all"
           fi
 
           ${SM_INST} installp_cmd -i -d ${input_choice} -f ${file_name}
      fi
}

# -----------------------------------------------------------------------
# NAME:    FixInstallStatus
#
# PURPOSE: List Fix (APAR) status for a machine or NIM target
#
# CALLED FROM: wsminstall.cpp
# -----------------------------------------------------------------------
FixInstallStatus()
{
        fixes="${1}"
        show_symptom_text="${2}"
        show_filesets="${3}"
	caller="${4}"
	target="${5}"
        options=""
 
        if [[ "${show_symptom_text}" = "true" ]] ; then
           options="a"
        fi

        if [[ "${show_filesets}" = "true" ]] ; then
           options="${options}v"
        fi

	RC=0
	if [[ ${caller} = NIM ]]; then
           	# Convert a list of targets to a machine group (if applicable)
           	ck_mult_targets "${target}" true 
           	RC=$?; [[ $RC -ne 0 ]] && return ${RC}

           	for tgt in ${NEW_TARGET}
           	do
			print_mult_non_grp_progress ${tgt}
			nim -o fix_query \
				 ${options:+ -a fix_query_flags=${options}} \
				 ${fixes:+ -a fixes="${fixes}"} \
				${tgt}
			((RC+=$?))
		done

		# Remove any group target we may have created above
		rm_tmp_grp "${target}"

	else
        	instfix -i${options}k "${fixes}"
		RC=$?
	fi
	return $RC
}






#---------------------------------------------------------------------------
#
# NAME: ListInstalledLicenseAgreementsDialog
#
# PURPOSE: Lists the installed e-licenses for products and filessets.
#
# Calledfrom: InteractionCommands.cmd
#
#---------------------------------------------------------------------------
function ListInstalledLicenseAgreementsDialog {

softwareList="${1}"
licenseTxt="${2}"
flags="-l"
filesetList=""
productList=""
tmpName=""
filename=""

      RC=0
      if [[ ${licenseTxt} = "true" ]] then
        flags="${flags}v"
      fi


      if [[ ${softwareList} = "all" ]] then

        inulag ${flags}
        RC=$?
        return ${RC}


      else


        for filename in ${softwareList}
        do
  
         tmpName=${filename%\.*}

         #check on tmpName being a product name
         if [[ ${tmpName} = ${filename} ]] then

           var=`odmget product | grep "lpp_name = \"${tmpName}" |awk '{if(NR==1) {print $3}}'`
           var=${var%\"}
           var=${var#\"}
           var2=`odmget -q lpp_name=${var} product | awk '{if (NR==4) {print $3} }'`
          if [[ ${var2} != "\"\"" ]] then
           var2=${var2%\"}
           var2=${var2#\"}
           productList="${productList}","${var2}"
          fi

          #tmpName is a fileset
         else

          filesetList="${filesetList}","${filename}"

         fi
        done

         productList=${productList#\,}
         filesetList=${filesetList#\,}

        #inulag products
        if [[ ${productList} != "" ]] then
          inulag ${flags}p ${productList}
          RC=$?
          if [[ $RC -eq 1 ]] then
              RC=0
          fi
        fi


        #inulag filesets
        if [[ ${filesetList} != "" ]] then
           inulag ${flags}n ${filesetList}
           RC=$?
           if [[ $RC -eq 1 ]] then
                RC=0
           fi

        fi

       fi

      return ${RC}

}



#---------------------------------------------------------------------------
#
# NAME: ListLicenseAgrOnMediaDialog
#
# PURPOSE: Lists the e-licenses on media or in a directory.
#
# Calledfrom: InteractionCommands.cmd
#
#---------------------------------------------------------------------------
function ListLicenseAgrOnMediaDialog {

inputDevice="${1}"
FILESETS="${2:-all}"


      RC=0
      installp -aEpd "${inputDevice}" "${FILESETS}"
      RC=$?
      return $RC

}



#---------------------------------------------------------------------------
#
# NAME: CopySWToFileDialog
#
# PURPOSE: To Copy the Installed Software Listing to a file
#          Field will be separated by a , instead of :
#
# Calledfrom: InteractionCommands.cmd
#
#---------------------------------------------------------------------------
function CopySWToFileDialog {

targetDir="${1}"

  # create the unique identifier if it hasn't been set yet
   uniqueID="_$$."`date +"%Y.%m.%d.%T"`
   uniqueID=$( echo "${uniqueID}" | sed 's/:/_/g' )     # replace colons with underscores
   tmpFile="/tmp/.installSW"

   LANG=C lslpp -Lqac  >  ${tmpFile}${uniqueID}

   sed 's/:/,/g' ${tmpFile}${uniqueID}   >  ${targetDir}
   RC=$?

   rm ${tmpFile}${uniqueID} 2>/dev/null

   return $RC


}


#---------------------------------------------------------------------------
#
# NAME:  AltDiskInstallPanel
#
# CALLED FROM: wsmnim.cpp
#
# Performs a bos_inst operation on the target(s) selected.
#---------------------------------------------------------------------------

function AltDiskInstallPanel {

target="${1}"
lpp_source="${2}"
mksysb="${3}"
image_data="${4}"
script="${5}"
resolv_conf="${6}"
remain_client="${7}"
install_software="${8}"
bundles="${9}"
filesets="${10}"
commit="${11}"
no_save="${12}"
install_reqs="${13}"
extend_fs="${14}"
overwrite="${15}"
verify="${16}"
phase="${17}"
exclude_files="${18}"
set_bootlist="${19}"
reboot_now="${20}"
fixbundle="${21}"
fixes="${22}"
verbose_output="${23}"
debug_output="${24}"
target_disk="${25}"
licenseValue="${26}"

        FILESETS=""

        LINE="/usr/sbin/alt_disk_install"

        if [ -n "${mksysb}" ]
        then
                LINE="$LINE -d ${mksysb}"
	else
		LINE="$LINE -C"
        fi

        if [ -n "${phase}" ]
        then
                LINE="$LINE -P ${phase}"
        fi

        if [[ -n "${image_data}" ]]
        then
                LINE="$LINE -i ${image_data}"
        fi

        if [[ -n "${script}" ]]
        then
                LINE="$LINE -s ${script}"
        fi

        if [[ -n "${resolv_conf}" ]]
        then
                LINE="$LINE -R ${resolv_conf}"
        fi


        if [[ -n "${exclude_files}" ]]
        then
                LINE="$LINE -e ${exclude_files}"
        fi

	if [[ ${reboot_now} != no ]]
	then
		LINE="$LINE -r"
        elif [[ ${set_bootlist} = "no" ]]
        then
                LINE="$LINE -B"
	fi

        if [[ ${install_software} != "no" ]]
        then
        	if [[ -n ${lpp_source} ]]
        	then
                	LINE="$LINE -l ${lpp_source}"
        	fi

                METHOD=`echo ${install_software} | cut -f2 -d","`
                if [[ $METHOD = "bundle" ]]
                then
                        LINE="$LINE -b ${bundles}"
                fi
                if [[ $METHOD = "fileset" ]]
                then
                       LINE="$LINE -w ${filesets}"
                fi
                if [[ $METHOD = "fixbundle" ]]
                then
                        LINE="$LINE -f ${fixbundle}"
                fi
                if [[ $METHOD = "fix" ]]
                then
                        LINE="$LINE -F ${fixes}"
                fi


                FLAGS=
                #-------------------
                # set installp FLAGS
                #-------------------
                if [ "${commit}" = "true" ]
                then
                        FLAGS="c"

                        if [ "${no_save}" = "true" ]
                        then
                                FLAGS="${FLAGS}N"
                        fi
                fi

                if [ "${install_reqs}" = "true" ]
                then
                        FLAGS="${FLAGS}g"
                fi

                if [ "${extend_fs}" = "true" ]
                then
                        FLAGS="${FLAGS}X"
                fi

                if [ "${overwrite}" = "true" ]
                then
                        FLAGS="${FLAGS}F"
                fi

                if [ "${verify}" = "true" ]
                then
                        FLAGS="${FLAGS}v"
                fi

                if [ ${licenseValue} = "true" ]
                then
                        FLAGS="${FLAGS}Y"
                fi


                LINE="$LINE -I -a${FLAGS}"
	
	fi

        #User selected to install all the sw from the source and accept licenses
        if [[ ${install_software} = "no" ]] && [[ ${licenseValue} = "true" ]]
        then
                LINE="$LINE -I -aY -w ALL -l ${lpp_source}"
        fi

	if [[ ${verbose_output} != "no" ]]
	then 
       	 	LINE="$LINE -V"
	fi
	
	if [[ ${debug_output} != "no" ]]
	then
        	LINE="$LINE -D"
	fi

	LINE="$LINE ${target_disk}"

        echo $LINE
        wsmexec $LINE
	RC=$?
	return $RC
}

