#!/bin/bash

SCRIPT_VER="1.10"

APP="Plex"
APP_CATEGORY="Streaming"
APP_ENABLED="yes"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.04"
USE_APACHE2="yes"
MEMORY_K_MIN="4000000"
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

APP_HOME="/var/lib/plexmediaserver"
APP_DIRECTORY="${APP_HOME}/Library/Application Support/Plex Media Server"

while : ; do

    main_rc=1

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

    if [ -f "/etc/monit/conf.d/plexmediaserver" ] ; then
        RM_FILES "/etc/monit/conf.d/plexmediaserver"
        MONIT_RELOAD
    fi

    SVC_DISABLE "plexmediaserver"
    SVC_STOP    "plexmediaserver"

    # -- Revoke `plex` group & user account permissions to write to user's Downloads directory
    RUN_LOG "setfacl -Rx g:plex,d:g:plex /etc/skel/Downloads   2>&1"
    RUN_LOG "setfacl -Rx g:plex,d:g:plex /home/user/Downloads  2>&1"
    RUN_LOG "setfacl -Rx g:plex,d:g:plex ${USERHOME}/Downloads 2>&1"

    ${PKG_REMOVE} "plexmediaserver"

    # Remove Plex repository and keys
    [ -f "/etc/apt/sources.list.d/plex.list" ] && RM_FILES "/etc/apt/sources.list.d/plex.list"
    [ -f "/etc/apt/keyrings/plex.gpg" ] && RM_FILES "/etc/apt/keyrings/plex.gpg"
    [ -f "/etc/apt/sources.list.d/plexmediaserver.list" ] && RM_FILES "/etc/apt/sources.list.d/plexmediaserver.list"

    for __f in \
        "/etc/yum.repos.d/plex.repo" \
        "/etc/systemd/system/plexmediaserver.service" \
        "/lib/systemd/system/plexmediaserver.service" \
    ; do
        [ -e "${__f}" ] \
            && RM_FILES "${__f}"
    done


    [ "${SVC_SYSTEM}" = "systemd" ] && SYSTEMD_RELOAD

    if [ -e "/etc/apt/sources.list.d/plexmediaserver.list" ] ; then
        RM_FILES "/etc/apt/sources.list.d/plexmediaserver.list"
        APT_UPDATE
    fi

    getent passwd "plex" > /dev/null 2>&1 && RUN_LOG "userdel -f -r plex 2>&1"
    getent group  "plex" > /dev/null 2>&1 && RUN_LOG "groupdel plex 2>&1"

    if [ -d "${APP_HOME}" ] ; then
        for __i in 1 2 3 ; do
            [ ! -e "${APP_HOME}" ] && break
            RM_DIRS "${APP_HOME}"
        done
    fi

    if [ -d "/data/apps.data/Plex Media Server" ] ; then
        for __i in 1 2 3 ; do
            [ ! -d  "/data/apps.data/Plex Media Server" ] && break
            RM_DIRS "/data/apps.data/Plex Media Server"
        done
    fi


    for __f in \
        "/usr/local/bin/reset-plex" \
        "/usr/local/bin/rsb-reset-plex"
    do
        [ -e "${__f}" ] \
            && RM_FILES "${__f}"
    done

    # ---------------------------------------------------
    # Configure Apache
    # ---------------------------------------------------
    if [ -d "/etc/apache2" ] ; then
        RUN_LOG "a2dissite plex.conf"
        for __f in \
            "/etc/apache2/users.plex.password" \
            "/etc/apache2/plex.users.password" \
            "/etc/apache2/sites-enabled/plex.conf" \
            "/etc/apache2/sites-available/plex.conf" \
            "/etc/apache2/userconf-enabled/plex.conf" \
        ; do
            [ -e "${__f}" ] \
                 && RM_FILES "${__f}"
        done
        SVC_RESTART  "${APACHE_SVC}"
    fi

    # Configure firewall
    FW_REMOVE_PORTS "32400/tcp" "33400/tcp" "3005/tcp" "8324/tcp" "32469/tcp" \
                    "32410/udp" "32412/udp" "32413/udp" "42414/udp" "1900/udp" \
                    "5353/udp"


    main_rc=0
    break

done


SCRIPTEXIT
