#!/bin/bash
#
# $Id: columnstore 3704 2013-08-07 03:33:20Z bwilkinson $
#
# columnstore        Starts MariaDB Columnstore database platform
#
#
# chkconfig: 2345 99 99
# description: MariaDB Columnstore is a database platform that utilizes Mysql
#
### BEGIN INIT INFO
# Provides:          columnstore
# Required-Start:    $local_fs $remote_fs $network $syslog $all
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop MariaDB Columnstore DW DBMS
### END INIT INFO

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

USER=`whoami 2>/dev/null`

# Source function library.
if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
fi

. /usr/share/columnstore/columnstore_functions

mt=`module_type`
mid=`module_id`

has_um=`/usr/bin/mcsGetConfig SystemModuleConfig ModuleCount2`
if [ "x$has_um" = x ]; then
	has_um=0
fi

lockdir=`/usr/bin/mcsGetConfig Installation LockFileDirectory`

#get temp directory
tmpDir=`/usr/bin/mcsGetConfig SystemConfig SystemTempFileDir`
mkdir $tmpDir >/dev/null 2>&1
if [ $USER == "root" ]; then
	chmod 777 $tmpDir
fi

checkInstallSetup() {
	InitialInstallFlag=`/usr/bin/mcsGetConfig -c /etc/columnstore/Columnstore.xml Installation InitialInstallFlag`
	if [ $InitialInstallFlag != "y" ]; then
		echo "Please run the postConfigure install script, check the Installation Guide"
		echo "for additional details"
		rm -f $lockdir/columnstore
		exit 1
	fi
}

start() {
	if [ -f $lockdir/columnstore ]; then
		echo "MariaDB Columnstore Database Platform already running"
		exit 0
	fi

    # Make sure ProcMon is down first
    touch ${tmpDir}/StopColumnstore
    pkill ProcMon
    pkill ProcMgr

	(mkdir -p $lockdir && touch $lockdir/columnstore) >/dev/null 2>&1

	#checkInstallSetup

	CoreFileFlag=`/usr/bin/mcsGetConfig -c /etc/columnstore/Columnstore.xml Installation CoreFileFlag`
	if [ $CoreFileFlag = "y" ]; then
    #columnstore core files
		ulimit -c unlimited  > /dev/null 2>&1
		sysctl -q -w kernel.core_uses_pid=1  > /dev/null 2>&1
		sysctl -q -w kernel.core_pattern=/var/log/mariadb/columnstore/corefiles/core.%e.%p  > /dev/null 2>&1

	fi

	RETVAL=0
    moduleName="$(cat /var/lib/columnstore/local/module)"
	echo "Starting module $moduleName of MariaDB Columnstore Database Platform"
	rm -f ${tmpDir}/StopColumnstore
	exec columnstore_run.sh -l ${tmpDir} ProcMon  > /dev/null 2>&1 &

	return $RETVAL
}	
stop() {
    moduleName="$(cat /var/lib/columnstore/local/module)"
	echo "Stopping module $moduleName of MariaDB Columnstore Database Platform"
	touch ${tmpDir}/StopColumnstore
    mcsadmin stopModule $moduleName y
	RETVAL=$?
	rm -f $lockdir/columnstore
	fuser -k 8604/tcp > /dev/null 2>&1
    # Test we are using systemd
    systemctl cat mariadb.service > /dev/null 2>&1
    if [ $? -eq 0 ] && [ $(running_systemd) -eq 0 ]; then
        systemctl stop mariadb.service > /dev/null 2>&1
    else
        pkill mysqld
    fi
    pkill ProcMon
    pkill ProcMgr
	return $RETVAL
}
restart() {
	stop
	start
}
status() {
	isrunning=0
	if [ $EUID -eq 0 ]; then
		if [ -f $lockdir/columnstore ]; then
			isrunning=1
		fi
	else
		pgrep ProcMon >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			isrunning=1
		fi
	fi
	if [ $isrunning -ne 0 ]; then
		echo "MariaDB Columnstore is running"
	else
		echo "MariaDB Columnstore is not running"
		exit 3
	fi
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	restart
	;;
condrestart)
	[ -f $lockdir/columnstore ] && restart || :
	;;
status)
	status
	;;
*)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $?

