#!/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 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 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
ERR_CHK=0
TMP_SF=QTMPWASSF
SAV_LIB=QWAS61
OSVersion=${OSVER:1:1}
OSRelease=${OSVER:3:1}
V6R1FRC=
if [ OSVersion -gt 5 ]; then
	V6R1FRC="FRCOBJCVN(*YES *ALL)"
fi
if [ OSVersion -eq 5 ] && [ OSRelease -gt 4 ]; then
	V6R1FRC="FRCOBJCVN(*YES *ALL)"
fi

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

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

#############################################################################################
# Create underlying product library for stuff common to all WAS V61 installs
#############################################################################################
if [ -e /QSYS.LIB/${V61_PROD_LIB}.LIB ]; then
  echo "V61 Product library, ${V61_PROD_LIB}, already exists"
else
  system "QSYS/CRTLIB LIB(${V61_PROD_LIB}) AUT(*USE)"
  ERR_CHK=$?
  if [ ${ERR_CHK} -ne 0 ]; then
    echo "Error: Failed to create library ${V61_PROD_LIB}"
    exit 1
  fi
  system "CHGOBJOWN OBJ(${V61_PROD_LIB}) OBJTYPE(*LIB) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"
fi

#############################################################################################
# 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

#############################################################################################
# Restore native parts from save files
#############################################################################################
PROD_PGMS=$(ls ${PROD_ROOT}/native/pgms)
PROD_SRVPGMS=$(ls ${PROD_ROOT}/native/srvpgms)
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)

# 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}) ${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(${V61_PROD_LIB}) ${V6R1FRC}"
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}) ${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 V61 common product library
for PRODPART in ${PROD_PRODUCT} ; do
  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(${V61_PROD_LIB}) ${V6R1FRC}"
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/${V61_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

# 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

# Set addiitonal authorities
chmod -R o=rx ${PROD_ROOT}
chmod -R o=  ${PROD_ROOT}/properties/WSCustomConstants.properties
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

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
# end of if new install
fi

if [ $# -eq 3 ]; then
  # We are handling an upgrade situation - restore the Base product load
  if [ -e ${PROD_ROOT}/native/product/qwas02 ]; then
    echo "Restoring QWAS02 product load"
    system "QSYS/CPYFRMSTMF FROMSTMF('${PROD_ROOT}/native/product/qwas02') 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}) ${V6R1FRC}"
  else
    echo "Product load QWAS02 not found"
  fi
fi

if [ ${ERR_CHK} -eq 1 ]; then
  exit 1
fi

exit 0