#!/bin/bash

#############################################
#    USAGE:
#         sudo bash uninstallHES.sh
#    or make this file executable:
#         chmode +x uninstallHES.sh
#    and run it:
#         sudo ./uninstallHES.sh
#############################################


###SETTING BLOCK
HES_DIR="/opt/HES"
#######################################
#If you want the script to ask no questions, un comment and fill in these variables
# (USER_PASSWORD must comply with mysql policy!)

DATABASE_NAME="db"
DATABASE_USER="user"
#MYSQL_ROOT_PASSWORD="mysqlrootpasswor"
#MSSQL_SA_PASSWORD="mS sql SA password"
#######################################



JSON_FILE="$HES_DIR/appsettings.Production.json"
DEFAULTCONNECTION=$(jq -r '.ConnectionStrings.DefaultConnection' $JSON_FILE)

if [ $? -eq 0 ]; then
    echo "Parameters from json file read successfully"
else
   # ups.... 
    echo "Cannot read parameters from json file ("$HES_DIR/appsettings.Production.json")"
    exit 1
fi




PROVIDER=$(jq -r '.ConnectionStrings.Provider' $JSON_FILE)
if [[ "$PROVIDER" == "null" ]]; then
    PROVIDER="MySql"
fi
echo "PROVIDER = $PROVIDER"


if [ "$PROVIDER" = "MySql" ] ; then
    if [ -z "$MYSQL_ROOT_PASSWORD" ] 
    then
        read -p "Enter MySQL root password: " MYSQL_ROOT_PASSWORD
    fi
    #echo MYSQL_ROOT_PASSWORD = $MYSQL_ROOT_PASSWORD
    ########################### end settings zone
fi

if [ "$PROVIDER" = "MsSql" ] ; then
    if [ -z "$MSSQL_SA_PASSWORD" ] 
    then
        read -p "Enter MS SQL SA password: " MSSQL_SA_PASSWORD
    fi
    #echo MSSQL_SA_PASSWORD = $MSSQL_SA_PASSWORD
fi


###############################
# DETECT OS
d=$(dirname $0)
. ${d}/detectOS.sh

#echo "OS: $OS"
#echo "DIST: $DIST"
#echo "PSUEDONAME: $PSUEDONAME"
#echo "REV: $REV"
#echo "DistroBasedOn: $DistroBasedOn"
#echo "KERNEL: $KERNEL"
#echo "MACH: $MACH"
#echo "SUB_REV=$SUB_REV
#echo "========"

###############################

# Remove certificates
rm /etc/nginx/certs/hes.*
###############################

# Remove Nginx config
#######################################################
#PATH_TO_CONFIG_DOMAIN="/etc/nginx/conf.d"

OLD_NGINX="/etc/nginx/nginx.conf.without_HES"
if [ -f $OLD_NGINX ]; then
    mv $OLD_NGINX  "/etc/nginx/nginx.conf"
fi
systemctl restart nginx

#######################################################


# Remove HES
#######################################################
systemctl stop HES.service
systemctl disable HES.service
rm /lib/systemd/system/HES.service 
rm -rf $HES_DIR
#######################################################

if [ "$PROVIDER" = "MySql" ] ; then
    # Remove MySQL user and database
    #######################################################
    mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "DROP USER '$DATABASE_USER'@'127.0.0.1';"
    DATABASE_NAME_FOR_MYSQL=$DATABASE_NAME
    mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "DROP DATABASE $DATABASE_NAME_FOR_MYSQL;"
    #######################################################
fi

if [ "$PROVIDER" = "MsSql" ] ; then
    # Remove MS SQL user and database
     /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "$MSSQL_SA_PASSWORD" -Q "DROP DATABASE $DATABASE_NAME; DROP LOGIN [$DATABASE_USER]"
#    sqlcmd -S localhost -U SA -P "$MSSQL_SA_PASSWORD" -Q "DROP USER [user]"
#    sqlcmd -S localhost -U SA -P "$MSSQL_SA_PASSWORD" -Q "DROP LOGIN [user]"

    if [ $? -eq 0 ]; then
      echo "database and user successfully dropped"
    else
      # ups.... 
      echo "error drop user or database"
      exit 1
   fi

    #######################################################
fi


echo "the end."
