#!/bin/sh
###############################################################################
# Install utility for IBM i
#
# Copyright IBM Corp. 2012
# The source code for this program is not published or other-
# wise divested of its trade secrets, irrespective of what has
# been deposited with the U.S. Copyright Office.
# 
# ----------------------------------------------------------------------------
#
# You can optionally run the _iPostInstallUtility script after unzipping the
# product archive file on IBM i. However, this step is only for administrative 
# installations. Do not use this command for private user installations.
# The administrator must have *ALLOBJ and *SECADM special authority to run this 
# script.
#
#

usage() {
  echo ""
  echo "Usage: _iPostInstallUtility [options]"
  echo ""
  echo "Options:"
  echo "  --action [SETUPNATIVE | POSTINSTALLEXIT | POSTFPEXIT ]"
  echo "  --installroot install_root"
  echo "  --userroot user_root"
  echo ""
  echo "The --installroot option is required by the"
  echo "POSTINSTALLEXIT and POSTFPEXIT actions for" 
  echo "InstallationManager type installs and is otherwise"   
  echo "ignored."
  echo "The --userroot option is required by the"
  echo "POSTINSTALLEXIT action for InstallationManager type"
  echo "installs and is otherwise ignored."
  exit 1
}

set_wlp_install_dir() {
        CUR_DIR=`pwd`
        WLP_DIR=`dirname ${0}`/../../../../
        cd "${WLP_DIR}"
        WLP_INSTALL_DIR=`pwd`
        cd "${CUR_DIR}"
}

setInDefaultEnv() {
  echo "Setting $1=$2 in ${WLP_INSTALL_DIR}/etc/default.env."
  echo "# _iPostInstallUtility:" >> ${WLP_INSTALL_DIR}/etc/default.env
  echo "$1=$2" >> ${WLP_INSTALL_DIR}/etc/default.env
}

##
## evalJavaPropsFile: Reads Java properties from file specified in $1,
## and for every property it replaces all "." with "_" in the property name, 
## sets the resulting named script variable to the property value and exports 
## the variable.
evalJavaPropsFile()
{
  if [ -f "$1" ]; then
    while read -r line; do
      case $line in
      \#*);;
      *=*)
        # Only accept alphanumeric variable names to avoid eval complexities.
        if var=`echo "$line" | sed -e 's/^\([a-zA-Z0-9_\.][a-zA-Z0-9_\.]*\)=.*/\1/'`; then
          var=`echo $var | sed -e 's/\./_/g'`
          value=\'`echo "$line" | sed -e 's/[^=]*=//' -e s/\'/\'\"\'\"\'/g`\'
          eval "$var=$value; export $var"
        fi
      esac
    done < "$1"
  fi
}

######################################################################
# post_fp_exit: Restore native objects after applying fixpacks
######################################################################
post_fp_exit() {

echo "Executing post_fp_exit"

#Load the location of the WAS_PRODUCT_LIB, as setup_native determines it dynamically
. ${WLP_INSTALL_DIR}/etc/default.env
PROD_LIB=$WAS_PRODUCT_LIB
echo "WLP install directory is ${WLP_INSTALL_DIR}"
echo "Product library is ${PROD_LIB}"

# Determine objects to restore
PROD_PGMS=$(ls ${WLP_INSTALL_DIR}/lib/native/os400/pgms)
PROD_SRVPGMS=$(ls ${WLP_INSTALL_DIR}/lib/native/os400/srvpgms)
ALL_PROD_PRODUCT_PGMS=$(ls ${WLP_INSTALL_DIR}/lib/native/os400/product/pgms)
PROD_PRODUCT_PGMS=""

for PGM1 in $ALL_PROD_PRODUCT_PGMS
  do
    case "${PGM1}" in
    'qwas01mx') if [ ${PRODUCT_EDITION} = "EXPRESS" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
      fi
    ;;
    'qwas02mx') if [ ${PRODUCT_EDITION} = "BASE" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
      fi
    ;;
    'qwas03mx') if [ ${PRODUCT_EDITION} = "ND" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
      fi
    ;;
    'qwas05mx') if [ ${PRODUCT_EDITION} = "LIBERTY_CORE" ] && [ ${IS_IM_INSTALL} ]; then
        if [ -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/QWAS05M.PRDLOD ]; then
           PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
        fi
      fi
    ;;
    *) PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
    ;;
    esac
  done

# 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 ${WLP_INSTALL_DIR}/lib/native/os400/pgms/$PGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${WLP_INSTALL_DIR}/lib/native/os400/pgms/${PGM}') 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}"
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 ${WLP_INSTALL_DIR}/lib/native/os400/product/pgms/$PRDPGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${WLP_INSTALL_DIR}/lib/native/os400/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 ${WLP_INSTALL_DIR}/lib/native/os400/srvpgms/$SRVPGM ...
    system "QSYS/CPYFRMSTMF FROMSTMF('${WLP_INSTALL_DIR}/lib/native/os400/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}"
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 ${WLP_INSTALL_DIR}/lib/native/os400/srvpgms/$SRVPGM ...
  system "QSYS/UPDSRVPGM SRVPGM(${PROD_LIB}/${SRVPGM}) MODULE(*NONE) SRVPGMLIB(${PROD_LIB})"
done

for PGM in ${PROD_PGMS} ; do
  echo Processing ${WLP_INSTALL_DIR}/lib/native/os400/pgms/$PGM ...
  system "QSYS/UPDPGM PGM(${PROD_LIB}/${PGM}) MODULE(*NONE) SRVPGMLIB(${PROD_LIB})"
done

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

# Set ownership and authorities on install dir
if [ ${IS_IM_INSTALL} ]; then
  /usr/bin/chown -R QSYS ${INSTALL_ROOT}
else
  /usr/bin/chown -R QSYS ${WLP_INSTALL_DIR}
  /usr/bin/chmod -R o=rx ${WLP_INSTALL_DIR}
fi

}


######################################################################
# setup_native: Restore native objects after installing
######################################################################
setup_native() {

echo "Executing setup_native"

SHR_PROD_LIB=QWAS9
CREATED_SHR_PROD_LIB=0
TMP_SF=QTMPWASSF
SAV_LIB=QWAS9

V6R1FRC="FRCOBJCVN(*YES *ALL)"

WAS_PRODUCT_LIB=

  # Create underlying product library for stuff common to all installs
  if [ -e /QSYS.LIB/${SHR_PROD_LIB}.LIB ]; then
    CREATED_SHR_PROD_LIB=1
    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
  
  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
  # 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

# Write the library info to the properties file
if [ -e ${WLP_INSTALL_DIR}/etc/default.env ]; then
  rm -rf ${WLP_INSTALL_DIR}/etc/default.env
else
  if [ ! -e ${WLP_INSTALL_DIR}/etc ]; then
    /usr/bin/mkdir -p ${WLP_INSTALL_DIR}/etc
  fi
fi
/usr/bin/touch -C 819 ${WLP_INSTALL_DIR}/etc/default.env
/usr/bin/chown QSYS ${WLP_INSTALL_DIR}/etc/default.env
echo "############# Do not alter or edit this file! #############" >> ${WLP_INSTALL_DIR}/etc/default.env
setInDefaultEnv WAS_SHARED_LIB "${SHR_PROD_LIB}" 
setInDefaultEnv WAS_PRODUCT_LIB "${PROD_LIB}"
setInDefaultEnv WAS_PRODUCT_LIB_PATH "/QSYS.LIB/${PROD_LIB}.LIB" 

# Determine objects to restore
PROD_PGMS=$(ls ${WLP_INSTALL_DIR}/lib/native/os400/pgms)
PROD_SRVPGMS=$(ls ${WLP_INSTALL_DIR}/lib/native/os400/srvpgms)
ALL_PROD_PRODUCT=$(ls ${WLP_INSTALL_DIR}/lib/native/os400/product)
ALL_PROD_PRODUCT_PGMS=$(ls ${WLP_INSTALL_DIR}/lib/native/os400/product/pgms)
PROD_PRODUCT_PGMS=""
PROD_PRODUCT=""

for PGM1 in $ALL_PROD_PRODUCT_PGMS
  do
    case "${PGM1}" in
    'qwas01mx') if [ ${PRODUCT_EDITION} = "EXPRESS" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
      fi
    ;;
    'qwas02mx') if [ ${PRODUCT_EDITION} = "BASE" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
      fi
    ;;
    'qwas03mx') if [ ${PRODUCT_EDITION} = "ND" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
      fi
    ;;
    'qwas05mx') if [ ${PRODUCT_EDITION} = "LIBERTY_CORE" ] && [ ${IS_IM_INSTALL} ]; then
        if [ ${CREATED_SHR_PROD_LIB} -eq 0 ]; then
           PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
	fi
      fi
    ;;
    *) PROD_PRODUCT_PGMS="${PROD_PRODUCT_PGMS} ${PGM1}"
    ;;
    esac
  done
for PGM2 in $ALL_PROD_PRODUCT
  do
    case "${PGM2}" in
    'qwas01') if [ ${PRODUCT_EDITION} = "EXPRESS" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT="${PROD_PRODUCT} ${PGM2}"
      fi
    ;;
    'qwas02') if [ ${PRODUCT_EDITION} = "BASE" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT="${PROD_PRODUCT} ${PGM2}"
      fi
    ;;
    'qwas03') if [ ${PRODUCT_EDITION} = "ND" ] && [ ${IS_IM_INSTALL} ]; then
        PROD_PRODUCT="${PROD_PRODUCT} ${PGM2}"
      fi
    ;;
    'qwas05') if [ ${PRODUCT_EDITION} = "LIBERTY_CORE" ] && [ ${IS_IM_INSTALL} ]; then
        if [ ${CREATED_SHR_PROD_LIB} -eq 0 ]; then
           PROD_PRODUCT="${PROD_PRODUCT} ${PGM2}"
	fi
      fi
    ;;
    'pgms')
    ;;
    *) PROD_PRODUCT="${PROD_PRODUCT} ${PGM2}"
    ;;
    esac
  done

# 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 ${WLP_INSTALL_DIR}/lib/native/os400/pgms/$PGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${WLP_INSTALL_DIR}/lib/native/os400/pgms/${PGM}') 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}"
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 ${WLP_INSTALL_DIR}/lib/native/os400/product/pgms/$PRDPGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${WLP_INSTALL_DIR}/lib/native/os400/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 ${WLP_INSTALL_DIR}/lib/native/os400/srvpgms/$SRVPGM ...
  system "QSYS/CPYFRMSTMF FROMSTMF('${WLP_INSTALL_DIR}/lib/native/os400/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}"
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 ${WLP_INSTALL_DIR}/lib/native/os400/product/${PRODPART}"
  else
  	echo Processing ${WLP_INSTALL_DIR}/lib/native/os400/product/$PRODPART ...
  	system "QSYS/CPYFRMSTMF FROMSTMF('${WLP_INSTALL_DIR}/lib/native/os400/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 ${WLP_INSTALL_DIR}/lib/native/os400/srvpgms/$SRVPGM ...
    system "QSYS/UPDSRVPGM SRVPGM(${PROD_LIB}/${SRVPGM}) MODULE(*NONE) SRVPGMLIB(${PROD_LIB})"
done

for PGM in ${PROD_PGMS} ; do
  echo Processing ${WLP_INSTALL_DIR}/lib/native/os400/pgms/$PGM ...
  system "QSYS/UPDPGM PGM(${PROD_LIB}/${PGM}) MODULE(*NONE) SRVPGMLIB(${PROD_LIB})"
done

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

if [ ${ERR_CHK} -eq 1 ]; then
  if [ ${ACTION} = "SETUPNATIVE" ]; then
    exit 1
  fi
fi

}

######################################################################
#post_install_exit: Create native objects and set authorities
######################################################################
post_install_exit() {

echo "Executing post_install_exit"

# Create the user directory
if [ ${IS_IM_INSTALL} ]; then
    if [ ! -e ${USER_ROOT} ]; then
      /usr/bin/mkdir -p ${USER_ROOT}
      /usr/bin/chown QSYS ${USER_ROOT}
      /usr/bin/chmod o=rx ${USER_ROOT}
    fi
    WLPUSRDIR=${USER_ROOT}/wlp/usr
    WLPOUTPUTDIR=${USER_ROOT}/wlp/output/servers
    if [ ! -e ${USER_ROOT}/wlp ]; then
      /usr/bin/mkdir ${USER_ROOT}/wlp
    fi
    if [ ! -e ${WLPUSRDIR} ]; then
      if [ -e ${WLP_INSTALL_DIR}/usr ]; then
	   cp -R ${WLP_INSTALL_DIR}/usr ${USER_ROOT}/wlp
      else /usr/bin/mkdir ${WLPUSRDIR}
      fi
    fi
    if [ ! -e ${WLPOUTPUTDIR} ]; then
      /usr/bin/mkdir -p ${WLPOUTPUTDIR}
    fi
    /usr/bin/chown -R QSYS ${USER_ROOT}/wlp
    /usr/bin/chmod -R 755 ${USER_ROOT}/wlp
fi

#Load the location of the WAS_PRODUCT_LIB, as setup_native determines it dynamically
. ${WLP_INSTALL_DIR}/etc/default.env
PROD_LIB=$WAS_PRODUCT_LIB
echo "WLP install directory is ${WLP_INSTALL_DIR}"
echo "Product library is ${PROD_LIB}"

# Register QEJBSVR with security function
# Allows server to run as QEJBSVR
system "QSYS/CALL PGM(${PROD_LIB}/QWAS00)"

# Add private authorities for product library parts
system "QSYS/GRTOBJAUT OBJ(${PROD_LIB}/QWLPSEC) OBJTYPE(*SRVPGM) USER(QEJBSVR) AUT(*USE)"
system "QSYS/GRTOBJAUT OBJ(${PROD_LIB}/QWLPCMN) OBJTYPE(*PGM) USER(QEJBSVR) AUT(*USE)"
system "QSYS/GRTOBJAUT OBJ(${PROD_LIB}/QWLPSTRSVR) OBJTYPE(*PGM) USER(QEJBSVR) AUT(*USE)"
system "QSYS/GRTOBJAUT OBJ(${PROD_LIB}/QWLPSTRSVR) OBJTYPE(*PGM) USER(*PUBLIC) AUT(*EXCLUDE)"

# Create jobd, jobq, sbsd, etc.
if [ ! -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/QWAS9.CLS ]; then
  system "QSYS/CRTCLS CLS(${SHR_PROD_LIB}/QWAS9) RUNPTY(20) TIMESLICE(2000) TEXT('WebSphere Application Server class')"
fi
system "QSYS/CHGOBJOWN  OBJ(${SHR_PROD_LIB}/QWAS9) OBJTYPE(*CLS) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"
if [ ! -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/QWASJOBQ.JOBQ ]; then
  system "QSYS/CRTJOBQ JOBQ(${SHR_PROD_LIB}/QWASJOBQ) TEXT('WebSphere Application Server job queue') OPRCTL(*YES)"
fi
system "QSYS/CHGOBJOWN  OBJ(${SHR_PROD_LIB}/QWASJOBQ) OBJTYPE(*JOBQ) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"
if [ ! -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/QWASOUTQ.OUTQ ]; then
  system "QSYS/CRTOUTQ OUTQ(${SHR_PROD_LIB}/QWASOUTQ) TEXT('WebSphere Application Server output queue')"
fi
system "QSYS/CHGOBJOWN  OBJ(${SHR_PROD_LIB}/QWASOUTQ) OBJTYPE(*OUTQ) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"
if [ ! -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/QWASJOBD.JOBD ]; then
  system "QSYS/CRTJOBD JOBD(${SHR_PROD_LIB}/QWASJOBD) JOBQ(${SHR_PROD_LIB}/QWASJOBQ) OUTQ(${SHR_PROD_LIB}/QWASOUTQ) TEXT('WebSphere Application Server job description') INLLIBL(*SYSVAL) LOG(4 0 *SECLVL) JOBMSGQFL(*WRAP) ALWMLTTHD(*YES)"
fi
system "QSYS/CHGOBJOWN  OBJ(${SHR_PROD_LIB}/QWASJOBD) OBJTYPE(*JOBD) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"
if [ ! -e /QSYS.LIB/${SHR_PROD_LIB}.LIB/QWAS9.SBSD ]; then
  system "QSYS/CRTSBSD SBSD(${SHR_PROD_LIB}/QWAS9) POOLS((1 *BASE)) MAXJOBS(*NOMAX) TEXT('WebSphere Application Server subsystem description')"
  system "QSYS/ADDRTGE SBSD(${SHR_PROD_LIB}/QWAS9) SEQNBR(9999) CMPVAL(*ANY) PGM(QSYS/QCMD)"
  system "QSYS/ADDJOBQE SBSD(${SHR_PROD_LIB}/QWAS9) JOBQ(${SHR_PROD_LIB}/QWASJOBQ) MAXACT(*NOMAX) SEQNBR(9999)"
fi
system "QSYS/CHGOBJOWN  OBJ(${SHR_PROD_LIB}/QWAS9) OBJTYPE(*SBSD) NEWOWN(QSYS) CUROWNAUT(*REVOKE)"

# Add private auths for QEJBSVR
system "QSYS/GRTOBJAUT OBJ(${SHR_PROD_LIB}/QWAS9) OBJTYPE(*SBSD) USER(QEJBSVR) AUT(*OBJMGT *OBJOPR *READ *EXECUTE)"
system "QSYS/GRTOBJAUT OBJ(${SHR_PROD_LIB}/QWASJOBD) OBJTYPE(*JOBD) USER(QEJBSVR) AUT(*OBJMGT *OBJOPR *READ *EXECUTE)"
system "QSYS/GRTOBJAUT OBJ(${SHR_PROD_LIB}/QWASJOBQ) OBJTYPE(*JOBQ) USER(QEJBSVR) AUT(*USE)"
system "QSYS/GRTOBJAUT OBJ(${SHR_PROD_LIB}/QWAS9) OBJTYPE(*CLS) USER(QEJBSVR) AUT(*USE)"

# Add service table entries
system "QSYS/ADDSRVTBLE SERVICE('was9-admin-ssl') PORT(9043) PROTOCOL('tcp') TEXT('Administrative console SSL port')"
system "QSYS/ADDSRVTBLE SERVICE('was9-inthttp') PORT(9080) PROTOCOL('tcp') TEXT('Internal HTTP port')"

# Set ownership and authorities on install dir
if [ ${IS_IM_INSTALL} ]; then
  /usr/bin/chown -R QSYS ${INSTALL_ROOT}
else
  /usr/bin/chown -R QSYS ${WLP_INSTALL_DIR}
  /usr/bin/chmod -R o=rx ${WLP_INSTALL_DIR}
fi

#Set WLP_DEFAULT_JAVA_HOME, WLP_DEFAULT_USER_DIR and WLP_DEFAULT_OUTPUT_DIR in default.env
#and set permissions for QEJBSVR
WLP_DEFAULT_JAVA_HOME=
WLP_DEFAULT_USER_DIR=
WLP_DEFAULT_OUTPUT_DIR=
#Source default.env if it exists, otherwise create it
if [ ! -e ${WLP_INSTALL_DIR}/etc/default.env ]; then
  if [ ! -e ${WLP_INSTALL_DIR}/etc ]; then
    /usr/bin/mkdir -p ${WLP_INSTALL_DIR}/etc
  fi
  /usr/bin/touch -C 819 ${WLP_INSTALL_DIR}/etc/default.env
  /usr/bin/chown QSYS ${WLP_INSTALL_DIR}/etc/default.env
else
  . ${WLP_INSTALL_DIR}/etc/default.env
fi

if [ ${WLP_DEFAULT_JAVA_HOME} ]; then
  if [ -e ${WLP_DEFAULT_JAVA_HOME} ]; then
    SET_VAR_IN_SERVER_ENV=
  fi
else
  setInDefaultEnv WLP_DEFAULT_JAVA_HOME $JAVA_LOCATION
fi

if [ ${WLPUSRDIR} ]; then
  if [ ${WLP_DEFAULT_USER_DIR} ]; then
      if [ -e ${WLPUSRDIR} ]; then
       if [ ! ${#WLPUSRDIR} = ${#WLP_DEFAULT_USER_DIR} ] || [ ! `echo ${WLP_DEFAULT_USER_DIR} | grep -i "${WLPUSRDIR}"` ]; then
          setInDefaultEnv WLP_DEFAULT_USER_DIR ${WLPUSRDIR}
        fi
      else
        echo "Error: Directory ${WLPUSRDIR} does not exist."
        ERR_CHK=1
      fi
  else
      if [ -e ${WLPUSRDIR} ]; then
        setInDefaultEnv WLP_DEFAULT_USER_DIR ${WLPUSRDIR}
      else
        echo "Error: Directory ${WLPUSRDIR} does not exist."
        ERR_CHK=1
      fi
  fi
fi

if [ ${WLPOUTPUTDIR} ]; then
  if [ ${WLP_DEFAULT_OUTPUT_DIR} ]; then
      if [ -e ${WLPOUTPUTDIR} ]; then
         if [ ! ${#WLP_DEFAULT_OUTPUT_DIR} = ${#WLPOUTPUTDIR} ] || [ ! `echo ${WLP_DEFAULT_OUTPUT_DIR} | grep -i "${WLPOUTPUTDIR}"` ]; then
          setInDefaultEnv WLP_DEFAULT_OUTPUT_DIR ${WLPOUTPUTDIR}
        fi
      else
        echo "Error: Directory ${WLPOUTPUTDIR} does not exist."
        ERR_CHK=1
      fi
  else
      if [ -e ${WLPOUTPUTDIR} ]; then
        setInDefaultEnv WLP_DEFAULT_OUTPUT_DIR ${WLPOUTPUTDIR}
      else
        echo "Error: Directory ${WLPOUTPUTDIR} does not exist."
        ERR_CHK=1
      fi
  fi
fi

${WLP_INSTALL_DIR}/lib/native/os400/bin/_iAuthUtility --rolename server --userprofilename QEJBSVR

if [ ${ERR_CHK} -eq 1 ]; then
  if [ ${ACTION} = "POSTINSTALLEXIT" ]; then
    exit 1
  fi
fi

}

######################################################################
#do_all_install: Execute all install actions
######################################################################
do_all_install() {
echo "Executing $ACTION"
  setup_native
  post_install_exit
}

######################################################################
#main
######################################################################

if [ "`uname`" != "OS400" ]; then
exit 1
fi

ACTION=DOALL
INSTALL_ROOT=
USER_ROOT=
ERR_CHK=0

while [ $# -gt 0 ]; do
  case $1 in
    '--action') if [ $# -gt 1 ]; then
	  ACTION=$2;shift
	fi
    ;;
    '--installroot') if [ $# -gt 1 ]; then
	  INSTALL_ROOT=$2;shift
	fi
    ;;
    '--userroot') if [ $# -gt 1 ]; then
	  USER_ROOT=$2;shift
	fi
    ;;
    *) usage
    ;;
  esac
  shift
done

set_wlp_install_dir
echo "WebSphere liberty profile install root is ${WLP_INSTALL_DIR}"
SHR_PROD_LIB=QWAS9
TMP_SF=QTMPWASSF
SAV_LIB=QWAS9
V6R1FRC="FRCOBJCVN(*YES *ALL)"

JAVA_LOCATION=/QOpenSys/QIBM/ProdData/JavaVM/jdk80/32bit

evalJavaPropsFile ${WLP_INSTALL_DIR}/lib/versions/WebSphereApplicationServer.properties
echo "Product edition is "${com_ibm_websphere_productEdition}
echo "Installation type is "${com_ibm_websphere_productInstallType}

IS_IM_INSTALL=
if [ ${com_ibm_websphere_productInstallType} = "InstallationManager" ]; then
  IS_IM_INSTALL=true
fi

PRODUCT_EDITION=${com_ibm_websphere_productEdition}

umask 022

case "$ACTION" in
  'DOALL') do_all_install
  ;;
  'SETUPNATIVE') setup_native
  ;;
  'POSTINSTALLEXIT') 
  if [ ${IS_IM_INSTALL} ]; then
    if [ ! ${INSTALL_ROOT} ]; then
      usage
    fi
    if [ ! ${USER_ROOT} ]; then
      usage
    fi
  fi
  post_install_exit
  ;;
  'POSTFPEXIT') 
  if [ ${IS_IM_INSTALL} ]; then
    if [ ! ${INSTALL_ROOT} ]; then
      usage
    fi
  fi
  post_fp_exit
  ;;
  *)
  usage
  ;;
esac

exit ${ERR_CHK}