#!/bin/bash
#
# Copyright (C) 2019 OX Software GmbH
# 
# This file is part of OX Automation.
#
# OX Automation is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OX Automation is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OX Automation. If not, see <http://www.gnu.org/licenses/>.
#
#
# Authors:
# Benedikt Kroening <benedikt.kroening@open-xchange.com>
#
set -e
SCRIPT_DIR="$( cd "$(dirname "$0")" ; pwd -P)"

source ${SCRIPT_DIR}/../../lib/common.bash

INIT_STATE_FILE="/opt/open-xchange/etc/20-init-middleware-done"

if [ ! -f ${INIT_STATE_FILE} ]; then
    echo "Initializing this middleware "
    # Increasing rate limit...
    # setting default config (key, value, filepath)
    setOXProperty com.openexchange.servlet.maxRate 1500000 /opt/open-xchange/etc/server.properties
    # Set sieve server
    setOXProperty com.openexchange.mail.filter.server "${OX_MAIL_HOST}" /opt/open-xchange/etc/mailfilter.properties 
    # Enabling Chronos owner change...
    # setOXProperty io.ox/calendar//chronos/allowChangeOfOrganizer true /opt/open-xchange/etc/settings/appsuite.properties
    setOXProperty com.openexchange.calendar.allowChangeOfOrganizer true /opt/open-xchange/etc/calendar.properties
    # setOXProperty for sharing to frontend 
    setOXProperty com.openexchange.share.guestHostname "frontend" /opt/open-xchange/etc/share.properties
    setOXProperty io.ox/tours//server/disableTours true /opt/open-xchange/etc/settings/guidedtours.properties	
    setOXProperty io.ox/core//wizards/firstStart/finished true /opt/open-xchange/etc/settings/guidedtours.properties
    setOXProperty io.ox/tours//server/startOnFirstLogin false /opt/open-xchange/etc/settings/guidedtours.properties
    setOXProperty io.ox/tours//whatsNew/neverShowAgain true /opt/open-xchange/etc/settings/guidedtours.properties
    # enable Jolokia
    setOXProperty com.openexchange.jolokia.start true /opt/open-xchange/etc/jolokia.properties
    setOXProperty com.openexchange.jolokia.user openxchange /opt/open-xchange/etc/jolokia.properties
    setOXProperty com.openexchange.jolokia.password secret /opt/open-xchange/etc/jolokia.properties
    setOXProperty com.openexchange.jolokia.restrict.to.localhost false /opt/open-xchange/etc/jolokia.properties

    echo "Waiting for database service ..."
    waitPort ${DATABASE_HOST_NAME} ${DATABASE_HOST_PORT}

    restartService open-xchange

    sleep 10
    waitPort localhost 8009

    /opt/open-xchange/sbin/registerserver -n "${OX_SERVER_NAME}" \
        -A "${OX_ADMINMASTER_USER}" \
        -P "${OX_ADMINMASTER_PASS}" > /opt/open-xchange/etc/20-registerserver.out
    export OX_SERVER_ID=$(cat /opt/open-xchange/etc/20-registerserver.out | awk '{ print $2 }')

    echo "Creating filestore at /var/opt/filestore (you may mount some external storage there ...)"
   
    mkdir -p /var/opt/filestore
    chown -R open-xchange:open-xchange /var/opt/filestore
    # Set permissions in uploads directory, which is mapped to host
    chmod 777 -R /var/spool/open-xchange/uploads

    /opt/open-xchange/sbin/registerfilestore -t file:///var/opt/filestore \
          -A "${OX_ADMINMASTER_USER}" \
          -P "${OX_ADMINMASTER_PASS}" > /opt/open-xchange/etc/20-registerfilestore.out
    export OX_FILESTORE_ID=$(cat /opt/open-xchange/etc/20-registerfilestore.out | awk '{ print $2 }')

    # register master database
    /opt/open-xchange/sbin/registerdatabase \
          -A "${OX_ADMINMASTER_USER}" \
          -P "${OX_ADMINMASTER_PASS}" \
          --hostname "${DATABASE_HOST_NAME}" \
          --dbuser "${OX_CONFIGDB_USER}" \
          --dbpasswd "${OX_CONFIGDB_PASS}" \
          --master "true" \
          --name "oxdatabase" > /opt/open-xchange/etc/20-registermasterdatabase.out
    OX_DATABASE_MASTER_ID=$(cat /opt/open-xchange/etc/20-registermasterdatabase.out | awk '{ print $2 }')

    # register globaldb
    /opt/open-xchange/sbin/registerdatabase \
         -A "${OX_ADMINMASTER_USER}" \
         -P "${OX_ADMINMASTER_PASS}" \
         --hostname "${DATABASE_HOST_NAME}" \
         --dbuser "${OX_CONFIGDB_USER}" \
         --dbpasswd "${OX_CONFIGDB_PASS}" \
         --master "true" \
         --name "oxglobal" \
         --maxunit 0 > /opt/open-xchange/etc/20-registerglobaldatabase.out
    OX_DATABASE_GLOBAL_ID=$(cat /opt/open-xchange/etc/20-registerglobaldatabase.out | awk '{ print $2 }')
    
    echo "Setting globaldb id in /opt/open-xchange/etc/globaldb.yml to '$OX_DATABASE_GLOBAL_ID'"
    sed -i "s/id:.*/id: '$OX_DATABASE_GLOBAL_ID'/g" /opt/open-xchange/etc/globaldb.yml

    echo "Successfully initialized environment..."
    echo "Server name:  "$OX_SERVER_NAME
    echo "Server id:    "$OX_SERVER_ID
    echo "Filestore ID: "$OX_FILESTORE_ID
    echo "Master DB ID: "$OX_DATABASE_MASTER_ID
    echo "Global DB ID: "$OX_DATABASE_GLOBAL_ID
    echo "===================================================="

    echo "$(date)" > ${INIT_STATE_FILE}
else
    echo "WARNING: This scripts has already been executed and will be skipped!"
    sleep 5
fi

echo "Singlenode environment init finished at: "
cat ${INIT_STATE_FILE}


