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

prefix=/usr/local
installdir=$prefix/mariadb/columnstore
rpmmode=install
user=`whoami 2>/dev/null`
SUDO=" "
if [ $user != "root" ]; then
        SUDO="sudo "
fi

quiet=0

for arg in "$@"; do
	if [ `expr -- "$arg" : '--prefix='` -eq 9 ]; then
		prefix="`echo $arg | awk -F= '{print $2}'`"
		installdir=$prefix/mariadb/columnstore
	elif [ `expr -- "$arg" : '--rpmmode='` -eq 10 ]; then
		rpmmode="`echo $arg | awk -F= '{print $2}'`"
	elif [ `expr -- "$arg" : '--installdir='` -eq 13 ]; then
		installdir="`echo $arg | awk -F= '{print $2}'`"
		prefix=`dirname $installdir`
	elif [ `expr -- "$arg" : '--user='` -eq 7 ]; then
		user="`echo $arg | awk -F= '{print $2}'`"
	elif [ `expr -- "$arg" : '--quiet'` -eq 7 ]; then
		quiet=1
	else
		echo "post-uninstall: ignoring unknown argument: $arg" 1>&2
	fi
done

#stop services
$installdir/bin/columnstore stop > /dev/null 2>&1
$installdir/myql/columnstore-Mysql stop > /dev/null 2>&1

# uninstall OS scripts updated by postConfigure
if test -f /etc/exports.columnstoreSave ; then
	$SUDO mv -f /etc/exports.columnstoreSave /etc/exports > /dev/null 2>&1
else
	$SUDO rm -f /etc/exports > /dev/null 2>&1
fi

export COLUMNSTORE_INSTALL_DIR=$installdir

cloud=`$COLUMNSTORE_INSTALL_DIR/bin/getConfig Installation Cloud`
if [ $cloud = "amazon-ec2" ] || [ $cloud = "amazon-vpc" ]; then
	if test -f /etc/fstab ; then
		$SUDO sed -i '/Columnstore\/data/d' /etc/fstab > /dev/null 2>&1
	fi
fi
 
#remove log file directories
#rm -rf /var/log/mariadb/columnstore > /dev/null 2>&1
#rm -f $installdir/mysql/db/*.err > /dev/null 2>&1
$SUDO rm -f /var/log/mariadb/columnstore/activeAlarms > /dev/null 2>&1
$SUDO rm -f /var/log/mariadb/columnstore/*.log1 > /dev/null 2>&1
rm -rf $installdir/mysql/db/columnstore_log_archive > /dev/null 2>&1
$SUDO rm -f /etc/default/columnstore

# remove Mariab Columnstore Log Rotate File abd Transaction Log File
$SUDO rm -f /etc/logrotate.d/columnstore > /dev/null 2>&1
$SUDO rm -f /etc/cron.d/transactionLog > /dev/null 2>&1

# delete Mariab Columnstore shared memory segments
$installdir/bin/clearShm  > /dev/null 2>&1

# delete prat files
$SUDO rm -f /etc/cron.d/ps > /dev/null 2>&1
$SUDO rm -f /etc/pscollect > /dev/null 2>&1
$SUDO /etc/init.d/crond reload  > /dev/null 2>&1
$SUDO systemctl reload crond.service > /dev/null 2>&1

# delete tmp files
rm -f $installdir/local/*.columnstore
rm -rf $installdir/local/etc/
rm -rf /tmp/bucketreuse
rm -f /tmp/columnstore.txt
rm -f /tmp/dbbuilder.*
rm -f /tmp/dbrmfiles
$SUDO rm -f /var/lock/subsys/columnstore
rm -f /tmp/pkgcheck
rm -f /tmp/upgrade-status.log.*
rm -f /tmp/mount.log
rm -f $installdir/data/bulk/tmpjob/* >/dev/null 2>&1
rm -rf /tmp/columnstore_tmp_files
rm -f $installdir/local/moveDbrootTransactionLog

#delete any old setenv files under home directory
if [ $user = "root" ]; then
	rm -f /root/setenv-hdfs*
else
	rm -f /home/$user/setenv-hdfs*
fi

# delete core files
#rm -f /var/log/mariadb/columnstore/corefiles/* > /dev/null 2>&1

#uninstall MariaDB Columnstore system logging
if [ -x $installdir/bin/syslogSetup.sh ]; then
	if [ $user = "root" ]; then
		$installdir/bin/syslogSetup.sh uninstall >/dev/null 2>&1
	else
		$SUDO $installdir/bin/syslogSetup.sh uninstall >/dev/null 2>&1
	fi
fi

#remove the start service command
if [ $user = "root" ]; then
	systemctl=`which systemctl 2>/dev/null`
	if [ -n "$systemctl" ]; then

		systemctl disable columnstore >/dev/null 2>&1
		rm -f /usr/lib/systemd/system/columnstore.service
		rm -f /lib/systemd/system/columnstore.service
	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
fi

if [ $quiet != 1 ]; then
	#make copy of Columnstore.xml
	/bin/cp -f $installdir/etc/Columnstore.xml $installdir/etc/Columnstore.xml.rpmsave > /dev/null 2>&1
	/bin/cp -f $installdir/mysql/my.cnf $installdir/mysql/my.cnf.rpmsave > /dev/null 2>&1
	cp $installdir/bin/myCnf-include-args.text $installdir/bin/myCnf-include-args.text.rpmsave >& /dev/null
	rm -f $installdir/etc/AlarmConfig.xml.installSave
fi

#umount file systems
if [ $user = "root" ]; then
	rm -f $installdir/data*/OAMdbrootCheck > /dev/null 2>&1
else
	$SUDO rm -f $installdir/data*/OAMdbrootCheck > /dev/null 2>&1
fi

#remove library paths
if [ $user = "root" ]; then
        rm -f /etc/ld.so.conf.d/columnstore.conf
        ldconfig
else
	$SUDO rm -f /etc/defaults/columnstore
fi

#cleanup profile file
if [ $user = "root" ]; then
	rm -f /etc/profile.d/columnstore*
else
	$SUDO rm -f /etc/profile.d/columnstore*
fi

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

exit 0

