#!/usr/bin/qsh
# // 5639-D57, 5630-A36, 5630-A37, 5724-D18 (C) COPYRIGHT International Business Machines Corp. 1997, 2023
# // 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 product install.
# The purpose of the script is to create the product library, 
# various native objects and to restore the product programs and 
# service programs to the product library
#
# Usage: _setupNative was_install_root
#  where 
#    was_install_root is the IFS directory to which the product was installed
#############################################################################################

##
## restoreNativeParts: Restore native parts to libraries
restoreNativeParts()
{
PROD_PGMS=$(ls ${PROD_ROOT}/native/pgms)
PROD_SRVPGMS=$(ls ${PROD_ROOT}/native/srvpgms)
PROD_PRODUCT=
PROD_PRODUCT_PGMS=
PROD_PRODUCT_SRVPGMS=
if [ ${INSTALLING_TWAS} ]; then
  PROD_PRODUCT=$(ls ${PROD_ROOT}/native/product)
  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}) RSTLIB(${SHR_PROD_LIB}) ${V6R1FRC}"
  else
    system "QSYS/RSTOBJ OBJ(*ALL) SAVLIB(${SAV_LIB}) DEV(*SAVF) OBJTYPE(*ALL) SAVF(QGPL/${TMP_SF}) RSTLIB(${PROD_LIB}) ${V6R1FRC}"
  fi
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}) RSTLIB(${SHR_PROD_LIB}) ${V6R1FRC}"
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}) 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}) RSTLIB(${PROD_LIB}) ${V6R1FRC}"
  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}) ${V6R1FRC}"
  fi
done

# Restore product def/loads and message file objects - these go to the WAS shared common product library
for PRODPART in ${PROD_PRODUCT} ; do
  # Do not restore Product Loads if they already exist (from another install).  This will remove any PTFs applied for the product/option.
  if [ -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/${PRODPART}M.PRDLOD ]; then
	echo "Skipping ${PROD_ROOT}/native/product/${PRODPART}"
  else
  	echo Processing ${PROD_ROOT}/native/product/$PRODPART ...
  	system "QSYS/CPYFRMSTMF FROMSTMF('${PROD_ROOT}/native/product/${PRODPART}') 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(${SHR_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})"    
  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

# Verify existence of objects restored
for PGM in ${PROD_PGMS} ; do
  if [ "${PGM}" = "qwascln" ]; then
    if [ ! -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/${PGM}.PGM ]; then
      echo "Error: ${PROD_LIB}/${PGM} does not exist."
      ERR_CHK=1
    fi
  else 
    if [ ! -e /QSYS.LIB/${PROD_LIB}.LIB/${PGM}.PGM ]; then
      echo "Error: ${PROD_LIB}/${PGM} does not exist."
      ERR_CHK=1
    fi
  fi
done
for SRVPGM in ${PROD_SRVPGMS} ; do
  if [ "${SRVPGM}" = "NativeFile.SAVEFILE" ]; then
    if [ ! -e /QSYS.LIB/${PROD_LIB}.LIB/NATIVEFILE.SRVPGM ]; then
      echo "Error: ${PROD_LIB}/NATIVEFILE does not exist."
      ERR_CHK=1
    fi
  else
    if [ ! -e /QSYS.LIB/${PROD_LIB}.LIB/${SRVPGM}.SRVPGM ]; then
      echo "Error: ${PROD_LIB}/${SRVPGM} does not exist."
      ERR_CHK=1
    fi
  fi
done
#######################################
# End of restoreNativeParts()
#######################################
}

##
## setupNative4Twas:
setupNative4Twas()
{
#######################################
# Create underlying product library for stuff common to all installs
#######################################
if [ -e /QSYS.LIB/${SHR_PROD_LIB}.LIB ]; then
  echo "Shared Product library, ${SHR_PROD_LIB}, already exists"
else
  system "QSYS/CRTLIB LIB(${SHR_PROD_LIB}) AUT(*USE)"
  ERR_CHK=$?
  if [ ${ERR_CHK} -ne 0 ]; then
    echo "Error: Failed to create library ${SHR_PROD_LIB}"
    exit 1
  fi
  system "CHGOBJOWN OBJ(${SHR_PROD_LIB}) OBJTYPE(*LIB) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"
fi
#######################################
# Determine the name of the PROD_LIB to use for this install
#######################################
PROD_LIB_NUMBER=1
ALPHABET="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

PROD_LIB=${SHR_PROD_LIB}A
for letter in $ALPHABET
do
	if [ -e /QSYS.LIB/${PROD_LIB}.LIB ]; then
	PROD_LIB=${SHR_PROD_LIB}${letter}
	fi	
done
rm -rf ${PROD_ROOT}/properties/os400.properties
/usr/bin/touch -C 819 ${PROD_ROOT}/properties/os400.properties
echo "WAS_PRODUCT_LIB=${PROD_LIB}" >> ${PROD_ROOT}/properties/os400.properties
echo "Product Lib root is ${PROD_LIB}"
echo "WAS_PRODUCT_LIB_PATH=/QSYS.LIB/${PROD_LIB}.LIB" >> ${PROD_ROOT}/properties/os400.properties
echo "Product Lib Path is ${WAS_PRODUCT_LIB_PATH}"
#######################################
# Create product library for this install
#######################################
if [ -e /QSYS.LIB/${PROD_LIB}.LIB ]; then
  echo "Product library, ${PROD_LIB}, already exists"
else
  system "QSYS/CRTLIB LIB(${PROD_LIB}) AUT(*USE)"
  ERR_CHK=$?
  if [ ${ERR_CHK} -ne 0 ]; then
    echo "Error: Failed to create library ${PROD_LIB}"
    exit 1
  fi
  system "CHGOBJOWN OBJ(${PROD_LIB}) OBJTYPE(*LIB) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"
fi

restoreNativeParts

# Change CCSID on the message catalog files
if [ -e ${PROD_ROOT}/MRI/2924/wasmsg.cat ]; then
  setccsid 361 ${PROD_ROOT}/MRI/2924/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2929/wasmsg.cat ]; then
  setccsid 273  ${PROD_ROOT}/MRI/2929/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2928/wasmsg.cat ]; then
  setccsid 297 ${PROD_ROOT}/MRI/2928/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2986/wasmsg.cat ]; then
  setccsid 933 ${PROD_ROOT}/MRI/2986/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2987/wasmsg.cat ]; then
  setccsid 937 ${PROD_ROOT}/MRI/2987/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2932/wasmsg.cat ]; then
  setccsid 280 ${PROD_ROOT}/MRI/2932/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2980/wasmsg.cat ]; then
  setccsid 37 ${PROD_ROOT}/MRI/2980/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2931/wasmsg.cat ]; then
  setccsid 284 ${PROD_ROOT}/MRI/2931/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2962/wasmsg.cat ]; then
  setccsid 5035 ${PROD_ROOT}/MRI/2962/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2989/wasmsg.cat ]; then
  setccsid 935 ${PROD_ROOT}/MRI/2989/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2975/wasmsg.cat ]; then
  setccsid 870 ${PROD_ROOT}/MRI/2975/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2979/wasmsg.cat ]; then
  setccsid 1025 ${PROD_ROOT}/MRI/2979/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2976/wasmsg.cat ]; then
  setccsid 870 ${PROD_ROOT}/MRI/2976/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2978/wasmsg.cat ]; then
  setccsid 870 ${PROD_ROOT}/MRI/2978/wasmsg.cat
fi
if [ -e ${PROD_ROOT}/MRI/2992/wasmsg.cat ]; then
  setccsid 870 ${PROD_ROOT}/MRI/2992/wasmsg.cat
fi

# Set additional authorities
# Set by IM, not needed here, for performance
#chmod -R o=rx ${PROD_ROOT}
chown -R QSYS ${PROD_ROOT}
if [ -e ${PROD_ROOT}/systemapps ]; then
  chown -R QEJBSVR ${PROD_ROOT}/systemapps
fi
# Set by IM, not needed here, for performance
#if [ -e ${PROD_ROOT}/optionalLibraries ]; then
#  chmod -R o=rx ${PROD_ROOT}/optionalLibraries
#fi

system "CHGAUT OBJ('${PROD_ROOT}/bin') USER(QEJBSVR) DTAAUT(*RWX) OBJAUT(*ALL)"
# Only do this section if this is a server install
if [ ${INSTALLING_TWAS_SERVER} ]; 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

}
# End of setupNative4Twas

if [ ! $# -ge 1 ]; then
  echo "Error: Missing required parameter"
  echo "Usage: _setupNative was_install_root"
  exit 1
fi
PROD_ROOT=$1
#PROD_LIB Generated dynamically
SHR_PROD_LIB=QWAS9
ERR_CHK=0
TMP_SF=QTMPWASSF
SAV_LIB=QWAS9
V6R1FRC="FRCOBJCVN(*YES *ALL)"

echo "Product install root is ${PROD_ROOT}"

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

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

if [ $# -eq 1 ]; then
 # We are handling a new install case

  if [ ${INSTALLING_TWAS} ]; then
    setupNative4Twas
  else 
    chown -R QSYS ${PROD_ROOT}
  fi
  if [ ${INSTALLING_WLP} ]; then
    if [ ! ${INSTALLING_TWAS} ]; then
      rm -rf ${PROD_ROOT}/properties/os400.properties
    fi
    ${PROD_ROOT}/wlp/lib/native/os400/bin/_iPostInstallUtility --action SETUPNATIVE 
    if [ $? -ne 0 ]; then
      ERR_CHK=1
    fi
    if [ ! ${INSTALLING_TWAS} ]; then
      WAS_PRODUCT_LIB=
      . ${PROD_ROOT}/wlp/etc/default.env
      if [ ${WAS_PRODUCT_LIB} ]; then
        PROD_LIB=${WAS_PRODUCT_LIB}
        SHR_PROD_LIB=${WAS_SHARED_LIB}
        rm -rf ${PROD_ROOT}/properties/os400.properties
        /usr/bin/touch -C 819 ${PROD_ROOT}/properties/os400.properties
        chown QSYS ${PROD_ROOT}/properties/os400.properties
        chmod 755 ${PROD_ROOT}/properties/os400.properties
        echo "WAS_PRODUCT_LIB=${PROD_LIB}" >> ${PROD_ROOT}/properties/os400.properties
        echo "Product Lib root is ${PROD_LIB}"
        echo "WAS_PRODUCT_LIB_PATH=/QSYS.LIB/${PROD_LIB}.LIB" >> ${PROD_ROOT}/properties/os400.properties
        echo "Product Lib Path is ${WAS_PRODUCT_LIB_PATH}"
        restoreNativeParts
      else
        echo "Error: Cannot restore native objects because WAS_PRODUCT_LIB is not set in ${PROD_ROOT}/wlp/etc/default.env."
        ERR_CHK=1
      fi
    fi
  fi
# end of if new install
fi

exit ${ERR_CHK}
