#!/bin/bash

# ---
# RSB installer bootstrap script
#
# Version: 1.0
#
# Usage: "${0} VPS_IP_ADDRESS RSB_INSTALLERS_SITE installer_application.sh"
#
# (c) 2019, RapidSeedbox, alexey@rapidseedbox.com
#

SCRIPT_VER=$( grep -m 1 -e '^# Version:' "${0}" | awk '{print $NF}' )

NODE="$( hostname -s )"
LOGFILE="/var/log/whmcs_installer_bootstrap.log"

function INFO()
{
    local msg="${1}"
    local tstamp=$( date )
    echo -e "[${tstamp}] [INFO] ${L_PFX}: ${msg}" >> "${LOGFILE}"
}

function WARN()
{
    local msg="${1}"
    local tstamp=$( date )
    echo -e "[${tstamp}] [WARN] ${L_PFX}: ${msg}" >> "${LOGFILE}"
}

function ERROR()
{
    local msg="${1}"
    local tstamp=$( date )
    echo -e "[${tstamp}] [ERROR] ${L_PFX}: ${msg}" >> "${LOGFILE}"
}

function DEBUG()
{
    local msg="${1}"
    local tstamp=$( date )
    echo -e "[${tstamp}] [DEBUG] ${L_PFX}: ${msg}" >> ${LOGFILE}
}


# -- Main secton

L_PFX=$( basename "${0}" )

INFO "Started with arguments: [${*}]"

if [ -z "${1}" ] ; then ERROR "Arg #1 is missing !" ; echo "33" ; exit 33 ; fi 
if [ -z "${2}" ] ; then ERROR "Arg #2 is missing !" ; echo "34" ; exit 34 ; fi 
if [ -z "${3}" ] ; then ERROR "Arg #3 is missing !" ; echo "35" ; exit 35 ; fi 

VPS_IP="${1}"
CTID=$( vzlist | awk -v i="${VPS_IP}" '$4 == i  {print $1}' )
RSB_SITE="https://installers.rapidseedbox.com/scripts/whmcs_client_installers_test"
APP_SCRIPT="${3}"

if [ -z "${CTID}" ] ; then
    ERROR "Error retrieving CTID for IP: [${VPS_IP}] !" ; 
    echo "51"
    >&2 echo "51"
    exit 51
fi

[ -e "/vz/root/${CTID}/tmp/${APP_SCRIPT}" ] \
    && rm -vf "/vz/root/${CTID}/tmp/${APP_SCRIPT}" >> ${LOGFILE} 2>&1

wget --no-cache -4 -t 5 -T 5 --no-check-certificate -O "/vz/root/${CTID}/tmp/${APP_SCRIPT}" \
     "${RSB_SITE}/${APP_SCRIPT}" >> ${LOGFILE} 2>&1
_rc=$?
if [ ${_rc} -eq 0 ] ; then
    cat <<-EOF | vzctl exec2 "${CTID}" -
	export OCI_BOOTSTRAP_SCRIPT_VER="${SCRIPT_VER}"
	export SUPPORT="${RSB_SITE}/support"
	cd /tmp
	flock -w 1 /var/lock/whmcs_client_installers.lock date > /dev/null 2>&1 || exit 1
	export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
	rm -f /tmp/functions*.sh
	/bin/bash ./${APP_SCRIPT}
	EOF
    _rc=$?
    if [ ${_rc} -eq 1 ] ; then
        _rc=114
        echo "114"
        >&2 echo "114"
    else
        if [ "${APP_SCRIPT}" = "install_dist_upgrade.sh" ] ; then
            /usr/bin/curl -X POST -F "action=maintenanceCompleted" -F "dedicated_ip=${VPS_IP}" \
                   "https://members.rapidseedbox.com/modules/addons/whmcs_client_installers/ajax/db.php"
            if [ ${_rc} -eq 0 ] ; then
                INFO "Rebooting ${CTID}..."
                vzctl exec2 "${CTID}" "rm -vf \"/tmp/${APP_SCRIPT}\" ; shutdown -r now"
            fi
        fi
    fi
else
    _rc=50
    echo "50"
    >&2 echo "50"
fi

if [ -e "/vz/root/${CTID}/tmp/${APP_SCRIPT}" ] ; then
    rm -vf "/vz/root/${CTID}/tmp/${APP_SCRIPT}" >> ${LOGFILE} 2>&1
fi

INFO "Finished with exit code: [${_rc}]"
INFO "----------------------------------"
exit ${_rc}
