#!/bin/bash

#
# Copyright (C) 2017 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.
#

systemctl is-active --quiet rh-postgresql12-postgresql
if [[ $? -ne 0 ]]; then
        echo "[ERROR] postgresql12 is not running, exiting"
        exit 1
fi
su - postgres -c "scl enable rh-postgresql12 -- psql --port=55434 -lqt | cut -d \| -f 1 | grep -q -w mattermost"
if [[ $? -eq 0 ]]; then # mattermost database exists, exiting
    exit 0
fi
su - postgres -c "scl enable rh-postgresql94 -- psql --port=55432 -lqt | cut -d \| -f 1 | grep -q -w mattermost"
if [[ $? -eq 0 ]]; then
    echo "[NOTICE] Migrating data from postgresql 9.4 to postgresql 12"
    databases=$(su - postgres -c "scl enable rh-postgresql94 -- psql -q -A -t --port=55432 -c $'SELECT datname FROM pg_database WHERE datistemplate = false AND datname <> \'postgres\';'")
    if [[ $? -ne 0 ]]; then
        echo "[ERROR] An error occured while retrieving list of databases"
        exit 1
    fi
    systemctl stop rh-postgresql94-postgresql rh-postgresql12-postgresql
    su - postgres -c "export LD_LIBRARY_PATH=/opt/rh/rh-postgresql94/root/usr/lib64:/opt/rh/rh-postgresql12/root/usr/lib64:$LD_LIBRARY_PATH && scl enable rh-postgresql12 -- pg_upgrade -b /opt/rh/rh-postgresql94/root/usr/bin -B /opt/rh/rh-postgresql12/root/usr/bin -d /var/opt/rh/rh-postgresql94/lib/pgsql/data -D /var/opt/rh/rh-postgresql12/lib/pgsql/data"
    if [[ $? -ne 0 ]]; then
        echo "[ERROR] An error occured during postgresql migration"
        systemctl start rh-postgresql94-postgresql rh-postgresql12-postgresql
        exit 1
    fi
    echo "[NOTICE] Migration completed successfully"
    if [[ "$databases" == "mattermost" ]]; then
        # only mattermost database is present, delete all postgres 9.4 data
        /sbin/e-smith/config setprop rh-postgresql94-postgresql status disabled
        systemctl disable rh-postgresql94-postgresql
        rm -rf /var/opt/rh/rh-postgresql94/lib/pgsql/data/*
        echo "[NOTICE] postgresql 9.4 is no longer needed and has been disabled"
    else
        echo "[NOTICE] postgresql 9.4 seems to be still in use. Removing only mattermost database"
        systemctl start rh-postgresql94-postgresql
        su - postgres -c "scl enable rh-postgresql94 -- psql -q -A --port=55432 -c 'DROP DATABASE mattermost;'"
        if [[ $? -ne 0 ]]; then
            echo "[ERROR] Error while dropping mattermost database"
        fi
    fi
    systemctl start rh-postgresql12-postgresql
else
    echo "[NOTICE] Creating mattermost database from scratch"
    tmp_sql=`mktemp`
    chmod a+r $tmp_sql
    password=`perl -e "use NethServer::Password; print NethServer::Password::store('mattermost');"`
    cat << EOF > $tmp_sql
CREATE DATABASE mattermost WITH ENCODING 'UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE=template0;
CREATE USER mattuser WITH PASSWORD '$password';
ALTER USER mattuser WITH SUPERUSER;
GRANT ALL PRIVILEGES ON DATABASE mattermost to mattuser;
EOF
    su - postgres  -c "scl enable rh-postgresql12 -- psql --port=55434 < $tmp_sql" >/dev/null
    rm -f $tmp_sql
fi
