#!/bin/bash

SCRIPT_VER="1.1"

APP="Microsoft Edge"
APP_CATEGORY="Web Browsers"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="microsoft-edge-dev"
#><#APP_CHECK_VER_CMD="${PKG_VER} microsoft-edge-dev"
SUPPORTED_ON="Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04"
LIBRARY="functions4.sh"

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

    case "${LSB_VENDOR}" in
      "Debian"|"Ubuntu")
          RUN_LOG "curl -s https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > ${TMP_DIR}/microsoft.gpg" || break
          RUN_LOG "install -o root -g root -m 644 ${TMP_DIR}/microsoft.gpg /etc/apt/trusted.gpg.d/"
          RUN_LOG \
             "echo 'deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main' \
                      > /etc/apt/sources.list.d/microsoft-edge-dev.list" \
            || break
          APT_UPDATE || break
          ;;
    esac

    ${PKG_INSTALL} "microsoft-edge-dev" || break


    main_rc=0 
    break

done

# Cleanup if error
if [ ${main_rc} -ne 0 -a ${main_rc} -ne 111 -a ${main_rc} -ne 117 ] ; then
    if [ -e "/etc/apt/sources.list.d/microsoft-edge-dev.list" ] ; then
        RM_FILES "/etc/apt/sources.list.d/microsoft-edge-dev.list" \
            && APT_UPDATE
    fi
fi

SCRIPTEXIT
