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

prefix=/usr/local
installdir=$prefix/mariadb/columnstore
rpmmode=install
user=`whoami 2>/dev/null`

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`
		prefix=`dirname $prefix`
	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 "pre-install: invalid unknown argument: $arg" 1>&2
		echo "exiting..."
		exit 1
	fi
done

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

if [ $user != "root" ]; then
	export COLUMNSTORE_INSTALL_DIR=$installdir
	export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$installdir/lib:$installdir/mysql/lib
fi

cloud=`$installdir/bin/getConfig Installation Cloud`
if [ $cloud = "amazon-ec2" ] || [ $cloud = "amazon-vpc" ]; then
	if test -f /etc/fstab ; then
	    sed -i '/Columnstore\/data/d' /etc/fstab > /dev/null 2>&1
	fi
fi

#remove profile file
if [ $user != "root" ]; then
	if [ -f $prefix/.bash_profile ]; then		
		profileFile=$prefix/.bash_profile
	elif [ -f $prefix/.profile ]; then		
		profileFile=$prefix/.profile
	else
		profileFile=$prefix/.bashrc
		touch $prefix/.bashrc
	fi

	if [ -f ${profileFile}_backup ] ; then
	    mv -f ${profileFile}_backup $profileFile
	fi
else
	rm -f /etc/profile.d/columnstoreAlias.sh
fi

#remove log file directories
if [ $user == "root" ]; then
	rm -f /var/log/mariadb/columnstore/activeAlarms > /dev/null 2>&1
	rm -f /var/log/mariadb/columnstore/*.log1 > /dev/null 2>&1
fi

rm -rf $installdir/mysql/db/columnstore_log_archive > /dev/null 2>&1\

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

#get temp directory
tmpDir=`$installdir/bin/getConfig SystemConfig SystemTempFileDir`
if [[ $tmpDir = *"tmp" ]]; then
	# delete tmp files
	rm -rf $tmpDir/*
fi
rm -f $installdir/local/*.columnstore
rm -rf $installdir/local/etc/
rm -f $installdir/data/bulk/tmp/job/* >/dev/null 2>&1
rm -f $installdir/local/moveDbrootTransactionLog

lockdir=`$installdir/bin/getConfig Installation LockFileDirectory`
rm -f $lockdir/columnstore
rm -f $lockdir/mysql-Columnstore

# 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
	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

#remove OAMdbrootCheck file
rm -f $installdir/data*/OAMdbrootCheck > /dev/null 2>&1

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

cat <<EOD

NOTE: For non-root install, you will need to run the following commands as root user to
      uninstall the MariaDB ColumnStore System Logging. This can be skipped if
      running this script as part of an upgrade

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/syslogSetup.sh --installdir=$installdir --user=$user uninstall


EOD

fi

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

exit 0

