#!/bin/bash

SCRIPT_VER="1.5"

APP="Update RSB Custom Scripts"
APP_CATEGORY="System Maintenance"
APP_SCOPE="system"
APP_ENABLED="yes"
SUPPORTED_ON="CentOS7,CentOS8,CentOSStream8,Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04"
LIBRARY="functions4.sh"
POSTINSTALL_MSG_SUCCESS="Please restart 'install-app' application."

if [ "${SUPPORT-}" = '' ]; then
    SUPPORT="https://installers.rapidseedbox.com/scripts/whmcs_client_installers/support"
fi

LOGFILE="/var/log/whmcs_client_installers/${0}.log"
[ ! -d "/var/log/whmcs_client_installers" ] && mkdir -p "/var/log/whmcs_client_installers"

wget -4 -nc -q --no-check-certificate --timeout=20 --tries=5 ${SUPPORT}/${LIBRARY} >> "${LOGFILE}" 2>&1
if [ ! -f "./${LIBRARY}" ] ; then
    echo "55"
    echo "Error downloading \"${SUPPORT}/${LIBRARY}\"" >> "${LOGFILE}"
    exit 55
fi

# shellcheck source=support/functions4.sh
. "./${LIBRARY}"
SCRIPTENTRY

while : ; do

    main_rc=1

    CHECK_APP ; _rc=$?
    if [ ${_rc} -ne 0 ] ; then
        main_rc=${_rc}
        break
    fi

    for app in \
      "changeseedboxpass" \
      "_ff" \
      "_ff-r" \
      "_svc" \
      "fix-cute" \
      "install-app" \
      "regen-cert" \
      "seedbox-cp" \
      "easy-ssl" \
      "easy-ssl-v2" \
      "easy-ssl-nginx" \
      "ssl-mgr" \
      "speedtest-cli" \
      "test-lw-speed" \
      "test-ovh-speed" \
      "test-velia-speed" \
      "pure-disable-tls" \
      "pure-enable-tls" \
      "reset-rsb-apache"
    do
        INFO "Checking if '${app}' update available ..."
        L_LOG
        WGET "${SUPPORT}/rsb-scripts/common/${app}"
        if [ $? -eq 0 ] ; then
            if [ -s "${TMP_DIR}/${app}" ] ; then
                __run_nolog "diff '${TMP_DIR}/${app}' '/usr/local/bin/${app}'"
                if [ $? -ne 0 ] ; then
                    INFO "Updating '${app}' ..."
                    MV "${TMP_DIR}/${app}" "/usr/local/bin/${app}"
                    CHOWN_FILE root:root "/usr/local/bin/${app}"
                    CHMOD_FILE 755 "/usr/local/bin/${app}"
                    s_app=$( echo "${app}" | sed -e 's/_\(.*\)/\1/' )
                    LN "/usr/local/bin/${app}" "/usr/local/bin/rsb-${s_app}"
                else
                    INFO "'${app}' is up to date."
                fi
            fi
        fi
    done


    main_rc=0
    break

done

SCRIPTEXIT
