#!/bin/bash
clear
ROOT_UID=0
E_NOTROOT=67

if [ "$UID" -ne "$ROOT_UID" ]; then
  echo "To run the script require root privileges."
  exit $E_NOTROOT
fi

if ps -C systemd | grep -q '^ *1 ' ;then

    if [ -f /etc/systemd/system/grdcontrol.service ]; then
        # disable on boot
        systemctl -q disable grdcontrol.service
        # stop service
        systemctl -q stop grdcontrol.service
        # remove systemd config symbolic link
        rm -rf /etc/systemd/system/grdcontrol.service
    fi

else

# echo stop service grdcontrold
if pgrep -x "grdcontrold" > /dev/null ; then
    sh /etc/init.d/grdcontrol stop
else
    rm -rf /var/run/grdcontrold.pid
fi

# check OS type
if [ -e /etc/rc.d/init.d/functions ]; then
    INSTALL_OS="RedHat"
elif [ -e /etc/rc.status ]; then
    INSTALL_OS="SUSE"
elif [ -e /etc/debian_version ]; then
    INSTALL_OS="Debian"
else
    echo "Unsupported Linux distribution."
    exit 7
fi

case $INSTALL_OS in
SUSE)
    insserv -r grdcontrol
    ;;
Debian)
    update-rc.d -f grdcontrol remove
    ;;
RedHat)
    /sbin/chkconfig --del grdcontrol
    ;;
esac

# remove files
#rm -rf /usr/sbin/grdcontrold
rm -rf /etc/init.d/grdcontrol

fi

rm /usr/bin/license_wizard
rm -rf /opt/guardant/grdcontrol
