#!/bin/bash

# Copyright: 2023 Virtuozzo International GmbH

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.



[ -n "${TOMCATDEPLOYLIB_VERSION:-}" ] && return 0;
TOMCATDEPLOYLIB_VERSION="0.1";

include os;

TOMCAT_HOME='/opt/tomcat/'

function applyPreDeploy(){
    
    if [ -e "$TOMCAT_HOME/work/Catalina" ]
    then 
        rm -fr $TOMCAT_HOME/work/Catalina
    fi
}

function applyPostDeploy(){

	if [ ! -d "$TOMCAT_HOME/temp" ]
    then 
        mkdir -p "$TOMCAT_HOME/temp";
        chown -R  $DATA_OWNER "$TOMCAT_HOME/temp" 2>>"$JEM_CALLS_LOG";
    fi
}

function applyPreUndeploy(){
	return 0;
}

function applyPostUndeploy(){
	rm -rf ${WEBROOT}/${context}
	rm -rf ${APPS_DIR}/${context}
}

function applyPreRename(){

	if [ -e "$TOMCAT_HOME/work/Catalina" ]
	then 
		rm -fr $TOMCAT_HOME/work/Catalina;
	fi
	if [ -e "$TOMCAT_HOME/conf/Catalina" ]
	then 
		rm -fr $TOMCAT_HOME/conf/Catalina;
	fi
	if [ -e "${WEBROOT}/$newContext" ]
        then
    	    rm -fr ${WEBROOT}/$newContext;
	fi
}

function applyPostRename(){
	shopt -s dotglob;
    	[ -d ${WEBROOT}/$oldContext ] && mv ${WEBROOT}/$oldContext ${WEBROOT}/$newContext;
    	[ -d ${APPS_DIR}/$oldContext ] && mv ${APPS_DIR}/$oldContext ${APPS_DIR}/$newContext;	
    shopt -u dotglob; 
}
