#!/bin/bash

# UniSASE Agent Pre-uninstallation Script
# Executed during the uninstallation process to clean up resources

echo "Executing UniSASE Agent pre-uninstallation cleanup..."

# Environment Variables
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. "${SCRIPT_DIR}/appenv.conf"
# shellcheck source=permissions.sh
. "${SCRIPT_DIR}/permissions.sh"

# Stop the service
echo "Stopping UniSASE Agent service..."
if [ -f "${RAM_APP_CTRL}" ]; then
    "${RAM_APP_CTRL}" stop
fi

# Withdraw the 4G/5G modem tty access granted by permissions.sh. This runs here
# rather than in afteruninstall because afteruninstall deletes APP_ROOT_DIR,
# which is where permissions.sh lives. Deleting the udev rule alone would not
# close the nodes again - see remove_agent_modem_tty_access for why the explicit
# chmod is required. A no-op on a machine that never had a modem.
echo "Withdrawing 4G/5G modem tty access..."
remove_agent_modem_tty_access

echo "Pre-uninstallation cleanup completed."
exit 0
