#usage
#bash UninstallHES.sh

###SETTING BLOCK
#######################################

PROVIDER="MySql"
LOGFILE="/dev/null"
HESSERVICE="HES"
HES_DIR="/opt/HES"
USE_MYSQL_ROOT_PASSWORD="1"
MYSQL_ROOT_PASSWORD=""
X509_FOLDER=""

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


while [ -n "$1" ]
do
        if [ -z "$2" ]  
        then
                echo "ERROR! no values specified after "$1" !" | tee -a $LOG_FILE
                echo "$USAGE"
                exit 1
        fi
        param="$2"

        case "$1" in
                --provider)
                        PROVIDER=$param
                        shift ;;
                --hes_dir)
                        HES_DIR=$param
                        shift ;;
                --logfile)
                        LOGFILE=$param
                        shift ;;
                --domain_name)
                        DOMAIN_NAME=$param
                        shift ;;
                --database_user)
                        DATABASE_USER=$param
                        shift ;;
                --database_name)
                        DATABASE_NAME=$param
                        shift ;;
                --hes_service)
                        HESSERVICE=$param
                        shift ;;
                --use_mysql_root_password)
                        USE_MYSQL_ROOT_PASSWORD=$param
                        shift ;;
                --mysql_root_password)
                        MYSQL_ROOT_PASSWORD=$param
                        shift ;;
                --x509_folder)
                        X509_FOLDER=$param
                        shift ;;
                --) shift
                        break ;;
                        *) echo "ERROR! $1 is not an option"
                        echo "$USAGE"
                        exit 1
                esac
                shift
done



echo "DATABASE_NAME = $DATABASE_NAME"

#MYSQL_ROOT_PASSWORD=""
#######################################

#domain name
rm /etc/nginx/certs/$DOMAIN_NAME.*
###############################


# Remove Nginx config
#######################################################

rm /etc/nginx/conf.d/$DOMAIN_NAME.conf
systemctl restart nginx

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

echo Removig  HES service..

# Remove HES
#######################################################


systemctl stop $HESSERVICE.service
systemctl disable $HESSERVICE.service
rm /lib/systemd/system/$HESSERVICE.service 


rm -rf $HES_DIR
rm -rf $HES_DIR-*


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


echo Removig MySQL user...

# Remove MySQL user and database
#######################################################

if [ "$USE_MYSQL_ROOT_PASSWORD" = "1" ] ; then
    mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "DROP USER '$DATABASE_USER'@'%';"
else
    mysql -u root -e "DROP USER '$DATABASE_USER'@'%';"
fi




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

# if set X509_FOLDER, then delete X509_FILE 
if [ -n "$X509_FOLDER" ] ; then
        QUERY="USE $DATABASE_NAME;  SELECT Value FROM AppSettings where id = 'dp';"
        set -f        # disable globbing
        IFS=$'\n'   # set field separator to NL (only)
        arr=($(mysql -u root -p"$MYSQL_ROOT_PASSWORD" -N  -e "$QUERY" --skip-column-names ))
        for i in "${arr[@]}"
        do
                #echo "i = $i"
                THUMBRPRINT=$(echo "$i" | jq -r '.DatabaseProtectionThumbprint')
                # echo "$i" | jq -r '.DatabaseProtectionThumbprint'
                CERT_FILE=THUMBRPRINT_FOLDER
                X509_FILE="$X509_FOLDER/$THUMBRPRINT.pfx"
                # echo "X509_FILE = $X509_FILE"
                if [ -f $X509_FILE ]; then
                        rm $X509_FILE
                fi
        done
fi # delete X509_FILE

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

echo Removig MySQL database...


DATABASE_NAME_FOR_MYSQL=$DATABASE_NAME 
#if name of database base contains a character "-"
# then the name of the base must be enclosed in reverse apostrophe
if [[ "$DATABASE_NAME" == *"-"* ]]; then
  DATABASE_NAME_FOR_MYSQL="\`$DATABASE_NAME\`"
fi

if [ "$USE_MYSQL_ROOT_PASSWORD" = "1" ] ; then
    mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "DROP DATABASE $DATABASE_NAME_FOR_MYSQL;"
else
    mysql -u root -e "DROP DATABASE $DATABASE_NAME_FOR_MYSQL;"
fi

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





echo finish!
