#!/bin/bash
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
#
# This script is run by rpm before the rpm is removed 
# It should do any special steps that need to be done for the core 
# components for a plain uninstall or the uninstall part of an upgrade.
# Usage: preuninstall install_flag
# Where install_flag is a number that tells you if this is a simple uninstall
# or an upgrade. 
# install_flag == 1 - Upgrade
# install_flag == 0 - Uninstall

#
# Exit codes
#
OK=0
FAIL=1

#
# Check we have the necessary arguments.
# 

if [ $# -lt 1 -o $# -gt 1 ]
then
    echo "Usage: $0 install_flag"
    exit $FAIL
fi

if [ -z "${TOMCAT_DIR}" ]
then
    echo "TOMCAT_DIR not set"
    exit $FAIL
fi

INSTALL_FLAG="$1"
UPGRADE="false"

if [ ${INSTALL_FLAG} -ne 0 -a ${INSTALL_FLAG} -ne 1 ]
then
    echo "Invalid install_flag: ${INSTALL_FLAG}"
    exit $FAIL
fi

if [ ${INSTALL_FLAG} -eq 1 ]
then
    UPGRADE="true"
fi

if [ "${UPGRADE}" == "false" ]
then
    # This is an uninstall, remove webserver content
    
    if [ -d "${TOMCAT_DIR}/work/Catalina" ]
    then
        # Remove runtime content from tomcat work directory
        echo "Removing content from tomcat work directory"
        rm -rf "${TOMCAT_DIR}/work/Catalina"
    fi
fi
