#!/bin/bash

SCRIPT_VER="1.6"

APP="NextCloud Server"
APP_CATEGORY="Sync Solutions"
APP_SCOPE="system"
APP_ENABLED="yes"
USE_APACHE2="yes"
SUPPORTED_ON="Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.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

MYSQL_PASSWORD=""
#DB_MODE="mysql"
DB_MODE="sqlite"

APP_DIRECTORY="/var/www/nextcloud"

# INFO:
# https://download.nextcloud.com/server/releases/
# https://www.marksei.com/how-to-install-nextcloud-16-on-ubuntu/
# https://www.rosehosting.com/blog/how-to-install-nextcloud-14-on-ubuntu-16-04/
# https://www.tutsmake.com/how-to-install-nextcloud-on-ubuntu-22-04/

while : ; do

    main_rc=1

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


# README.1st !!!
# https://help.nextcloud.com/t/nextcloud-with-apache-reverse-proxy/19742/4

    if [ "${DB_MODE}" = "mysql" ] ; then
        MYSQL_PKG="mysql-server"
        [ "${LSB_VER}" = "Debian9" ] && MYSQL_PKG="mariadb-server"
        DPKG_CHECK "${MYSQL_PKG}"
        if [ $? -ne 0 ] ; 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
        else
            INFO "${MYSQL_PKG} is already installed."
        fi
    elif [ "${DB_MODE}" = sqlite ] ; then
        # Let's stick with `sqlite3` for testing purposes
        APT_INSTALL "sqlite3" || break
    fi

    case "${LSB_CODENAME}" in
      "xenial")      # u16.04
        PHP_VER="7.0"
        PHP_INI="/etc/php/${PHP_VER}/apache2/php.ini"
        PHP_ADD_MODS="php-mcrypt"
        PHPENMOD="phpenmod"
        ARCHIVE="latest-15.zip"
        ;;
      "bionic")      # u18.04
        PHP_VER="7.2"
        PHP_INI="/etc/php/${PHP_VER}/apache2/php.ini"
        PHP_ADD_MODS=""
        PHPENMOD="phpenmod"
        ARCHIVE="latest-24.zip"
        ;;
      "focal")       # u20.04
        PHP_VER="7.4"
        PHP_INI="/etc/php/${PHP_VER}/apache2/php.ini"
        PHP_ADD_MODS=""
        PHPENMOD="phpenmod"
#        ARCHIVE="latest-24.zip"
        ARCHIVE="latest-25.zip"
        ;;
      "jammy")       # u22.04
        PHP_VER="8.1"
        PHP_INI="/etc/php/${PHP_VER}/apache2/php.ini"
        PHP_ADD_MODS=""
        PHPENMOD="phpenmod"
        ARCHIVE="latest-27.zip"
#        ARCHIVE="latest.zip"
        ;;
      *)
        ERROR "Unsupported ${LSB_VENDOR} release: ${LSB_RELEASE}"
        break
        ;;
    esac

    if ! which pip > /dev/null 2>&1 ; then
        _install_pip
    fi
    APT_INSTALL "unzip" "git-core" "python3-lxml" || break
    APT_INSTALL "php${PHP_VER}" "libapache2-mod-php${PHP_VER}" || break

    # Install PHP modules
    APT_INSTALL \
        "libapache2-mod-php" \
        "php-gd" \
        "php-json" \
        "php-mysql" \
        "php-curl" \
        "php-sqlite3" \
        "php-mbstring" \
        "php-intl" \
        "php-imagick" \
        "php-xml" \
        "php-zip" \
      || break

    if [ -n "${PHP_ADD_MODS}" ] ; then
        APT_INSTALL ${PHP_ADD_MODS} || break
    fi

    WGET "https://download.nextcloud.com/server/releases/${ARCHIVE}" || break
#    UNZIP "${TMP_DIR}/${ARCHIVE}" "/var/www/" || break
#    CHOWN_DIR "${APACHE_USER}":"${APACHE_USER}" "${APP_DIRECTORY}"
    [ ! -d "/data/apps.data" ] && MKDIR "/data/apps.data"
    UNZIP "${TMP_DIR}/${ARCHIVE}" "/data/apps.data/" || break
    RUN_LOG "ln -svf '/data/apps.data/nextcloud' '/var/www/nextcloud' 2>&1" || break
    CHOWN_DIR "${APACHE_USER}":"${APACHE_USER}" "$( readlink -f "${APP_DIRECTORY}" )"

    # Configure Apache2 site
    cat <<-EOF > "/etc/apache2/sites-available/nextcloud.conf"
	Alias /nextcloud "${APP_DIRECTORY}/"

	<Directory ${APP_DIRECTORY}/>
	  Options +FollowSymlinks
	  AllowOverride All
	  Require all granted

	 <IfModule mod_dav.c>
	  Dav off
	 </IfModule>

	 SetEnv HOME ${APP_DIRECTORY}
	 SetEnv HTTP_HOME ${APP_DIRECTORY}

	</Directory>
	EOF
    ${A2ENSITE} "nextcloud.conf"
    # Enable apache2 modules
    RUN_LOG "a2enmod headers 2>&1"
    RUN_LOG "a2enmod rewrite 2>&1"
    RUN_LOG "a2enmod env 2>&1"
    RUN_LOG "a2enmod dir 2>&1"
    RUN_LOG "a2enmod mime 2>&1"
    # Restart `apache2` service
    SVC_RESTART "${APACHE_SVC}"

    # -- Configure NextCloud directory URL in RSB Dashboard
    if [ -d "/var/www/dashboard" ] ; then
        RUN_LOG "ln -svf '${APP_DIRECTORY}' '/var/www/dashboard/nextcloud' 2>&1"
    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
    ERROR "'${APP}': Installation failed."
    # Cleanup tasks here
    if [ -e "/etc/apt/sources.list.d/morph027-nextcloud.list" ] ; then
        RM_FILES "/etc/apt/sources.list.d/morph027-nextcloud.list"
        APT_UPDATE
    fi
    if [ -f "/etc/apache2/sites-available/nextcloud.conf" ] ; then
        ${A2DISSITE} "nextcloud.conf"
        RM_FILES "/etc/apache2/sites-available/nextcloud.conf"
        SVC_RESTART "${APACHE_SVC}"
    fi
    RM_DIRS "${APP_DIRECTORY}"
fi

SCRIPTEXIT
