#!/usr/bin/qsh
# // 5639-D57, 5630-A36, 5630-A37, 5724-D18 (C) COPYRIGHT International Business Machines Corp. 1997, 2022

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


#USAGE FORMAT
# _preuninstall was.install.root DLTDIRS
# _preuninstall was.install.root DLTPRDLIB
# _preuninstall was.install.root DLTSHRPRDLIB
# _preuninstall was.install.root DLTPRDLOD was.install.os400.profile.location 
if [ ! $# -ge 2 ]; then
  echo "Error: Missing required parameter"
  echo "Usage: _preuninstall was.install.root option"
  exit 1
fi

PROD_ROOT=$1
SHR_PROD_LIB=QWAS9
OPTION=$2
. ${PROD_ROOT}/properties/os400.properties
. ${PROD_ROOT}/properties/productEdition.properties
PROD_LIB=${WAS_PRODUCT_LIB}

if [ ! $# -eq 2 ] && [ "${OPTION}" = "DLTDIRS" ]; then
  echo "Error: Incorrect usage with option DLTDIRS"
  echo "Usage: _preuninstall was.install.root DLTDIRS"
  exit 1
fi
if [ ! $# -eq 2 ] && [ "${OPTION}" = "DLTPRDLIB" ]; then
  echo "Error: Incorrect usage with option DLTPRDLIB"
  echo "Usage: _preuninstall was.install.root DLTPRDLIB"
  exit 1
fi
if [ ! $# -eq 2 ] && [ "${OPTION}" = "DLTSHRPRDLIB" ]; then
  echo "Error: Incorrect usage with option DLTSHRPRDLIB"
  echo "Usage: _preuninstall was.install.root DLTSHRPRDLIB"
  exit 1
fi
if [ ! $# -eq 3 ] && [ "${OPTION}" = "DLTPRDLOD" ]; then
  echo "Error: Incorrect usage with option DLTPRDLOD"
  echo "Usage: _preuninstall was.install.root DLTPRDLOD was.install.os400.profile.location"
  exit 1
fi


if [ "${OPTION}" = "DLTDIRS" ]; then
 if [ -e ${PROD_ROOT}/properties/product.properties ]; then
  system "RMVLNK OBJLNK('${PROD_ROOT}/properties/product.properties')"
 fi
 if [ -e ${PROD_ROOT}/java/tools/tools.jar ]; then
  system "RMVLNK OBJLNK('${PROD_ROOT}/java/tools/tools.jar')"
 fi
 if [ -e ${PROD_ROOT}/properties/version/history ]; then
  rm -R ${PROD_ROOT}/properties/version/history
 fi
 if [ -e ${PROD_ROOT}/derby/db2j.log ]; then
  rm -f ${PROD_ROOT}/derby/db2j.log
 fi
 if [ -e ${PROD_ROOT}/lib/ext ]; then
  rm -R ${PROD_ROOT}/lib/ext
 fi
 if [ -e ${PROD_ROOT}/deploytool/itp/configuration ]; then
  rm -R ${PROD_ROOT}/deploytool/itp/configuration
 fi
 if [ -e ${PROD_ROOT}/deploytool/itp/ejbdeploy ]; then
  system "RMVLNK OBJLNK('${PROD_ROOT}/deploytool/itp/ejbdeploy')"
 fi
 if [ -e ${PROD_ROOT}/deploytool/itp/setupCmdLine ]; then
  system "RMVLNK OBJLNK('${PROD_ROOT}/deploytool/itp/setupCmdLine')"
 fi
 if [ -e ${PROD_ROOT}/derby/bin/embedded/setupCmdLine ]; then
  system "RMVLNK OBJLNK('${PROD_ROOT}/derby/bin/embedded/setupCmdLine')"
 fi
 if [ -e ${PROD_ROOT}/derby/bin/networkServer/setupCmdLine ]; then
  system "RMVLNK OBJLNK('${PROD_ROOT}/derby/bin/networkServer/setupCmdLine')"
 fi
 if [ -e ${PROD_ROOT}/java/extj9/PD.jar ]; then
  system "RMVLNK OBJLNK('${PROD_ROOT}/java/extj9/PD.jar')"
 fi
fi

if [ "${OPTION}" = "DLTPRDLIB" ]; then
  system "QSYS/DLTLIB LIB(${PROD_LIB})"
fi
if [ "${OPTION}" = "DLTSHRPRDLIB" ]; then
	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, no deleting shared library"
		else
			echo "Library /QSYS.LIB/${PROD_LIB}.LIB does not exists"
		fi	
		PROD_LIB=${SHR_PROD_LIB}${letter}
	done

	if [ "$DELETE_SHR_PRD_LIB" = "true" ]; then
  	echo "Clearing output queue"
  	system "QSYS/CLROUTQ OUTQ(QWAS9/QWASOUTQ)"
  	echo "Removing TCP Service Table port entries"
  	system "QSYS/CALL PGM(QWAS9/QWASCLN)"
  	echo "Ending QWAS9 subsystem"
  	system "QSYS/ENDSBS SBS(QWAS9) OPTION(*IMMED)"
  	sleep 5
  	system "QSYS/DLTLIB LIB(QWAS9)"
  	echo "Since this is the last product, we can safely remove the Base Product Load QWAS00M"
  	${PROD_ROOT}/bin/deleteProdLod QWAS00M
  else 
  	echo "Do Nothing, there are libraries that may be using the shared library still"
	fi
fi

if [ "${OPTION}" = "DLTPRDLOD" ]; then
	APP_DATA_LOC=$3
	echo "IM App Data location used: ${APP_DATA_LOC}"
	if [ "$PRODUCT_EDITION" = "EXPRESS" ]; then
		grep package.*com.ibm.websphere.EXPRESS.v9 ${APP_DATA_LOC}/installed.xml > tmp
		grep package.*com.ibm.websphere.EXPRESSTRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.WEBENAB.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.EXPRESS.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.EXPRESSTRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.WEBENAB.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		PROD_LOAD=QWAS01M
		echo "Product Load = $PROD_LOAD"
	fi
	if [ "$PRODUCT_EDITION" = "BASE" ]; then
		grep package.*com.ibm.websphere.BASE.v9 ${APP_DATA_LOC}/installed.xml > tmp
		grep package.*com.ibm.websphere.BASETRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.DEVELOPERS.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.DEVELOPERSILAN.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.WEBENAB.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.BASE.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.BASETRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.DEVELOPERS.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.DEVELOPERSILAN.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.WEBENAB\' ${APP_DATA_LOC}/installed.xml >> tmp
		PROD_LOAD=QWAS02M
		echo "Product Load = $PROD_LOAD"
	fi
	if [ "$PRODUCT_EDITION" = "ND" ]; then
		grep package.*com.ibm.websphere.liberty.ND\' ${APP_DATA_LOC}/installed.xml > tmp
		grep package.*com.ibm.websphere.ND.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.NDTRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.ND.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		grep package.*com.ibm.websphere.liberty.NDTRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		PROD_LOAD=QWAS03M
		echo "Product Load = $PROD_LOAD"
	fi
	if [ "$PRODUCT_EDITION" = "NDDMZ" ]; then
		grep package.*com.ibm.websphere.NDDMZ.v9 ${APP_DATA_LOC}/installed.xml > tmp
		grep package.*com.ibm.websphere.NDDMZTRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		PROD_LOAD=QWAS04M
		echo "Product Load = $PROD_LOAD"
	fi
	if [ "$PRODUCT_EDITION" = "APPCLIENT" ]; then
		grep package.*com.ibm.websphere.APPCLIENT.v9 ${APP_DATA_LOC}/installed.xml > tmp
		grep package.*com.ibm.websphere.APPCLIENTTRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
        grep package.*com.ibm.websphere.APPCLIENTILAN.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		PROD_LOAD=QWAS06M
		echo "Product Load = $PROD_LOAD"
	fi
	if [ "$PRODUCT_EDITION" = "PLG" ]; then
		grep package.*com.ibm.websphere.PLG.v9 ${APP_DATA_LOC}/installed.xml > tmp
		grep package.*com.ibm.websphere.PLGTRIAL.v9 ${APP_DATA_LOC}/installed.xml >> tmp
        grep package.*com.ibm.websphere.PLGILAN.v9 ${APP_DATA_LOC}/installed.xml >> tmp
		PROD_LOAD=QWAS07M
		echo "Product Load = $PROD_LOAD"
	fi

  count=0
  if [ -e tmp ]; then
  	while read line   
		do  
			echo "$line"
		 	count=`expr $count + 1`
		done < tmp
		rm -f tmp
	
		echo "Total instances of package $PRODUCT_EDITION = $count"
	
		if [ $count -le 1 ]; then
			${PROD_ROOT}/bin/deleteProdLod ${PROD_LOAD}
		else
			echo "Not removing product load $PROD_LOAD because there are more than one instance of this offering"
		fi
	else 
		echo "Could not find any instance of the offering $PRODUCT_EDITION, doing nothing"
	fi
fi
