#!/usr/bin/qsh
# // 5639-D57, 5630-A36, 5630-A37, 5724-D18 (C) COPYRIGHT International Business Machines Corp. 1997, 2021
# // 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.
#
#############################################################################################
# This script is invoked by the configuration manager during the
# config action phase of the fix pack install.
# The purpose of the script is to restore the product programs and 
# service programs to the product library
#
# Usage: _postfpexit was_install_root [cim_repository_root]
#  where 
#    was_install_root is the IFS directory to which the product was installed
#    product_library is the name of the library to create to hold product native objects
#############################################################################################

if [ ! $# -ge 1 ]; then
  echo "Error: Missing required parameter"
  echo "Usage: _postfpexit was_install_root [cim_repository_root]"
  exit 1
fi

PROD_ROOT=$1

UPDATING_TWAS=
UPDATING_TWAS_SERVER=
if [ -d ${PROD_ROOT}/profileTemplates ]; then
  UPDATING_TWAS=true
  if [ -d ${PROD_ROOT}/profileTemplates/management ]; then
    UPDATING_TWAS_SERVER=true
  fi
fi

UPDATING_WLP=
if [ -f ${PROD_ROOT}/wlp/lib/native/os400/bin/_iPostInstallUtility ]; then
  UPDATING_WLP=true
fi

. ${PROD_ROOT}/properties/os400.properties
PROD_LIB=${WAS_PRODUCT_LIB}
CIM_ROOT=$2
SHR_PROD_LIB=QWAS9
TMP_SF=QTMPWASSF
SAV_LIB=QWAS9

V6R1FRC="FRCOBJCVN(*YES *ALL)"

echo "Product install root is ${PROD_ROOT}"
echo "Product library is ${PROD_LIB}"


#############################################################################################
# Restore native parts from save files
#############################################################################################
PROD_PRODUCT_PGMS=
PROD_PRODUCT_SRVPGMS=
PROD_PGMS=$(ls ${PROD_ROOT}/native/pgms)
PROD_SRVPGMS=$(ls ${PROD_ROOT}/native/srvpgms)
if [ ${UPDATING_TWAS} ]; then
  PROD_PRODUCT_PGMS=$(ls ${PROD_ROOT}/native/product/pgms)
  PROD_PRODUCT_SRVPGMS=$(ls ${PROD_ROOT}/native/product/srvpgms)
fi

# Create temporary save file
system "QSYS/CRTSAVF FILE(QGPL/${TMP_SF})"

# Restore program objects - these go to the product library for the install
for PGM in ${PROD_PGMS} ; do
  echo Processing ${PROD_ROOT}/native/pgms/$PGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${PROD_ROOT}/native/pgms/${PGM}') TOMBR('/QSYS.LIB/QGPL.LIB/${TMP_SF}.FILE') MBROPT(*REPLACE)"
  if [ "${PGM}" = "qwascln" ]; then
    system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(${SAV_LIB}) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) ALWOBJDIF(*OWNER) RSTLIB(${SHR_PROD_LIB}) ${V6R1FRC}"
  else
    system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(${SAV_LIB}) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) ALWOBJDIF(*OWNER) RSTLIB(${PROD_LIB}) ${V6R1FRC}"
  fi
done

# Restore shared product program objects - these go to the shared product library for the install
for PRDPGM in ${PROD_PRODUCT_PGMS} ; do
  echo Processing ${PROD_ROOT}/native/product/pgms/$PRDPGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${PROD_ROOT}/native/product/pgms/${PRDPGM}') TOMBR('/QSYS.LIB/QGPL.LIB/${TMP_SF}.FILE') MBROPT(*REPLACE)"
  system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(${SAV_LIB}) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) ALWOBJDIF(*OWNER) RSTLIB(${SHR_PROD_LIB}) ${V6R1FRC}"
done

# Restore shared product service program objects - these go to the shared product library for the install
for PRDSRVPGM in ${PROD_PRODUCT_SRVPGMS} ; do
  echo Processing ${PROD_ROOT}/native/product/srvpgms/$PRDSRVPGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${PROD_ROOT}/native/product/srvpgms/${PRDSRVPGM}') TOMBR('/QSYS.LIB/QGPL.LIB/${TMP_SF}.FILE') MBROPT(*REPLACE)"
  system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(${SAV_LIB}) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) ALWOBJDIF(*OWNER) RSTLIB(${SHR_PROD_LIB}) ${V6R1FRC}"
done

# Restore service program objects - these go to the product library for the install
for SRVPGM in ${PROD_SRVPGMS} ; do
  echo Processing ${PROD_ROOT}/native/srvpgms/$SRVPGM ...
  if [ "${SRVPGM}" = "NativeFile.SAVEFILE" ]; then
    system "QSYS/CPYFRMSTMF FROMSTMF('${PROD_ROOT}/native/srvpgms/${SRVPGM}') TOMBR('/QSYS.LIB/QGPL.LIB/${TMP_SF}.FILE') MBROPT(*REPLACE)"
    system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(QNATVF) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) ALWOBJDIF(*OWNER) RSTLIB(${PROD_LIB}) ${V6R1FRC}"
  elif [ "${SRVPGM}" = "qsvtap24" ] && ([ "${QSH_VERSION:0:4}" = "V6R1" ] || [ "${QSH_VERSION:0:4}" = "V7R1" ]); then
     echo "${PROD_ROOT}/native/srvpgms/$SRVPGM will not be restored on ${QSH_VERSION} due to apache 2.4/2.2 incompatibility."
  elif [ "${SRVPGM}" = "qsvtap22" ] && [ "${QSH_VERSION:0:4}" != "V6R1" ] && [ "${QSH_VERSION:0:4}" != "V7R1" ]; then
     echo "${PROD_ROOT}/native/srvpgms/$SRVPGM will not be restored on ${QSH_VERSION} due to apache 2.4/2.2 incompatibility."
  elif [ "${SRVPGM}" = "qsvtap22lf" ] && [ "${QSH_VERSION:0:4}" != "V6R1" ] && [ "${QSH_VERSION:0:4}" != "V7R1" ]; then
     echo "${PROD_ROOT}/native/srvpgms/$SRVPGM will not be restored on ${QSH_VERSION} due to apache 2.4/2.2 incompatibility."
  else
    system "QSYS/CPYFRMSTMF FROMSTMF('${PROD_ROOT}/native/srvpgms/${SRVPGM}') TOMBR('/QSYS.LIB/QGPL.LIB/${TMP_SF}.FILE') MBROPT(*REPLACE)"
    system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(${SAV_LIB}) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) ALWOBJDIF(*OWNER) RSTLIB(${PROD_LIB}) ${V6R1FRC}"
  fi
done

# Delete temporary save file
system "QSYS/DLTF FILE(QGPL/${TMP_SF})"

# Update the *PGM and *SRVPGM to be bound to product library for this install
# This must be done after all *PGMs and *SRVPGMs restored to avoid dependency problems
for SRVPGM in ${PROD_SRVPGMS} ; do
  echo Processing ${PROD_ROOT}/native/srvpgms/$SRVPGM ...
  if [ "${SRVPGM}" = "NativeFile.SAVEFILE" ]; then
    system "QSYS/UPDSRVPGM SRVPGM(${PROD_LIB}/NATIVEFILE) MODULE(*NONE) SRVPGMLIB(${PROD_LIB})"
  elif [ "${SRVPGM}" = "qsvtap24" ] && ([ "${QSH_VERSION:0:4}" = "V6R1" ] || [ "${QSH_VERSION:0:4}" = "V7R1" ]); then
     echo "${PROD_ROOT}/native/srvpgms/$SRVPGM will not be updated on ${QSH_VERSION} due to apache 2.4/2.2 incompatibility."
  elif [ "${SRVPGM}" = "qsvtap22" ] && [ "${QSH_VERSION:0:4}" != "V6R1" ] && [ "${QSH_VERSION:0:4}" != "V7R1" ]; then
     echo "${PROD_ROOT}/native/srvpgms/$SRVPGM will not be updated on ${QSH_VERSION} due to apache 2.4/2.2 incompatibility."
  elif [ "${SRVPGM}" = "qsvtap22lf" ] && [ "${QSH_VERSION:0:4}" != "V6R1" ] && [ "${QSH_VERSION:0:4}" != "V7R1" ]; then
     echo "${PROD_ROOT}/native/srvpgms/$SRVPGM will not be updated on ${QSH_VERSION} due to apache 2.4/2.2 incompatibility."
  else
    system "QSYS/UPDSRVPGM SRVPGM(${PROD_LIB}/${SRVPGM}) MODULE(*NONE) SRVPGMLIB(${PROD_LIB})"
  fi
done

for PGM in ${PROD_PGMS} ; do
  echo Processing ${PROD_ROOT}/native/pgms/$PGM ...
  if [ ! "${PGM}" = "qwascln" ]; then
    system "QSYS/UPDPGM PGM(${PROD_LIB}/${PGM}) MODULE(*NONE) SRVPGMLIB(${PROD_LIB})"
  fi
done

# Set additional authorities

chown -R QSYS ${PROD_ROOT}
. $(/usr/bin/dirname $0)/setupCmdLine
if [ -e ${WAS_USERDATA_ROOT}/profiles ]; then
  chown -R QEJBSVR ${WAS_USERDATA_ROOT}/profiles
fi
if [ -e ${WAS_USERDATA_ROOT}/profileRegistry ]; then
  chown -R QEJBSVR ${WAS_USERDATA_ROOT}/profileRegistry
fi
if [ -e ${PROD_ROOT}/systemapps ]; then
  chown -R QEJBSVR ${PROD_ROOT}/systemapps
fi
if [ -e ${PROD_ROOT}/optionalLibraries ]; then
  chmod -R o=rwx ${PROD_ROOT}/optionalLibraries
fi
if [ -e ${PROD_ROOT}/deploytool ]; then
  chown -R QEJBSVR ${PROD_ROOT}/deploytool
fi
if [ -e ${PROD_ROOT}/configuration ]; then
  chown -R QEJBSVR ${PROD_ROOT}/configuration
fi
if [ -e ${PROD_ROOT}/derby ]; then
  system "QSYS/CHGAUT OBJ('${PROD_ROOT}/derby') USER(QEJBSVR) DTAAUT(*RWX) OBJAUT(*ALL)"
fi
if [ -e ${PROD_ROOT}/derby/derby.log ]; then
  system "QSYS/CHGAUT OBJ('${PROD_ROOT}/derby/derby.log') USER(QEJBSVR) DTAAUT(*RWX) OBJAUT(*ALL)"
fi
system "CHGAUT OBJ('${PROD_ROOT}/bin') USER(QEJBSVR) DTAAUT(*RWX) OBJAUT(*ALL)"
# Only do this section if this is a server install
#ldh: Start changes for defect 709648
if [ ${UPDATING_TWAS_SERVER} ]; then
  chmod o= ${PROD_ROOT}/bin/logViewer
  system "CHGAUT OBJ('${PROD_ROOT}/bin/logViewer') USER(QEJBSVR) DTAAUT(*RX)"
#ldh: End changes for defect 709648
  chmod o= ${PROD_ROOT}/bin/addNode
  system "CHGAUT OBJ('${PROD_ROOT}/bin/addNode') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/backupConfig
  system "CHGAUT OBJ('${PROD_ROOT}/bin/backupConfig') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/cancelEJBTimers
  chmod o= ${PROD_ROOT}/bin/chgwassvr
  chmod o= ${PROD_ROOT}/bin/dspwasinst
  chmod o= ${PROD_ROOT}/bin/enbprfwas
  chmod o= ${PROD_ROOT}/bin/GenPluginCfg
  system "CHGAUT OBJ('${PROD_ROOT}/bin/GenPluginCfg') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/grtwasaut
  chmod o= ${PROD_ROOT}/bin/iscdeploy
  chmod o= ${PROD_ROOT}/bin/removeNode
  system "CHGAUT OBJ('${PROD_ROOT}/bin/removeNode') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/restoreConfig
  chmod o= ${PROD_ROOT}/bin/rvkwasaut
  chmod o= ${PROD_ROOT}/bin/stopNode
  system "CHGAUT OBJ('${PROD_ROOT}/bin/stopNode') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/stopServer
  system "CHGAUT OBJ('${PROD_ROOT}/bin/stopServer') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/svrsslcfg
  chmod o= ${PROD_ROOT}/bin/syncNode
  system "CHGAUT OBJ('${PROD_ROOT}/bin/syncNode') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/updwashost
  chmod o= ${PROD_ROOT}/bin/wsadmin
  system "CHGAUT OBJ('${PROD_ROOT}/bin/wsadmin') USER(QEJBSVR) DTAAUT(*RX)"
  chmod o= ${PROD_ROOT}/bin/wsdeploy
  chmod o= ${PROD_ROOT}/bin/PropFilePasswordEncoder
  if [ -e ${PROD_ROOT}/bin/stopManager ]; then
    chmod o= ${PROD_ROOT}/bin/stopManager
    system "CHGAUT OBJ('${PROD_ROOT}/bin/stopManager') USER(QEJBSVR) DTAAUT(*RX)"
    chmod o= ${PROD_ROOT}/bin/cleanupNode
    system "CHGAUT OBJ('${PROD_ROOT}/bin/cleanupNode') USER(QEJBSVR) DTAAUT(*RX)"
  fi
  if [ "${CIM_ROOT}" == "" ]; then
     
  else
      if [ -e ${CIM_ROOT} ]; then
        system "CHGAUT OBJ('${CIM_ROOT}') USER(QEJBSVR) DTAAUT(*RWX) OBJAUT(*ALL)"
      fi
  fi
# end of if server install
fi

if [ ${UPDATING_WLP} ]; then
  ${PROD_ROOT}/wlp/lib/native/os400/bin/_iPostInstallUtility --action POSTFPEXIT 
fi

# Set LOGOUTPUT to *JOBEND in ${SHR_PROD_LIB}/QWASJOBD
if [ -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/QWASJOBD.JOBD ]; then
  system "CHGJOBD JOBD(${SHR_PROD_LIB}/QWASJOBD) LOGOUTPUT(*JOBEND)"
fi  

exit 0
