#!/usr/bin/bash

PLUGIN_DIR='/var/cpanel/plugins/monitoring'
#PERL_BIN='/usr/local/cpanel/3rdparty/bin/perl'

/usr/bin/echo "Removing the server monitoring configuration before an image snapshot"

/usr/bin/rm -vf "/root/.360monitoring/config"
/usr/bin/rm -vf "/etc/agent360-token.ini"
/usr/bin/rm -vf "/etc/agent360.ini"
/usr/bin/rm -vf "/var/cpanel/whm/nvdata/root/cpanel.360-monitoring.plugin.cache"
/usr/bin/rm -vf "/var/cpanel/whm/nvdata/root/cpanel.360-monitoring.plugin.yaml"

( systemctl list-unit-files | grep "agent360" ) &&
   /usr/bin/echo "Stoping and disabling the server monitoring agent" &&
   /usr/bin/systemctl disable --now agent360

$PLUGIN_DIR/uninstall.sh || true;

# Note: The above steps are only needed on servers where both the cpanel-monitoring-plugin is installed
# and a 360 Monitoring users, or the 360 monitoring agent have been setup. These preconditions are limited
# to specific company ids. So we exit 0 even if one or more of the above steps fail.

# Note2: This clean_snapshot is different than pkg uninstalls because it does not remove the hooks & systemd
# timers, as these lines would do:
#  /usr/local/cpanel/bin/manage_hooks delete module Whostmgr::360Monitoring::Hooks::ServerRoleChange || true
#  /usr/local/cpanel/scripts/setup_systemd_timer_for_plugins --app_name monitoring --method remove || true
#  $PERL_BIN -e 'use Cpanel::Plugins::State (); Cpanel::Plugins::State::install("cpanel.360-monitoring.plugin")

exit 0
