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

inherit os;
include output;
$PROGRAM 'grep';

function writeGeneralLogInfo(){
    echo -n "$(date +%D.%k:%M:%S.%N ) NodeId - $(hostname|awk -F - {'print $1'}|grep -o [0-9]*); Host - $(hostname); URL - $URL; Directory - ${WEBROOT}/${PROJECT_NAME};"| tee -a "$DEPLOY_LOG" >> "$VCS_UPDATE_LOG" 2>&1;
}

function doCheckout(){
    set -f
    rm  -Rf "${WEBROOT}/${PROJECT_NAME}";
    set +f
    writeGeneralLogInfo
    $SVN co $URL --username $LOGIN --password $PASSWORD --no-auth-cache --non-interactive --trust-server-cert --force "${WEBROOT}/${PROJECT_NAME}" >> "$ACTIONS_LOG" 2>&1;
    result=$?
    cat ${ACTIONS_LOG} | tee -a "$DEPLOY_LOG" >> "$VCS_UPDATE_LOG";
    [[ $result -ne 0 ]] && {
        [ ! -d "${WEBROOT}/${PROJECT_NAME}/" ] && mkdir -p "${WEBROOT}/${PROJECT_NAME}/" 1>/dev/null 2>&1 ;
        updateOwnership "${WEBROOT}/${PROJECT_NAME}";
        echo "Host $(hostname) Repository $URL Directory ${WEBROOT}/${PROJECT_NAME}" >> $ACTIONS_LOG; 
        { writeJSONResponseErr "result=>4000" "message=>An error occurred during the VCS deployment on node $(hostname|awk -F - {'print $1'}|grep -o [0-9]*). Please, check the logs for more details."; die -q; };
    }
    [ "${COMPUTE_TYPE}" == "apache-ruby" -o "${COMPUTE_TYPE}" ==  "nginx-ruby" ] && {
        inherit ruby-common;
        bundleInstall;
    }
    return 0;
}

function updateProject(){
    writeGeneralLogInfo
    $SVN revert -R  "${WEBROOT}/${PROJECT_NAME}" > "${VCS_LOG_DIR}/deploy.log.log";
    $SVN up "${WEBROOT}/${PROJECT_NAME}" --username $LOGIN --password $PASSWORD --no-auth-cache --non-interactive --trust-server-cert --force  >> "$ACTIONS_LOG" 2>&1;
    result=$?;
    cat ${ACTIONS_LOG} | tee -a "$DEPLOY_LOG" >> "$VCS_UPDATE_LOG";
    if [ $result -ne 0 ] ; then
        echo "Host $(hostname) Repository $URL Directory ${WEBROOT}/${PROJECT_NAME}" >> $ACTIONS_LOG;
        { writeJSONResponseErr "result=>4000" "message=>An error occurred during the VCS deployment on node $(hostname|awk -F - {'print $1'}|grep -o [0-9]*). Please, check the logs for more details."; die -q; };
    else
        [ "${COMPUTE_TYPE}" == "apache-ruby" -o "${COMPUTE_TYPE}" ==  "nginx-ruby" ] && {
            inherit ruby-common;
            bundleInstall;
        }
    fi
    return $result;
}

function getLastRepoCommit(){
    readProjectConfig;
    local last_repo_commit=$($SVN --username $LOGIN --password $PASSWORD --no-auth-cache --non-interactive --trust-server-cert info "${WEBROOT}/${PROJECT_NAME}" 2>&1 | $GREP Revision: |cut -c11-);
    [ -z "${last_repo_commit}" ] && last_repo_commit="00000000000" && LAST_DEPLOYED_COMMIT="00000000000";
    echo $last_repo_commit;
}

function getLastRemoteCommit(){
    readProjectConfig;
    last_remote_commit=$($SVN --username ${LOGIN} --password ${PASSWORD} --no-auth-cache --non-interactive --trust-server-cert info ${URL} 2>&1 | $GREP Revision: |cut -c11-);
    [ -z "${last_remote_commit}" ] && last_remote_commit="00000000000";
    echo $last_remote_commit;
}

function vcsChangeUrl(){
    writeGeneralLogInfo
    $SVN switch --relocate --username $LOGIN --password $PASSWORD --no-auth-cache --non-interactive --trust-server-cert --force  "$OLDURL" "$URL" "${WEBROOT}/${PROJECT_NAME}/" >> $ACTIONS_LOG 2>&1;
    result=$?;
    cat ${ACTIONS_LOG} | tee -a "$DEPLOY_LOG" >> "$VCS_UPDATE_LOG";
    if [ $result -ne 0 ] ; then
        echo "Host $(hostname) Repository $URL Directory ${WEBROOT}/${PROJECT_NAME}" >> $ACTIONS_LOG;
        { writeJSONResponseErr "result=>4000" "message=>An error occurred during the VCS deployment on node $(hostname|awk -F - {'print $1'}|grep -o [0-9]*). Please, check the logs for more details."; die -q; };
    fi
    return $result;
}
