#!/bin/bash

SCRIPT_VER="1.2"

APP="OwnCloud Server"
APP_CATEGORY="Sync Solutions"
APP_SCOPE="system"
APP_ENABLED="no"
USE_APACHE2="yes"
SYS_PKG_NAME="owncloud-files"
SUPPORTED_ON="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

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

APP_DIRECTORY="/var/www/owncloud"

while : ; do

    main_rc=1

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

    # https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-owncloud-on-ubuntu-16-04

    # Prepare APT
    APT_INSTALL "apt-transport-https" || break
#    ADD_APT_KEY_URL \
#        "https://download.owncloud.org/download/repositories/stable/Ubuntu_16.04/Release.key" \
#      || break
#    RUN_LOG "echo 'deb https://download.owncloud.org/download/repositories/stable/Ubuntu_16.04/ /' \
#                         > '/etc/apt/sources.list.d/owncloud.list'" \
#        || break
    ADD_APT_KEY_URL \
        "https://download.owncloud.org/download/repositories/production/Ubuntu_16.04/Release.key" \
        "owncloud.gpg" \
      || break
    RUN_LOG "echo 'deb https://download.owncloud.org/download/repositories/production/Ubuntu_16.04/ /' \
                         > '/etc/apt/sources.list.d/owncloud.list'" \
        || break
    APT_UPDATE || break

    if [ "${DB_MODE}" = "mysql" ] ; then
        MYSQL_PKG="mysql-server"
        [ "${LSB_VER}" = "Debian9" ] && MYSQL_PKG="mariadb-server"
        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
        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

    # Install required packages
    APT_INSTALL \
        "sudo" \
        "libapache2-mod-php7.0" \
        "php7.0-gd" \
        "php7.0-json" \
        "php7.0-mysql" \
        "php7.0-curl" \
        "php7.0-sqlite3" \
        "php7.0-mbstring" \
        "php7.0-intl" \
        "php7.0-mcrypt" \
        "php-imagick" \
        "php7.0-xml" \
        "php7.0-zip" \
      || break
    APT_INSTALL "owncloud-files" || break

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

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

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

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

	</Directory>
	EOF
    ${A2ENSITE} "owncloud.conf"

    # Restart `apache2` service
    SVC_RESTART "${APACHE_SVC}"


    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
    APT_REMOVE "owncloud"
    if [ -e "/etc/apt/sources.list.d/owncloud.list" ] ; then
        RM_FILES "/etc/apt/sources.list.d/owncloud.list"
        APT_UPDATE
    fi
    if [ -e "/etc/apache2/conf-available/owncloud.conf" ] ; then
        RUN_LOG "a2disconf owncloud.conf 2>&1"
        RM_FILES "/etc/apache2/conf-available/owncloud.conf"
        SVC_RESTART "${APACHE_SVC}"
    fi
    RM_DIRS "${APP_DIRECTORY}"
fi

SCRIPTEXIT
