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

inherit default exceptor output;
inherit php-common-deploy ;
inherit ruby-common;
VERBOSE=0;
$PROGRAM 'unzip';

[[ -n "${WEBROOT}" ]] && [ ! -d "$WEBROOT" ] && mkdir -p ${WEBROOT};

[ -e "${MANAGE_CORE_PATH}/${COMPUTE_TYPE}"-deploy.lib ] && { include ${COMPUTE_TYPE}-deploy; }

function setContext(){
    local context=$1;
    log "context: $context" ;
}

function prepareContext(){
    local context=$1;
    if ! $GREP -q ${context} <<< "development production test" ; then
        writeJSONResponseErr "result=>4079" "message=>Invalid context name specified" ; exit 4079;
    fi
    if [[ "$UID" != '0' ]]; then
        sudo chown ${DATA_OWNER} /var/lib/jelastic/env
    fi
    echo "${context}" > /var/lib/jelastic/env;
    bundle_type=$context;
    APPWEBROOT=${WEBROOT}/ROOT/;
}

function deploy(){
    echo "Starting deploying application ..." >> $ACTIONS_LOG 2>&1;
    local package_url=$1;
    local context=$2;
    local ext=$3;
    clearCache;
    ensureFileCanBeDownloaded $package_url;
    prepareContext ${context} ;
    if [[ -f "${APPWEBROOT%/}" ]]
    then
        set -f
        rm -f "${APPWEBROOT%/}";
        set +f
    fi
    unpack $APPWEBROOT && echo "Application deployed successfully!" >> $ACTIONS_LOG 2>&1 || {  if [ "$context" != "ROOT" ];then rm -rf $APPWEBROOT 1>/dev/null 2>&1; fi;  writeJSONResponseErr "result=>4071" "message=>Cannot unpack package"; die -q; }
    shiftContentFromSubdirectory $APPWEBROOT;
    if [ "$context" != "ROOT" ]
    then
        setContext $context;
    fi
    finishDeploy;
}

function finishDeploy(){
    bundleInstall;
    updateOwnership $APPWEBROOT;
    clearCache;
    reloadServiceSilent ${SERVICE} || restartServiceSilent ${SERVICE};
}

function undeploy(){
    local context=$1;
#   if [[ ${COMPUTE_TYPE} == "apache-ruby" ]]
#   then
        shopt -s dotglob;
        set -f
        rm -Rf $WEBROOT/ROOT/* ;
        set +f
        shopt -u dotglob;
        return 0;
#   fi
}

function renameContext(){
    local newContext=$1;
    local oldContext=$2;
    if ! $GREP -q ${oldContext} <<< "development production test" ;then
            writeJSONResponseErr "result=>4079" "message=>Invalid context name specified: ${oldContext}" ; exit 1;
    fi
    if ! $GREP -q ${newContext} <<< "development production test" ; then
            writeJSONResponseErr "result=>4079" "message=>Invalid context name specified: ${newContext}" ; exit 1;
    fi

    echo "${newContext}" > /var/lib/jelastic/env;

    stopService ${SERVICE} > /dev/null 2>&1;

    bundleInstall

    startServiceSilent ${SERVICE} ;

    updateOwnership "$WEBROOT/ROOT"
}

function describeDeploy(){
    echo "deploy ruby application \n\t\t -u \t <package URL> \n\t\t -c \t <context> \n\t\t -e \t zip | tar | tar.gz | tar.bz";
}

function describeUndeploy(){
    echo "undeploy ruby application \n\t\t -c \t <context>";
}

function describeRename(){
    echo "rename ruby context \n\t\t -n \t <new context> \n\t\t -o \t <old context>\n\t\t -e \t <extension>";
}
