#!/bin/bash

SCRIPT_VER="1.4"

APP="Ombi"
APP_CATEGORY="Streaming"
APP_SCOPE="system"
APP_ENABLED="yes"
SUPPORTED_ON="Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04"
MEMORY_K_MIN="4000000"
USE_APACHE2="yes"
DAEMON_PORT=15000
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_DIRECTORY="/opt/Ombi"

while : ; do

    main_rc=1

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

    MKDIR "${APP_DIRECTORY}"

    _ver="$( curl -Ls -m 30 -o /dev/null -w '%{url_effective}' \
                  https://github.com/Ombi-app/Ombi/releases/latest \
              | awk -F "/" '{print $NF}' )"
    if [ -z "${_ver}" ] ; then
        ERROR "Could not determine ${APP} version!"
        break
    fi
    INFO "Latest build: ${_ver}"
    WGET "https://github.com/Ombi-app/Ombi/releases/download/${_ver}/linux-x64.tar.gz" \
        || break
    TAR_ZXF "${TMP_DIR}/linux-x64.tar.gz" "${APP_DIRECTORY}" || break
    CHMOD_FILE 755 "${APP_DIRECTORY}/Ombi" || break

    # NB!!!
    # https://github.com/tidusjar/Ombi/wiki/Reverse-Proxy-Examples#apache

    # Configure `apache2` service
    if [ -d "/etc/apache2/userconf-enabled/" ] ; then
        for __mod in         \
            "proxy"          \
            "proxy_http"     \
            "proxy_ajp"      \
            "rewrite"        \
            "deflate"        \
            "headers"        \
            "proxy_balancer" \
            "proxy_connect"  \
            "proxy_html"     \
        ; do
            RUN_LOG "a2enmod ${__mod} 2>&1"
        done
        cat <<-EOF > "/etc/apache2/userconf-enabled/ombi.conf"
	RewriteEngine on
	RewriteRule ^/ombi$ /ombi/ [R=permanent]
	<Location /ombi>
	    order deny,allow
	    deny from all
	    allow from all
	    ProxyPass http://127.0.0.1:15000/ombi connectiontimeout=5 timeout=30 keepalive=on
	    ProxyPassReverse http://127.0.0.1:15000/ombi
	    #AuthType Digest
	    #AuthName "apache2"
	    #AuthUserFile /etc/apache2/users.password
	    #Require user user
	</Location>
	EOF
        SVC_RESTART "apache2"
    fi

    MKDIR "/var/lib/Ombi" || break
    cat <<-EOF > "/etc/systemd/system/ombi.service" 2>> "${LOGFILE}"
	[Unit]
	Description=Ombi - PMS Requests System
	After=network-online.target

	[Service]
	User=root
	Group=root
	WorkingDirectory=${APP_DIRECTORY}/
	ExecStart=${APP_DIRECTORY}/Ombi --host http://127.0.0.1:15000 --storage /var/lib/Ombi --baseurl /ombi
	Type=simple
	TimeoutStopSec=30
	Restart=on-failure
	RestartSec=5

	[Install]
	WantedBy=multi-user.target
	EOF

    SVC_ENABLE  "ombi"
    SVC_START   "ombi"


    main_rc=0
    break

done


# Cleanup if error
if [ ${main_rc} -ne 0 -a ${main_rc} -ne 111 -a ${main_rc} -ne 117 ] ; then
    ERROR "'${APP}': Starting cleanup procedure"
    SVC_DISABLE "ombi"
    SVC_STOP  "ombi"
    RM_FILES "/etc/systemd/system/ombi.service" \
        && SYSTEMD_RELOAD
    RM_FILES "/etc/apache2/userconf-enabled/ombi.conf" \
        && SVC_RESTART "${APACHE_SVC}"
    RM_DIRS "${APP_DIRECTORY}" "/var/lib/Ombi"
fi

SCRIPTEXIT
