#!/bin/bash

SCRIPT_VER="1.5"

APP="PyLoad"
APP_CATEGORY="Download Managers"
APP_SCOPE="system"
APP_ENABLED="yes"
SUPPORTED_ON="Debian9,Ubuntu16.04"
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

#VALIDATE_USER

PYLOAD_VER="0.4.9"
APP_VER="0.4.9"
APP_DIRECTORY="/var/pyload"

while : ; do

    main_rc=1

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

    APT_INSTALL \
        "libqt4-dev" \
        "python-qt4" \
        "python-imaging" \
        "python-pil" \
        "python-crypto" \
        "python-pycurl" \
        "tesseract-ocr" \
        "tesseract-ocr-eng" \
        "tesseract-ocr-equ" \
        "tesseract-ocr-osd" \
        "libmozjs-24-bin" \
    || break

    case "${LSB_VER}" in
      "Debian9")
          APT_INSTALL "libwebpmux2" || break
          ;;
      *)
          APT_INSTALL "libwebpmux1" || break
          ;;
    esac

    if ! DPKG_CHECK "pyload" ; then
        WGET "${SUPPORT}/pyload/pyload_${PYLOAD_VER}_all.deb" \
           || break
        DPKG_INSTALL "${TMP_DIR}/pyload_${PYLOAD_VER}_all.deb" || break
    fi

    MKDIR "${APP_DIRECTORY}" || break

    cat <<-EOF > "/etc/systemd/system/pyload.service"
	[Unit]
	Description=pyLoad Core
	After=network.target

	[Service]
	Type=simple
	User=root
	Group=root
	UMask=0775
	ExecStart=/usr/bin/pyLoadCore --configdir=${APP_DIRECTORY} --pidfile=/var/run/pyload.pid
	Restart=always
	KillSignal=SIGQUIT

	[Install]
	WantedBy=multi-user.target
	EOF
    WGET "${SUPPORT}/files.db" || break
    CP_FILE "${TMP_DIR}/files.db" ${APP_DIRECTORY}/ || break
    cat <<-EOF > "${APP_DIRECTORY}/pyload.conf"
	version: 1 

	remote - "Remote":
		bool nolocalauth : "No authentication on local connections" = True
		bool activated : "Activated" = True
		int port : "Port" = 7227
		ip listenaddr : "Adress" = 0.0.0.0

	log - "Log":
		int log_size : "Size in kb" = 100
		folder log_folder : "Folder" = Logs
		bool file_log : "File Log" = True
		int log_count : "Count" = 5
		bool log_rotate : "Log Rotate" = True

	permission - "Permissions":
		str group : "Groupname" = users
		bool change_dl : "Change Group and User of Downloads" = False
		bool change_file : "Change file mode of downloads" = False
		str user : "Username" = user
		str file : "Filemode for Downloads" = 0644
		bool change_group : "Change group of running process" = False
		str folder : "Folder Permission mode" = 0755
		bool change_user : "Change user of running process" = False

	general - "General":
		en;de;fr;it;es;nl;sv;ru;pl;cs;sr;pt_BR language : "Language" = en
		folder download_folder : "Download Folder" = Downloads
		bool checksum : "Use Checksum" = False
		bool folder_per_package : "Create folder for each package" = True
		bool debug_mode : "Debug Mode" = False
		int min_free_space : "Min Free Space (MB)" = 200
		int renice : "CPU Priority" = 0

	ssl - "SSL":
		file cert : "SSL Certificate" = ssl.crt
		bool activated : "Activated" = False
		file key : "SSL Key" = ssl.key

	webinterface - "Webinterface":
		str template : "Template" = default
		bool activated : "Activated" = True
		str prefix : "Path Prefix" = 
		builtin;threaded;fastcgi;lightweight server : "Server" = builtin
		ip host : "IP" = 0.0.0.0
		bool https : "Use HTTPS" = False
		int port : "Port" = 8000

	proxy - "Proxy":
		str username : "Username" = None
	EOF

    SYSTEMD_RELOAD
    SVC_ENABLE "pyload"
    SVC_START  "pyload"


    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}': Installation failed."
    # Cleanup tasks here
    SVC_DISABLE "pyload"
    SVC_STOP    "pyload"
    RM_DIRS "${APP_DIRECTORY}"
    SYSTEMD_RELOAD
fi

SCRIPTEXIT
