#!/bin/bash

SCRIPT_VER="1.5"

APP="Waterfox"
APP_CATEGORY="Web Browsers"
APP_SCOPE="system"
APP_ENABLED="yes"
#SYS_PKG_NAME="waterfox-classic-kde"
SYS_PKG_NAME="waterfox-classic-kpe"
SUPPORTED_ON="Debian9,Debian10,Ubuntu18.04,Ubuntu20.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")
          _rep="Debian_${LSB_RELEASE}"
          case "${LSB_VER}" in
            "Debian9")
                _rep="${_rep}.0"
                ;;
          esac
          echo \
              "deb http://download.opensuse.org/repositories/home:/hawkeye116477:/waterfox/${_rep}/ /" \
            > "/etc/apt/sources.list.d/waterfox.list"
          ADD_APT_KEY_URL \
            "https://download.opensuse.org/repositories/home:hawkeye116477:waterfox/${_rep}/Release.key" \
            "waterfox.gpg" \
                 || break
          ;;
      "Ubuntu")
          echo \
              "deb http://download.opensuse.org/repositories/home:/hawkeye116477:/waterfox/xUbuntu_${LSB_RELEASE}/ /" \
            > "/etc/apt/sources.list.d/waterfox.list"
          ADD_APT_KEY_URL \
            "https://download.opensuse.org/repositories/home:hawkeye116477:waterfox/xUbuntu_${LSB_RELEASE}/Release.key" \
            "waterfox.gpg" \
                 || break
          ;;
    esac
    APT_UPDATE || break
#    ${PKG_INSTALL} "waterfox-classic-kde" || break
    ${PKG_INSTALL} "waterfox-classic-kpe" || break

    [ ! -d "/etc/skel/Desktop" ] && MKDIR "/etc/skel/Desktop"
    desktop_file="Waterfox Classic.desktop"
    cat <<-EOF > "/etc/skel/Desktop/${desktop_file}"
	[Desktop Entry]
	Version=1.0
	Type=Application
	Name=Waterfox Classic
	Comment=Browse the World Wide Web
	Exec=waterfox-classic %u
	Icon=waterfox-classic
	Path=
	Terminal=false
	StartupNotify=true
	EOF
    CHMOD_FILE 755 "/etc/skel/Desktop/${desktop_file}"
    if [ ! -d "${USERHOME}/Desktop" ] ; then
        MKDIR "${USERHOME}/Desktop"
        CHOWN_DIR "${USERNAME}":"${USERNAME}" "${USERHOME}/Desktop"
    fi
    CP_FILE "/etc/skel/Desktop/${desktop_file}" "${USERHOME}/Desktop/"
    CHOWN_FILE "${USERNAME}":"${USERNAME}" "${USERHOME}/Desktop/${desktop_file}"


    main_rc=0
    break

done


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

SCRIPTEXIT
