#!/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 "${JETTYDEPLOYLIB_VERSION:-}" ] && return 0;
JETTYDEPLOYLIB_VERSION="0.1";

include os string;

JETTY_HOME='/opt/jetty'
APPS='webapps'
TEMPLATES='templates'
CONTEXT_CONF_DIR='contexts'
WADI_TEMPLATE='replica.xml'
TMP_TEMPLATE='/tmp/wadi$$$.xml'
WORK_DIR="$JETTY_HOME/work"
$PROGRAM 'wget';
APPLICATION='jetty'

function applyWadiSettings(){

	local context=$1;
	WADICONF=$(basename ${context}.xml)

	if [[ -s "$JETTY_HOME/$TEMPLATES/$WADI_TEMPLATE" ]]
	then
		cp $JETTY_HOME/$TEMPLATES/$WADI_TEMPLATE $TMP_TEMPLATE
		if [ "$context" == "root" ]
		then
			$SED -i "s/CONTEXTPATH//g" $TMP_TEMPLATE
		else
			$SED -i "s/CONTEXTPATH/$context/g" $TMP_TEMPLATE
		fi
		RESBASE=$(echo ${APPS}/${context}.war | $SED 's,/,\\/,g')
		$SED -i "s/RESOURCEBASE/$RESBASE/g" $TMP_TEMPLATE
		mv $TMP_TEMPLATE ${JETTY_HOME}/${CONTEXT_CONF_DIR}/${WADICONF}
		chown $DATA_OWNER ${JETTY_HOME}/${CONTEXT_CONF_DIR}/${WADICONF}	2>>"$JEM_CALLS_LOG"
       	fi
}

function removeWadiSettings(){

    local context=$1;
    WADICONF=$(basename ${context}.xml)

    if [ -f "$JETTY_HOME/$CONTEXT_CONF_DIR/$WADICONF" ]
    then
        set -f
        rm -f $JETTY_HOME/$CONTEXT_CONF_DIR/$WADICONF
        set +f
    fi
}

function applyPreDeploy(){
    [ "$context" == "ROOT" ]  && context='root';
    if [[ -n $(find ${WEBROOT} -maxdepth 1 -iname ${context}.war) ]]
    then
        set -f
        rm -f ${WEBROOT}/${context}.war
        set -f
        rm -f ${WEBROOT}/$(toLowerCase ${context}.war)
        set -f
        rm -rf ${WORK_DIR}/Jetty_0_0_0_0_8080_${context}.war*
        set -f
        rm -rf ${WORK_DIR}/Jetty_0_0_0_0_8080_$(toLowerCase ${context}).war*
        set +f
    fi
    return 0;
}

function applyPostDeploy(){
	[ ${COMPUTE_TYPE_VERSION} == "6" ] && applyWadiSettings $context;
}

function applyPreUndeploy(){
	[ "$context" == "ROOT" ]  && context='root';
	return 0;
}

function applyPostUndeploy(){
    set -f
    rm -Rf ${WORK_DIR}/Jetty_0_0_0_0_8080_${context}*
    set -f
    rm -Rf ${WORK_DIR}/Jetty_0_0_0_0_8080_*/webapp/${context}.war ;
    set +f
    [ ${COMPUTE_TYPE_VERSION} == "6" ] && removeWadiSettings $context;
}

function applyPreRename(){
    [ "$oldContext" == "ROOT" ]  && oldContext='root';
    [ "$newContext" == "ROOT" ]  && newContext='root';
    return 0;
}

function applyPostRename(){
    set -f
    rm -Rf ${WORK_DIR}/Jetty_0_0_0_0_8080_${oldContext}.war*
    set -f
    rm -Rf ${WORK_DIR}/Jetty_0_0_0_0_8080_${oldContext}*
    set +f
    [ ${COMPUTE_TYPE_VERSION} == "6" ] &&  removeWadiSettings $oldContext;
    [ ${COMPUTE_TYPE_VERSION} == "6" ] &&  applyWadiSettings $newContext;
}
