#!/bin/bash

SCRIPT_VER="1.3"

APP="PlexPy"
APP_CATEGORY="Streaming"
APP_ENABLED="yes"
#><#APP_CHECK_VER_CMD="if [ -e /opt/plexpy/CHANGELOG.md ] ; then grep -m 1 -e "^## " /opt/plexpy/CHANGELOG.md | awk "{print \$2}" ; else echo "" ; fi"
DEPENDS="Plex"
CONFLICTS="Tautulli"
SUPPORTED_ON="Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04"
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


PLEXPY_USER="plexpy"
APP_DIRECTORY="/opt/plexpy"

while : ; do

    main_rc=1

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

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

    SVC_DISABLE "plexpy"
    SVC_STOP    "plexpy"
    case "${SVC_SYSTEM}" in
      "init")
          [ -e "/etc/init.d/plexpy" ] \
              && RM_FILES "/etc/init.d/plexpy"
          ;;
      "systemd")
          if [ -e "/etc/systemd/system/plexpy.service" ] ; then
              RM_FILES "/etc/systemd/system/plexpy.service"
              SYSTEMD_RELOAD
          fi
          ;;
    esac

    # Delete plexpy user
    RUN_LOG "userdel -f ${PLEXPY_USER} 2>&1"

    # Remove APP directory
    [ -e "${APP_DIRECTORY}" ] \
        && RM_DIRS "${APP_DIRECTORY}"



    main_rc=0
    break

done

SCRIPTEXIT
