#!/bin/bash

SCRIPT_VER="1.5"

APP="Pydio"
APP_CATEGORY="File Managers"
APP_ENABLED="yes"
##><#APP_CHECK_VER_CMD="if [ -e /var/www/html/pydio/conf/VERSION ] ; then awk -F"__" "{print \$1}" /var/www/html/pydio/conf/VERSION ; else echo "" ; fi"
SUPPORTED_ON="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

MYSQL_PASSWORD=""

PYDIO_DB="pydio"
PYDIO_USER="pydio"
PYDIO_PASS="pydio"

APP_DIRECTORY="/usr/share/pydio"

while : ; do

    main_rc=1

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

    APT_REMOVE "pydio" "pydio-all"

    RM_DIRS "/var/lib/pydio" \
            "/var/www/html/pydio"

    if [ -e "/etc/apt/sources.list.d/pydio.list" ] ; then
        RM_FILES "/etc/apt/sources.list.d/pydio.list"
        APT_UPDATE
    fi
    SVC_RESTART "${APACHE_SVC}"

    # Cleanup DB
    INFO "Cleaning up DB"
    cat <<-EOF > "${tmp}" 2>> "${LOGFILE}"
	revoke ALL on *.* from '${PYDIO_USER}'@'localhost';
	revoke ALL on pydio.* from '${PYDIO_USER}'@'localhost';
	flush privileges;
	drop database ${PYDIO_DB};
	use mysql;
	delete from user where user='${PYDIO_USER}';
	flush privileges;
	EOF
    RUN_LOG "mysql -f -vv <'${tmp}'"


    main_rc=0
    break

done


SCRIPTEXIT
