#!/bin/bash

SCRIPT_VER="1.5"

APP="Opera"
APP_CATEGORY="Web Browsers"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="opera-stable"
SUPPORTED_ON="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
      "CentOS")
#          RPM_INSTALL "http://download1.operacdn.com/pub/opera/desktop/41.0.2353.69/linux/opera-stable_41.0.2353.69_amd64.rpm"
          cat <<-EOF > "/etc/yum.repos.d/opera.repo"
		[opera]
		name=Opera packages
		type=rpm-md
		baseurl=https://rpm.opera.com/rpm
		gpgcheck=1
		gpgkey=https://rpm.opera.com/rpmrepo.key
		enabled=1
		EOF
          ;;
      "Debian"|"Ubuntu")
          RUN_LOG "echo 'deb https://deb.opera.com/opera-stable/ stable non-free' \
                      > '/etc/apt/sources.list.d/opera.list'"
          if [ $? -ne 0 ] ; then
              ERROR "Error adding '${APP}' repository!"
              break
          fi
          CHMOD_FILE 644 '/etc/apt/sources.list.d/opera.list'
          CHOWN_FILE root:root '/etc/apt/sources.list.d/opera.list'
          ADD_APT_KEY_URL "https://deb.opera.com/archive.key" "opera-stable.gpg" || break
          APT_UPDATE || break
          RUN_LOG "echo 'debconf opera-stable/add-deb-source select false' | debconf-set-selections"
          ;;
    esac

    ${PKG_INSTALL} "opera-stable" || 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/opera.list" ] ; then
        RM_FILES "/etc/apt/sources.list.d/opera.list" \
            && APT_UPDATE
    fi
    [ -e "/etc/yum.repos.d/opera.repo" ] \
        && RM_FILES "/etc/yum.repos.d/opera.repo"
fi

SCRIPTEXIT
