#!/bin/sh
###############################################################################
# WebSphere Application Server liberty admininstrative pre-uninstall script 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.
# 
# ----------------------------------------------------------------------------
#
# Run the _iPreUninstallUtility script before removing product files on IBM i. 
# However, this step is only for manual administrative "unzip" 
# installations. Do not use this command for manual developer installations.
# The administrator must have *ALLOBJ and *SECADM special authority to run this 
# script.
#

usage() {
  echo ""
  echo "Usage: _iPreUninstallUtility [options]"
  echo ""
  echo "Options:"
  echo "  --action [ DLTPRDLIB | DLTSHRPRDLIB ]"
  exit 1
}

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

delete_prod_lib() {
  echo "Executing delete_prod_lib"
  if [ -e ${WAS_PRODUCT_LIB_PATH} ]; then
    system "QSYS/DLTLIB LIB(${WAS_PRODUCT_LIB})"
    if [ $? -ne 0 ]; then
      ERR_CHK=1
    fi
  fi
}

delete_shared_prod_lib() {
  echo "Executing delete_shared_prod_lib"
	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 Z"

	PROD_LIB=${SHR_PROD_LIB}A
	DELETE_SHR_PRD_LIB=true
	for letter in $ALPHABET
	do
		if [ -e /QSYS.LIB/${PROD_LIB}.LIB ]; then
			DELETE_SHR_PRD_LIB=false
			echo "Library /QSYS.LIB/${PROD_LIB}.LIB exists, so the shared library will not be deleted"
		else
			echo "Library /QSYS.LIB/${PROD_LIB}.LIB does not exist"
		fi	
		PROD_LIB=${SHR_PROD_LIB}${letter}
	done

	if [ "$DELETE_SHR_PRD_LIB" = "true" ]; then
  	echo "Clearing output queue"
  	system "QSYS/CLROUTQ OUTQ(QWAS85/QWASOUTQ)"
      if [ $? -ne 0 ]; then
        ERR_CHK=1
      fi
  	echo "Removing TCP Service Table port entries"
  	system "QSYS/CALL PGM(QWAS85/QWASCLN)"
      if [ $? -ne 0 ]; then
        ERR_CHK=1
      fi
  	echo "Ending QWAS85 subsystem"
  	system "QSYS/ENDSBS SBS(QWAS85) OPTION(*IMMED)"
  	sleep 5
  	echo "Since this is the last product, we can safely remove the Base Product Load QWAS00M"
      export QIBM_MULTI_THREADED=N
      system "CALL QSYS/QSZDLTPL PARM('$1   QWAS85    ' '')"
      if [ $? -ne 0 ]; then
        ERR_CHK=1
      fi
  	system "QSYS/DLTLIB LIB(QWAS85)"
      if [ $? -ne 0 ]; then
        ERR_CHK=1
      fi
  else 
  	echo "Did not remove the shared product library (QWAS85) because other product installations are using the shared library"
	fi
}

do_default() {
#
  echo "Executing $ACTION"
  delete_prod_lib
  delete_shared_prod_lib
}


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

# Validate parameters
# Do the whole 9 yards if no parameters are passed.

ACTION=DODEFAULT
ERR_CHK=0

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

echo "Action = $ACTION"

SHR_PROD_LIB=QWAS85
set_wlp_install_dir

SCRIPT_DIR="${WLP_INSTALL_DIR}/bin"
SCRIPT_DIR_OWNER=`ls -ld ${SCRIPT_DIR} | awk '{ print $3 }'`
if [ ! ${SCRIPT_DIR_OWNER} = "QSYS" ]; then
  echo "Nothing to do because the \"iAdmin POSTINSTALL\" command has not been run."
  exit 0
fi

echo "WebSphere liberty profile install root is ${WLP_INSTALL_DIR}"
if [ ! -e ${WLP_INSTALL_DIR}/etc/default.env ]; then
  echo "Error: ${WLP_INSTALL_DIR}/etc/default.env does not exist"
  exit 1
fi
. ${WLP_INSTALL_DIR}/etc/default.env

WAS_INSTALL_ROOT=`dirname $WLP_INSTALL_DIR`
IS_IM_INSTALL=
if [ -e "${WAS_INSTALL_ROOT}/properties/version/was.product" ]; then
  IS_IM_INSTALL=true
fi


case "$ACTION" in
  'DODEFAULT') do_default
  ;;
  'DLTPRDLIB') delete_prod_lib
  ;;
  'DLTSHRPRDLIB') delete_shared_prod_lib
  ;;
  *)
  usage
  ;;
esac

exit ${ERR_CHK}