#!/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-mattermost"
source bind.env

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

if [[ "${MIGRATE_ACTION}" == "finish" ]]; then
    # During the last, "finish" rsync run there must be no changes to
    # the database: stop the service early.
    systemctl stop mattermost
fi

# Sync files: --owner --group --chown=2000:2000 are needed to map data for the mattermost user
rsync -tr --owner --group --chown=2000:2000 -s --delete  /var/lib/nethserver/mattermost/data/ "${RSYNC_ENDPOINT}"/data/volumes/mattermost-data/

# Create database dump
su - postgres -c "scl enable rh-postgresql12 -- pg_dump --port 55434 mattermost > /var/lib/nethserver/mattermost/backup/dump.sql"
rsync -tr /var/lib/nethserver/mattermost/backup/dump.sql "${RSYNC_ENDPOINT}"/data/state/restore/
rm -f /var/lib/nethserver/mattermost/backup/dump.sql

# Copy original config
rsync -tr /opt/mattermost/config/config.json "${RSYNC_ENDPOINT}"/data/state/

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

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

# Stop and disable Mattemost
/sbin/e-smith/config setprop mattermost status disabled || :
systemctl disable --now mattermost || :

# Setup migration page
mkdir -p ./webroot
cat - >webroot/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Mattermost Migration</title>
  </head>
  <body>
    <h1>Mattermost Migration</h1>
    <p>Site has moved, wait for the DNS update.</a></p>
  </body>
</html>
EOF

mkdir -p /etc/e-smith/templates-custom/etc/httpd/conf.d/zz_mattermost.conf/
cat - >/etc/e-smith/templates-custom/etc/httpd/conf.d/zz_mattermost.conf/10base <<EOF
# ns8migration
<VirtualHost *:80>
    IncludeOptional conf.d/default-virtualhost.inc
</VirtualHost>

<VirtualHost *:80>
   ServerName {{\$mattermost{'VirtualHost'} || 'mattermost.'.\$DomainName}}
   RedirectMatch 301 ^(?!/\.well-known/acme-challenge/).* https://{{\$mattermost{'VirtualHost'} || 'mattermost.'.\$DomainName}}
</VirtualHost>

<VirtualHost *:443>
  ServerName {{\$mattermost{'VirtualHost'} || 'mattermost.'.\$DomainName}}

  Alias / /var/lib/nethserver/nethserver-ns8-migration/nethserver-mattermost/webroot/
  <Directory "/var/lib/nethserver/nethserver-ns8-migration/nethserver-mattermost/webroot/">
     AllowOverride all
     Require all granted
  </Directory>

</VirtualHost>
EOF
/sbin/e-smith/expand-template /etc/httpd/conf.d/zz_mattermost.conf
systemctl reload httpd

# Terminate the rsync server
rsync "${RSYNC_ENDPOINT}"/terminate || :

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

host=$(/sbin/e-smith/config getprop mattermost VirtualHost)
ns8-action --attach "module/${MODULE_INSTANCE_ID}" configure-module '{"host": "'$host'", "lets_encrypt": false, "http2https": true}'
