#!/bin/bash

# Colour definitions
COLOUR=""
COL_DEFAULT="\e[39m"
COL_YELLOW="\e[93m"
COL_LIGHT_RED="\e[91m"
COL_LIGHT_GREEN="\e[92m"
COL_LIGHT_BLUE="\e[34m"

RELEASE=$(cat /etc/redhat-release)
NOT_EXPECTED_OS=CentOS
echo $RELEASE
if [[ "$RELEASE" == *"$NOT_EXPECTED_OS"* ]]; then
  echo "Cannot import on current server"
  exit
fi

# Need IP
echo "Please provide the internal IP of the current server: "
read CURRENT_SERVER_IP

download_file () {
    FILE_NAME=$1
    FILE_URL=$2
    echo "Downloading $FILE_NAME from $FILE_URL"
    curl -f -k -o $FILE_NAME $FILE_URL
    RESULT=$?
    echo "Result $RESULT"
    if [ "$RESULT" != "0" ];
    then
        echo "Failed to download $FILE_NAME from $FILE_URL"
        exit
    fi
}

HOST_ARG=$1
HOST="${HOST_ARG:-localhost}" 
DB_PASSWORD=""
DB_RAW_PWD=$DB_PASSWORD
DB_USERNAME=root
EXPORT_SUFFIX=$(date +%Y_%B)
DB_SCHEMA=db-sog-shema-$EXPORT_SUFFIX.sql
DB_DATA=db-sog-$EXPORT_SUFFIX.sql
GNSERVICE_DATA=db-gnservice-$EXPORT_SUFFIX.sql
GLAM_CONFIG_TAR=glam_config_etc-$EXPORT_SUFFIX.tar.gz

EXPORT_SUFFIX=$(hostname)_$(date +%Y_%B)
if [ ! -d migration ];
then
    mkdir migration
fi
cd migration

download_file $DB_SCHEMA https://$CURRENT_SERVER_IP/$DB_SCHEMA
download_file $DB_DATA https://$CURRENT_SERVER_IP/$DB_DATA
download_file $GNSERVICE_DATA https://$CURRENT_SERVER_IP/$GNSERVICE_DATA
download_file $GLAM_CONFIG_TAR https://$CURRENT_SERVER_IP/$GLAM_CONFIG_TAR

echo "Please enter the database version:"
read DATABASE_VERSION

while [ "$DATABASE_VERSION" == "" ]
do
    echo "Please enter the database version:"
    read DATABASE_VERSION
done

COLOUR=""
MYSQL_PROMPT="MySQL"
DONE="no"
while [ "$DONE" == "no" ];
do
    printf "$COLOUR"
    echo "$MYSQL_PROMPT connection required. Please enter MySQL credentials"
    printf "$COL_DEFAULT"
    read -p "Username: " -e DB_USERNAME
    read -p "Password: " -e DB_PASSWORD
    MYSQL_PROMPT="Valid MySQL"    
    COLOUR=$COL_LIGHT_RED
    if [ "$DB_USERNAME" == "quit" ] && [ "$DB_PASSWORD" == "quit" ];
    then
        print_notice "Exiting..."
        # User exit application stop inform outside database not properly checked
        exit 1
    fi

    if [ "$DB_PASSWORD" != "" ];
    then
        DB_RAW_PWD=$DB_PASSWORD
        # mysql expects password to follow straight after the -p flag.
        DB_PASSWORD="-p$DB_PASSWORD"
    fi
    TEST_MYSQL=$(mysql -h$HOST -u $DB_USERNAME $DB_PASSWORD -e "show grants for current_user" 2>&1)
    # Expect a user with all privileges
    ERR_MYSQL=$(echo $TEST_MYSQL | grep ERROR)
    GRANT_ENOUGH=$(echo $TEST_MYSQL | grep -i 'ALL PRIVILEGES')    
    if [ "$ERR_MYSQL" == "" ] && [ "$GRANT_ENOUGH" != "" ];
    then
        DONE="yes"  
    fi    
done


# echo "Configure version"
echo $DATABASE_VERSION > /usr/local/glam/database/meta/version
echo $DATABASE_VERSION > /usr/local/glam/database/meta/installed

# echo "Clear database"
mysql -h ${HOST} --user=${DB_USERNAME} $DB_PASSWORD -e 'drop database IF EXISTS sog;create database sog;drop database IF EXISTS gnservice;create database gnservice;'
echo "Import sog schema"
mysql -h ${HOST} -u ${DB_USERNAME} $DB_PASSWORD sog < $DB_SCHEMA
echo "Import sog data"
mysql -h ${HOST} -u ${DB_USERNAME} $DB_PASSWORD sog < $DB_DATA
echo "Import gnservice"
mysql -h ${HOST} -u ${DB_USERNAME} $DB_PASSWORD gnservice < $GNSERVICE_DATA

echo "Sync sog"
DB_HOST=localhost glam-database-sync
echo "Sync gnservice"
DB_HOST=localhost gnservice-database-sync

echo "Restore configuration"
tar -xvzf $GLAM_CONFIG_TAR -C /etc --strip 1

echo "Restart Glam Key Daemon"
systemctl restart glamd
echo "Restart Glam AND"
systemctl restart glam_and
echo "Restart Glam IoT"
systemctl restart glam_iotd
echo "Restart Notification Service"
systemctl restart gnservice

