#!/bin/bash
#
# Bitlane Agent Uninstaller
#

set -euo pipefail

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

echo -e "${RED}Uninstalling Bitlane Agent...${NC}"

WG_INTERFACE=$(cat /etc/bitlane/agent.conf 2>/dev/null | python3 -c "import sys,json;print(json.load(sys.stdin).get('wg_interface','wg0'))" 2>/dev/null || echo "wg0")

# Stop services
systemctl stop bitlane-agent 2>/dev/null || true
systemctl disable bitlane-agent 2>/dev/null || true

# Remove WireGuard interface
wg-quick down "$WG_INTERFACE" 2>/dev/null || true
systemctl disable "wg-quick@${WG_INTERFACE}" 2>/dev/null || true

# Remove files
rm -f /etc/systemd/system/bitlane-agent.service
rm -rf /opt/bitlane
rm -rf /etc/bitlane
rm -f /var/log/bitlane-agent.log

systemctl daemon-reload

echo -e "${GREEN}Bitlane Agent has been uninstalled.${NC}"
echo "Note: WireGuard packages were not removed. Run 'apt remove wireguard' if needed."
