#!/usr/bin/env bash

info () {
    echo -e "\033[1m info:   \033[0m $@ ";
}

ok () {
    echo -e "\033[1m done:  \033[0m \033[32m $@ \033[0m";
}

error () {
    echo -e  "\033[1m ERROR:\033[0m \033[31m $@\033[0m";
}


read -p "QoE Stor will be completely removed with all data… Are you sure??? (Enter yes or no)? " answer

if [ "$answer" != "yes" ]; then
  exit
fi

info "Removing all data and DB..."
clickhouse-client --query="drop database qoestor"

info "Disabling services..."
sh /var/qoestor/backend/app_bash/receivers_stop.sh
systemctl stop clickhouse-server
systemctl disable clickhouse-server

info "Remove cron jobs..."
crontab -l > allcrons
sed -i '/qoestor/d' allcrons
crontab allcrons
rm -f allcrons


info "Disabling ports..."
FULLFLOW_PORT=1500
CLICKSTREAM_PORT=1501
META_PORT=1502
CLICKHOUSE_PORT=8123
CLICKHOUSE_NATIVE_PORT=9000

firewall-cmd --permanent --remove-port=$FULLFLOW_PORT/tcp
firewall-cmd --permanent --remove-port=$CLICKSTREAM_PORT/tcp
firewall-cmd --permanent --remove-port=$META_PORT/tcp
firewall-cmd --permanent --remove-port=$CLICKHOUSE_PORT/tcp
firewall-cmd --permanent --remove-port=$CLICKHOUSE_NATIVE_PORT/tcp
firewall-cmd --reload

info "Remove rpms fastor, ipfixreceiver2, clickhouse-server..."
yum remove -y fastor ipfixreceiver2 clickhouse-server clickhouse-client

rm -rf /var/qoestor

rm -- "$0"
