#!/bin/bash
# ------------------------------------------------------------------------------
# Copyright (c) 2011-2016 SUSE Linux GmbH, Nuernberg, Germany.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, contact SUSE Linux GmbH.
#
# ------------------------------------------------------------------------------
# cs_delcib
#	Remove the pacemaker's Cluster Information Base files
# Version: 0.3 2016-02-16
# Author: L.Pinne

ERR="/dev/null"

CFG="/etc/ClusterTools2/cs_delcib"

test -s $CFG && source $CFG

FIL=${FIL:-"/var/lib/corosync/* /var/lib/heartbeat/cores/*/*
	/var/lib/pacemaker/cib/* /var/lib/pacemaker/pengine/*"}

BAK=${BAK:-"/var/adm/backup/cib.xml-$(date +%Y%m%d-%H%M%S)"}

function clear_cib(){
    local cib_new="/var/lib/pacemaker/cib/cib.xml"
    local cib_old="/var/lib/heartbeat/crm/cib.xml"

    (mv $cib_new $BAK 2>$ERR && echo "Backed up \"$cib_new\" to \"$BAK\"") ||
    	(mv $cib_old $BAK 2>$ERR && echo "Backed up \"$cib_old\" to \"$BAK\"")
    for path in $FIL; do
        rm -f $path 2>$ERR
    done
}

case $1 in
	-v|--version)
		echo -n "$(basename $0) "
        	head -11 $0 | grep "^# Version: " | cut -c3-
		exit
	;;
	-f|--force)
		/usr/bin/systemctl stop pacemaker || exit
		clear_cib
		exit
	;;
	-o|--offline)
		/usr/bin/systemctl is-active pacemaker >/dev/null 2>&1
		if [[ $? -eq  0 ]]; then
            echo "Pacemaker is active, exiting.";
            exit
        fi
        clear_cib
		exit
	;;
	*)
		echo "usage: $(basename $0) [OPTION]"
		echo
		echo " --offline	remove CIB and PE logs."
		echo " --force		stop cluster service and resources, remove CIB and PE logs."
		echo " --help		show help."
		echo " --version	show version."
		exit
	;;
esac
