#!/bin/bash

SCRIPT_VER="1.4"

APP="PlexPy"
APP_CATEGORY="Streaming"
APP_SCOPE="system"
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

    ${PKG_INSTALL} "git-core" || break

    # Clone Git repository
    GIT_CLONE "https://github.com/drzoidberg33/plexpy.git" "${APP_DIRECTORY}" || break

    # Create plexpy user
    RUN_LOG "id plexpy 2>&1" && RUN_LOG "userdel -f plexpy 2>&1"
    RUN_LOG "useradd -r -M -c 'PlexPy Service' -d '${APP_DIRECTORY}' ${PLEXPY_USER} 2>&1"
    if [ $? -ne 0 ] ; then
        RM_DIRS "${APP_DIRECTORY}"
        break
    fi

    CHOWN_DIR ${PLEXPY_USER}:${PLEXPY_USER} "${APP_DIRECTORY}"

    # Configure services
    # -- install 'python27' on Debian/Ubuntu
    case "${LSB_VENDOR}" in
      "Debian" | "Ubuntu")
          APT_INSTALL "python" || break
          ;;
    esac
    # -- install systemd service
    cat <<-EOF > "/etc/systemd/system/plexpy.service" 2>> "${LOGFILE}"
		[Unit]
		Description=PlexPy - Stats for Plex Media Server usage

		[Service]
		ExecStartPre=/bin/rm -f ${APP_DIRECTORY}/plexpy.pid
		ExecStart=${APP_DIRECTORY}/PlexPy.py --quiet --daemon --nolaunch --config ${APP_DIRECTORY}/config.ini --datadir ${APP_DIRECTORY} --pidfile ${APP_DIRECTORY}/plexpy.pid
		GuessMainPID=no
		Type=forking
		User=${PLEXPY_USER}
		Group=nogroup

		[Install]
		WantedBy=multi-user.target
		EOF
    SYSTEMD_RELOAD
    SVC_ENABLE "plexpy"
    SVC_START  "plexpy"

    if [ -d "/etc/monit/conf.d" ] ; then
        cat <<-EOF > "/etc/monit/conf.d/plexpy"
	check process plexpy with pidfile ${APP_DIRECTORY}/plexpy.pid
	  start program = "/usr/sbin/service plexpy restart"
	  stop program  = "/usr/sbin/service plexpy stop"
	  if 5 restarts within 5 cycles then timeout
	EOF
        MONIT_RELOAD 
    fi


    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 [ -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")
          [ -f "/etc/init.d/plexpy" ] \
              && RM_FILES "/etc/init.d/plexpy"
          ;;
      "systemd")
          if [ -f "/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}"
fi

SCRIPTEXIT
