#!/bin/bash

SCRIPT_VER="1.3"

APP="Overseerr"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="system"
APP_ENABLED="yes"
STORED_PASSWORD_SERVICE="user"
#><#APP_CHECK_VER_CMD="if which snap > /dev/null 2>&1 ; then snap list | awk -v app=overseerr "\$1 == app {print \$2}" | tr -d "v" ; else echo "" ; fi"
SUPPORTED_ON="Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04"
USE_APACHE2="yes"
DAEMON_PORT=5055
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

# References:
# https://docs.overseerr.dev/getting-started/installation

while : ; do

    main_rc=1 

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

    RUN_LOG "snap remove overseerr 2>&1"

    # -- Configure `systemd` service unit
    if [ -d "/etc/systemd/system/snap.overseerr.daemon.service.d" ] ; then
        RM_DIRS "/etc/systemd/system/snap.overseerr.daemon.service.d"
        SYSTEMD_RELOAD
    fi

#    # -- Configure Apache virtual site
#    if which "easy-ssl" > /dev/null 2>&1 ; then
#        _flag=""
#        if grep -q -E '//installers\.seedbox\.vip/' <<< "${SUPPORT}" ; then
#            _flag="-t"
#        fi
#        RUN_LOG "easy-ssl -b ${_flag} 2>&1"
#    fi

    main_rc=0 
    break

done


SCRIPTEXIT
