#!/bin/sh

VIRSH_AUTH='/etc/ovirt-hosted-engine/virsh_auth.conf'

die() {
    echo "FATAL: $1" >&2
    exit 1
}

caution() {
    if [ -z "${QUIET_MODE}" ]; then
    cat << __EOF__
$(tput smso) This will de-configure the host to run ovirt-hosted-engine-setup from scratch. $(tput rmso)
Caution, this operation should be used with care.

Are you sure you want to proceed? [y/n]
__EOF__
    read answer
    [ "${answer}" = "y" ] || die "Please contact support for further assistance."
    fi
}

usage() {
    cat << __EOF__
Usage: $0 [options]

    -h            - This help text.
    -q            - Quiet mode, do not prompt for confirmation. Dangerous!!!

__EOF__
}

get_HE_procs() {
	ps auxww | grep '[H]ostedEngine' | awk '{print $2}'
}

kill_HE_procs() {
	local MAX_ATTEMPTS=5
	local INITIAL_DELAY=0.1
	local NEXT_DELAY=5
	local PIDs=$(get_HE_procs)
	local left_attempts
	if [ -n "${PIDs}" ]; then
		left_attempts="${MAX_ATTEMPTS}"
		echo "  -=== Killing left-behind HostedEngine processes ===- "
		kill $PIDs
		sleep "${INITIAL_DELAY}"
		while PIDs=$(get_HE_procs) && [ -n "${PIDs}" ] && [ "${left_attempts}" -gt 0 ]; do
			sleep "${NEXT_DELAY}"
			kill $PIDs
			left_attempts=$(expr "${left_attempts}" - 1)
		done
	fi
	[ -n "${PIDs}" ] && return 1
	return 0
}

while getopts hq option; do
    case "${option}" in
        \?) usage; exit 1;;
        h) usage; exit 0;;
        q) QUIET_MODE=1;;
    esac
done

caution

echo "  -=== Destroy hosted-engine VM ===- "
hosted-engine --vm-poweroff

kill_HE_procs
/usr/bin/virsh \
    -c qemu:///system?authfile=${VIRSH_AUTH} \
    undefine \
    HostedEngine

echo "  -=== Stop HA services ===- "
systemctl stop ovirt-ha-agent
systemctl stop ovirt-ha-broker

echo "  -=== Shutdown sanlock ===- "
sanlock client shutdown -f 1

echo "  -=== Disconnecting the hosted-engine storage domain ===- "
hosted-engine --disconnect-storage

echo "  -=== De-configure VDSM networks ===- "
vdsm-tool restore-nets

echo "  -=== Stop other services ===- "
systemctl stop vdsmd
systemctl stop supervdsmd
systemctl stop libvirtd
systemctl stop momd
systemctl stop sanlock

echo "  -=== De-configure external daemons ===- "
vdsm-tool remove-config

echo "  -=== Removing configuration files ===- "
FILES="/etc/init/libvirtd.conf"
FILES+=" /etc/libvirt/nwfilter/vdsm-no-mac-spoofing.xml"
FILES+=" /etc/ovirt-hosted-engine/answers.conf"
FILES+=" /etc/ovirt-hosted-engine/hosted-engine.conf"
FILES+=" /etc/vdsm/vdsm.conf"
FILES+=" /etc/pki/vdsm/*/*.pem"
FILES+=" /etc/pki/CA/cacert.pem"
FILES+=" /etc/pki/libvirt/*.pem"
FILES+=" /etc/pki/libvirt/private/*.pem"
FILES+=" /etc/pki/ovirt-vmconsole/*.pem"
FILES+=" /var/cache/libvirt/*"
FILES+=" /var/run/ovirt-hosted-engine-ha/*"

for f in $FILES
do
   [ ! -e $f ] && echo "? $f already missing" && continue
   echo "- removing $f"
   rm -rf $f && continue
   echo "! error removing $f"
done

