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

include os;


function deploy(){
        $glassfish_tools -m deploy -p $package_url -c $context -e war > /dev/null 2>&1;
        result=$?;
        if [ "$result" -ne 0 ] 
        then 
                case "$result" in
                  156)
	              echo "Error loading file from URL" >> $ACTIONS_LOG
	              ;;
                   56)
            	      echo "Cannot unpack package" >>  $ACTIONS_LOG
            	      ;;
                esac
        else
                echo "Application deployed successfully" >> $ACTIONS_LOG
        fi
	return $result
}

function undeploy(){

        $glassfish_tools -m undeploy -c $context -e war;

}

function renameContext(){
        warurl=$(cat "/tmp/$oldContext.url");
        $glassfish_tools -m undeploy -c $oldContext -e war > /dev/null 2>&1;
        [ -a "/tmp/${oldContext}.war" ] && rm "/tmp/$oldContext.war";
        [ -a "/tmp/${oldContext}.url" ] && rm "/tmp/$oldContext.url";
        $glassfish_tools -m deploy -p $warurl -c $newContext -e war > /dev/null 2>&1;
}
