#!/usr/bin/env bash

set -e

BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Uninstall cjdns
sudo systemctl disable cjdns.service 2>/dev/null || true
sudo systemctl stop cjdns.service 2>/dev/null || true
sudo rm -f /usr/bin/cjdroute
sudo rm -f /lib/systemd/system/cjdns.service
sudo rm -f /lib/systemd/system/cjdns-resume.service
sudo rm -rf /opt/cjdns

if [ -f "/etc/cjdroute.conf" ]; then
    echo "Found /etc/cjdroute.conf"
    read -p "Keep your cjdns identity (Y/n)? " -n 1 -r
    echo ""
    if [[ $REPLY =~ ^[Nn]$ ]]; then
        echo -e "\e[1;31mRemoving /etc/cjdroute.conf\e[0m"
        sudo rm -f /etc/cjdroute.conf
    else
        echo -e "\e[1;32mKeeping /etc/cjdroute.conf\e[0m"
    fi
fi

# Uninstall optional modules
source "$BASE_DIR/mesh-point/uninstall"
source "$BASE_DIR/hostapd/uninstall"
source "$BASE_DIR/ipfs/uninstall"
source "$BASE_DIR/prometheus-node-exporter/uninstall"
source "$BASE_DIR/prometheus-server/uninstall"
source "$BASE_DIR/grafana/uninstall"
source "$BASE_DIR/extra-tools/uninstall"

sudo systemctl daemon-reload

# Uninstall status script
sudo rm -f /usr/local/bin/status

# Remove .bash_profile
rm -f ~/.bash_profile
