#!/usr/bin/bash
#
# $Id: post-uninstall 421 2007-04-05 15:46:55Z dhill $
#
# pre-uninstall steps for columnstore install

running_systemd() {
    if  [ "$(ps --no-headers -o comm 1)" = "systemd" ]; then
        echo 0
    else
        echo 1
    fi
}

if [[ -f /etc/mysql/debian.cnf ]]; then
  MDB="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf"
else
  MDB="/usr/bin/mysql"
fi

$MDB <<EOD
DROP DATABASE IF EXISTS infinidb_querystats;
DELETE FROM mysql.func WHERE dl="ha_columnstore.so";
DELETE FROM mysql.func WHERE dl="libregr_mysql.so";
DELETE FROM mysql.func WHERE dl="libudf_mysql.so";
ALTER TABLE calpontsys.systable ENGINE=MyISAM;
ALTER TABLE calpontsys.syscolumn ENGINE=MyISAM;
DROP DATABASE IF EXISTS calpontsys;
DROP PROCEDURE IF EXISTS infinidb_querystats.calSetUserPriority;
DROP PROCEDURE IF EXISTS infinidb_querystats.calRemoveUserPriority;
DROP PROCEDURE IF EXISTS infinidb_querystats.calShowProcessList;
DROP DATABASE IF EXISTS columnstore_info;
EOD

# Test we are using systemd
systemctl cat mariadb-columnstore.service > /dev/null 2>&1
if [ $? -eq 0 ] && [ $(running_systemd) -eq 0 ]; then
    systemctl stop mariadb-columnstore >/dev/null 2>&1
else
    PROGS='PrimProc ExeMgr DMLProc DDLProc WriteEngineServer StorageManager controllernode workernode'
    kill $(pidof $PROGS) > /dev/null
    sleep 3
    kill -9 $(pidof $PROGS) > /dev/null
    clearShm  > /dev/null 2>&1
fi

if [ -n "$(pgrep -x ProcMon)" ] || [ -n "$(pgrep -x ProcMgr)" ];then
   # Old system must be running, kill ProcMon/ProcMgr
    pkill ProcMon
    pkill ProcMgr
fi

rm -f /etc/profile.d/columnstoreAlias.sh
rm -f /var/log/mariadb/columnstore/*.log1 > /dev/null 2>&1

#get temp directory
tmpDir=`/usr/bin/mcsGetConfig SystemConfig SystemTempFileDir`
if [[ $tmpDir = *"tmp" ]]; then
	# delete tmp files
	rm -rf $tmpDir/*
fi

rm -f /var/lib/columnstore/local/*.columnstore
rm -rf /var/lib/columnstore/local/etc/
rm -f /var/lib/columnstore/local/moveDbrootTransactionLog
rm -rf /var/log/mariadb/columnstore
rm -rf /tmp/columnstore_tmp_files

lockdir=`/usr/bin/mcsGetConfig Installation LockFileDirectory`
rm -f $lockdir/columnstore # Dangerous remove command

#uninstall MariaDB Columnstore system logging
columnstoreSyslogSetup.sh uninstall >/dev/null 2>&1

#remove the start service command
systemctl=`which systemctl 2>/dev/null`
if [ -n "$systemctl" ] && [ $(running_systemd) -eq 0 ]; then

    systemctl disable mariadb-columnstore >/dev/null 2>&1
    systemctl disable mcs-controllernode > /dev/null 2>&1
    systemctl disable mcs-ddlproc > /dev/null 2>&1
    systemctl disable mcs-dmlproc > /dev/null 2>&1
    systemctl disable mcs-exemgr > /dev/null 2>&1
    systemctl disable mcs-primproc > /dev/null 2>&1
    systemctl disable mcs-workernode@1 > /dev/null 2>&1
    systemctl disable mcs-workernode@2 > /dev/null 2>&1
    systemctl disable mcs-writeengineserver > /dev/null 2>&1
    systemctl disable mcs-loadbrm > /dev/null 2>&1
    systemctl disable mcs-storagemanager > /dev/null 2>&1

    rm -f /usr/lib/systemd/system/mariadb-columnstore.service
    rm -f /lib/systemd/system/mariadb-columnstore.service
    rm -f /usr/lib/systemd/system/mcs-controllernode.service
    rm -f /lib/systemd/system/mcs-controllernode.service
    rm -f /usr/lib/systemd/system/mcs-ddlproc.service
    rm -f /lib/systemd/system/mcs-ddlproc.service
    rm -f /usr/lib/systemd/system/mcs-dmlproc.service
    rm -f /lib/systemd/system/mcs-dmlproc.service
    rm -f /usr/lib/systemd/system/mcs-exemgr.service
    rm -f /lib/systemd/system/mcs-exemgr.service
    rm -f /usr/lib/systemd/system/mcs-primproc.service
    rm -f /lib/systemd/system/mcs-primproc.service
    rm -f /usr/lib/systemd/system/mcs-workernode@.service
    rm -f /lib/systemd/system/mcs-workernode@.service
    rm -f /usr/lib/systemd/system/mcs-writeengineserver.service
    rm -f /lib/systemd/system/mcs-writeengineserver.service
    rm -f /usr/lib/systemd/system/mcs-loadbrm.service
    rm -f /lib/systemd/system/mcs-loadbrm.service
    rm -f /usr/lib/systemd/system/mcs-storagemanager.service
    rm -f /lib/systemd/system/mcs-storagemanager.service

    systemctl daemon-reload
    # remove flag to prevent clusters using shared storage from initializing columnstore more than once
    IFLAG=/var/lib/columnstore/storagemanager/storagemanager-lock
    if [ -e $IFLAG ]; then
        rm $IFLAG
    fi
else
    chkconfig=`which chkconfig 2>/dev/null`
    if [ -n "$chkconfig" ]; then
        chkconfig columnstore off > /dev/null 2>&1
        chkconfig --del columnstore > /dev/null 2>&1
        rm -f /etc/init.d/columnstore > /dev/null 2>&1
    else
        updaterc=`which update-rc.d 2>/dev/null`
        if [ -n "$updaterc" ]; then
            update-rc.d -f columnstore remove > /dev/null 2>&1
            rm -f /etc/init.d/columnstore > /dev/null 2>&1
        fi
    fi
fi

#make copy of Columnstore.xml
/bin/cp -f /etc/columnstore/Columnstore.xml /etc/columnstore/Columnstore.xml.rpmsave > /dev/null 2>&1
/bin/cp -f /etc/columnstore/storagemanager.cnf /etc/columnstore/storagemanager.cnf.rpmsave > /dev/null 2>&1
/bin/cp -f /etc/my.cnf.d/columnstore.cnf /etc/my.cnf.d/columnstore.cnf.rpmsave > /dev/null 2>&1
rm -f /etc/columnstore/AlarmConfig.xml.installSave

#remove OAMdbrootCheck file
rm -f /var/lib/columnstore/data*/OAMdbrootCheck > /dev/null 2>&1

rm -f /etc/rsyslog.d/49-columnstore.conf > /dev/null 2>&1
rm -rf /tmp/columnstore_tmp_files > /dev/null 2>&1

#tell user to run post configure script
echo " "
echo "Mariab Columnstore uninstall completed"

exit 0

