#!/bin/bash

#
# Copyright (C) 2022 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer 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 any later version.
#
# NethServer 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 NethServer.  If not, see COPYING.
#

set -e

source /etc/nethserver/agent.env
source "${AGENT_STATE_DIR}"/agent.env
source "${AGENT_STATE_DIR}"/environment
cd "${AGENT_STATE_DIR}/nethserver-roundcubemail"
source bind.env

# Ensure endpoint is defined
: "${RSYNC_ENDPOINT:?}"
export RSYNC_PASSWORD

# Sync mysql roundcubemail DB dump
rm -vf roundcubemail.sql
mysqldump --single-transaction --quick --add-drop-table -QB "roundcubemail" -r roundcubemail.sql
rsync -zi roundcubemail.sql "${RSYNC_ENDPOINT}"/data/state/roundcubemail.sql

if [[ "${MIGRATE_ACTION}" != "finish" ]]; then
    exit 0
fi

# Obtain the Mail module UUID for configure-module:
mail_instance_uuid=$(ns8-action --attach "module/${MAIL_INSTANCE_ID}" list-service-providers \
  "$(printf '{"service":"imap", "transport":"tcp", "filter":{"module_id":"%s"}}' "${MAIL_INSTANCE_ID}")" \
  | jq -r '.[0].module_uuid')

# Assert required vars are set
: "${ROUNDCUBE_VHOST:?}" "${IMPORT_TASK_ID:?}" "${mail_instance_uuid:?}"

# Set up web redirects
mkdir -vp webroot
cat - >webroot/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Roundcube Migration</title>
  </head>
  <body>
    <h1>Roundcube Migration</h1>
    <p>Site has moved to &#9758; <a href="https://${ROUNDCUBE_VHOST}">${ROUNDCUBE_VHOST}</a></p>
  </body>
</html>
EOF
/sbin/e-smith/config setprop roundcubemail migration finished
/sbin/e-smith/expand-template /etc/httpd/conf.d/00ns8migration.conf
httpd -k graceful

# Commit Webtop migration
rsync "${RSYNC_ENDPOINT}"/terminate

# Wait until the import-module task has completed
ns8-action --attach wait "${IMPORT_TASK_ID}"

# Repeat the configure-module if it fails because mail module is not found
while ! ns8-action --attach "module/${MODULE_INSTANCE_ID}" configure-module "$(
    jq -n -c \
    --arg hostname "${ROUNDCUBE_VHOST}" \
    --arg mail_server "${mail_instance_uuid}" \
    --arg mail_domain "$(hostname -d)" \
    '{
      "host": $hostname,
      "http2https": true,
      "lets_encrypt": false,
      "mail_server": $mail_server,
      "mail_domain": $mail_domain,
      "plugins": "",
      "upload_max_filesize": 10,
    }')"
do
    exit_code="$?"
    if [[ "${exit_code}" != 3 ]]; then
        exit "${exit_code}"
    fi
    sleep 5
    echo "[NOTICE] Module ${MAIL_INSTANCE_ID} seems not ready: trying again to configure Roundcubemail..."
done
