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

inherit exceptor;
$PROGRAM 'unzip';

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


function deploy(){
    echo "Starting deploying application ..." >> $ACTIONS_LOG 2>&1;
    local package_url=$1;
    local context=$2;
    local ext=$3;
    [ ! -d "$DOWNLOADS" ] && { mkdir -p $DOWNLOADS; }
    clearCache;
    ensureFileCanBeDownloaded $package_url;
    $WGET --content-disposition --no-check-certificate --directory-prefix=$DOWNLOADS $package_url >> $ACTIONS_LOG 2>&1;
    [ $? -gt 0 ] && { writeJSONResponseErr "result=>4078" "message=>Error loading file from URL" ; die -q; };
    package_name=$(ls ${DOWNLOADS});
    [ ! -s "$DOWNLOADS/$package_name" ] && {
        set -f
        rm -f ${DOWNLOADS}/${package_name};
        set +f
        writeJSONResponseErr "result=>4078" "message=>Error loading file from URL";
        die -q;
    }

    unpack $CARTRIDGE_HOME && echo "Application deployed successfully!" >> $ACTIONS_LOG 2>&1 || { writeJSONResponseErr "result=>4071" "message=>Cannot unpack package"; die -q; }
    updateOwnership $APPROOT;
    clearCache;
}

function  _setup(){

    [ -d ${CARTRIDGE_HOME} ] &&  cd  ${CARTRIDGE_HOME};

    if [[ -f "$CARTRIDGE_HOME/bin/setup" ]]
    then
    local setup_script="${CARTRIDGE_HOME}/bin/setup --version $1";
        ${setup_script} >> $ACTIONS_LOG 2>&1;
    fi

    if [[ -f "$CARTRIDGE_HOME/bin/install" ]]
    then
        local install_script="${CARTRIDGE_HOME}/bin/install --version $1";
        ${install_script} >> $ACTIONS_LOG 2>&1;
    fi

    if [[ -f "$CARTRIDGE_HOME/bin/post-install" ]]
    then
        local post_install_script="${CARTRIDGE_HOME}/bin/post-install --version $1";
        ${post_install_script} >> $ACTIONS_LOG 2>&1;
    fi
    proceedStaticENV;
    ( [ ! -d  "${CARTRIDGE_HOME}/log"  ] || [[ ! -L "${CARTRIDGE_HOME}/log"  ]] ) && [ -d "${CARTRIDGE_HOME}/${Name}/log"  ] && ln -s "${CARTRIDGE_HOME}/${Name}/log" "${CARTRIDGE_HOME}/logs";
    saveEnvironmentVariables;
}

function describeSetup(){
    echo "setup cartridge \n\t\t -v \t <version> ";
}

function describeDeploy(){
    echo "deploy cartridge \n\t\t -u \t <package URL> ";
}

function setEnvApp(){
    local bundle_type=$1;

    [ -f "/etc/httpd/conf.d/ruby.env" ] && { [ -n "${APRUBYDEPLOY_VERSION:-}" ] && return 0; APRUBYDEPLOY="0.1"; echo "RailsEnv ${bundle_type}" > /etc/httpd/conf.d/ruby.env; chown -hR ${DATA_OWNER} /etc/httpd/conf.d/ 2>>"$JEM_CALLS_LOG"; }
    [ -f "/etc/nginx/ruby.env" ] && { [ -n "${NGRUBYDEPLOY_VERSION:-}" ] && return 0; NGRUBYDEPLOY="0.1"; echo "RailsEnv ${bundle_type}" > /etc/nginx/ruby.env; chown -hR ${DATA_OWNER} /etc/nginx/ 2>>"$JEM_CALLS_LOG"; }

}
