#!/usr/bin/qsh
# // 5639-D57, 5630-A36, 5630-A37, 5724-D18 (C) COPYRIGHT International Business Machines Corp. 1997, 2005
# // 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 inovked 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: _setupNative was_install_root product_library
#  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 2 ]; then
  echo "Error: Missing required parameter"
  echo "Usage: _setupNative was_install_root product_library"
  exit 1
fi

PROD_ROOT=$1
PROD_LIB=$2
V61_PROD_LIB=QWAS61
TMP_SF=QTMPWASSF
SAV_LIB=QWAS61
V5R5FRC=
if [ "${OSVER}" = "V5R5M0" ]; then
  V5R5FRC="FRCOBJCVN(*YES *ALL)"
fi

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

#############################################################################################
# Restore native parts from save files
#############################################################################################
PROD_PGMS=$(ls ${PROD_ROOT}/native/pgms)
PROD_SRVPGMS=$(ls ${PROD_ROOT}/native/srvpgms)
PROD_PRODUCT_PGMS=$(ls ${PROD_ROOT}/native/product/pgms)

# 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}) RSTLIB(${V61_PROD_LIB}) ${V5R5FRC}"
  else
    system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(${SAV_LIB}) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) RSTLIB(${PROD_LIB}) ${V5R5FRC}"
  fi
done

# Restore shared product program objects - these go to the sahred 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}) RSTLIB(${V61_PROD_LIB}) ${V5R5FRC}"
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}) RSTLIB(${PROD_LIB}) ${V5R5FRC}"
  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}) RSTLIB(${PROD_LIB}) ${V5R5FRC}"
  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})"    
  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 addiitonal authorities
chmod -R o=rx ${PROD_ROOT}
chown -R QSYS ${PROD_ROOT}
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
system "CHGAUT OBJ('${PROD_ROOT}/bin') USER(QEJBSVR) DTAAUT(*RWX) OBJAUT(*ALL)"
# Only do this section if this is a server install
if [ -e ${PROD_ROOT}/classes/properties/os400.systemlaunch.properties ]; then
  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
# end of if server install
fi
exit 0