#!/bin/bash

SCRIPT_VER="1.6"

APP="Pydio"
APP_CATEGORY="File Managers"
APP_SCOPE="system"
APP_ENABLED="yes"
SUPPORTED_ON="Ubuntu16.04,Ubuntu18.04,Ubuntu20.04"
USE_APACHE2="yes"
#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

    # README:
    # https://computingforgeeks.com/setup-pydio-cells-file-sharing-server-on-ubuntu/

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

    MYSQL_PKG="mysql-server"
    MYSQL_SVC="mysql"
    PD_REPO="${LSB_CODENAME}"
    case "${LSB_CODENAME}" in
      "xenial")
        PHP_VER="7.0"
        PHP_INI="/etc/php/${PHP_VER}/apache2/php.ini"
        PHPENMOD="phpenmod"
        ;;
      "bionic")
        PHP_VER="7.2"
        PHP_INI="/etc/php/${PHP_VER}/apache2/php.ini"
        PHPENMOD="phpenmod"
        ;;
      "focal")
        PHP_VER="7.4"
        PHP_INI="/etc/php/${PHP_VER}/apache2/php.ini"
        PHPENMOD="phpenmod"
        MYSQL_PKG="mariadb-server"
        MYSQL_SVC="mariadb"
        PD_REPO="bionic"
        ;;
      *)
        ERROR "Unsupported ${LSB_VENDOR} release: ${LSB_RELEASE}"
        break
        ;;
    esac

    echo "deb https://download.pydio.com/pub/linux/debian/ ${PD_REPO} main" \
        > "/etc/apt/sources.list.d/pydio.list"
    ADD_APT_KEY_URL "https://download.pydio.com/pub/linux/debian/key/pubkey" \
                    "pydio.gpg" \
        || break
    APT_UPDATE || break

    # Install Pydio
    APT_INSTALL "pydio" "pydio-all" || break

    # Configure PHP
    INFO "Configuring 'PHP${PHP_VER}'"
    tm="$( date +"%Y%m%d%H%M%S" )"
    CP_FILE "${PHP_INI}" "${PHP_INI}.pydio.bak.${tm}"
    RUN_LOG "grep -e '^output_buffering = Off' '${PHP_INI}' 2>&1" \
        || RUN_LOG "sed -i -e 's/^output_buffering =.*/output_buffering = Off/' '${PHP_INI}' 2>&1"
    RUN_LOG "grep -e '^upload_max_filesize = 1G' '${PHP_INI}' 2>&1" \
        || RUN_LOG "sed -i -e 's/^upload_max_filesize =.*/upload_max_filesize = 1G/' '${PHP_INI}' 2>&1"
    RUN_LOG "grep -e '^post_max_size = 1G' '${PHP_INI}' 2>&1" \
        || RUN_LOG "sed -i -e 's/^post_max_size =.*/post_max_size = 1G/' '${PHP_INI}' 2>&1"


    # Setup Apache2
    INFO "Setting up 'Apache2'"
    RUN_LOG "apachectl -t" || break
    SVC_RESTART "${APACHE_SVC}" || break

    # Configure MySQL
    if ! DPKG_CHECK "${MYSQL_PKG}" ; then
        debconf-set-selections <<< "${MYSQL_PKG} ${MYSQL_PKG}/root_password password ${MYSQL_PASSWORD}"
        debconf-set-selections <<< "${MYSQL_PKG} ${MYSQL_PKG}/root_password_again password ${MYSQL_PASSWORD}"
        APT_INSTALL "${MYSQL_PKG}" || break
        SVC_ENABLE  "${MYSQL_SVC}" || break
        SVC_START   "${MYSQL_SVC}" || break
    else
        INFO "${MYSQL_PKG} is already installed."
    fi

    if ! RUN_LOG "systemctl is-active ${MYSQL_SVC} 2>&1" ; then
        SVC_START "${MYSQL_SVC}" || break
    fi
    INFO "Sleeping 10 seconds ..."
    sleep 10
    RUN_LOG "systemctl is-active ${MYSQL_SVC} 2>&1" || break

    if RUN_LOG "mysqlshow pydio" ; then
        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}'"
    fi

    cat <<-EOF > "${tmp}" 2>> "${LOGFILE}"
	revoke ALL on *.* from '${PYDIO_USER}'@'localhost';
	use mysql;
	delete from user where user='${PYDIO_USER}';
	flush privileges;
	EOF
    RUN_LOG "mysql -f -vv <'${tmp}'"
    cat <<-EOF > "${tmp}" 2>> "${LOGFILE}"
	create database pydio;
	create user '${PYDIO_USER}'@'localhost' identified by '${PYDIO_PASS}';
	flush privileges;
	grant all on ${PYDIO_DB}.* to '${PYDIO_USER}'@'localhost' identified by '${PYDIO_PASS}';
	flush privileges;
	EOF
    RUN_LOG "mysql -f -vv <'${tmp}'"


    main_rc=0
    break

done


# Cleanup if error
if [ ${main_rc} -ne 0 -a ${main_rc} -ne 111 -a ${main_rc} -ne 117 ] ; then
    APT_REMOVE "pydio" "pydio-all"
    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}"
fi

SCRIPTEXIT
