#!/bin/bash # ============================================================================= # RDC Simulation - Uninstall Script # ============================================================================= # Removes ArduPilot SITL, Gazebo plugin, and related configurations. # ROS 2 and Gazebo system packages are NOT removed (use apt to remove if needed). # # Usage: # ./setup/uninstall.sh # Interactive mode # ./setup/uninstall.sh --all # Remove everything without prompts # ./setup/uninstall.sh --ardupilot # Remove ArduPilot only # ============================================================================= set -e echo "==============================================" echo " RDC Simulation - Uninstall" echo "==============================================" echo "" # Get script directory and project root SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Directories to remove ARDUPILOT_HOME="$HOME/ardupilot" ARDUPILOT_GZ="$HOME/ardupilot_gazebo" VENV_DIR="$PROJECT_ROOT/venv" ACTIVATE_SCRIPT="$PROJECT_ROOT/activate.sh" # Parse arguments REMOVE_ALL=false REMOVE_ARDUPILOT=false INTERACTIVE=true for arg in "$@"; do case $arg in --all) REMOVE_ALL=true INTERACTIVE=false ;; --ardupilot) REMOVE_ARDUPILOT=true INTERACTIVE=false ;; --help|-h) echo "Usage: ./setup/uninstall.sh [OPTIONS]" echo "" echo "Options:" echo " --all Remove everything without prompts" echo " --ardupilot Remove ArduPilot SITL and Gazebo plugin only" echo " --help Show this help message" echo "" echo "What gets removed:" echo " - ~/ardupilot (ArduPilot SITL source and build)" echo " - ~/ardupilot_gazebo (ArduPilot Gazebo plugin)" echo " - Project venv/ directory" echo " - Project activate.sh" echo " - ArduPilot configuration from ~/.bashrc" echo "" echo "What is NOT removed:" echo " - ROS 2 (use: sudo apt remove 'ros-*')" echo " - Gazebo (use: sudo apt remove 'gz-*')" echo " - System packages" exit 0 ;; esac done # Helper function to ask yes/no ask_yes_no() { local prompt="$1" local default="${2:-n}" if [ "$default" = "y" ]; then prompt="$prompt [Y/n]: " else prompt="$prompt [y/N]: " fi read -p "$prompt" response response="${response:-$default}" case "$response" in [yY][eE][sS]|[yY]) return 0 ;; *) return 1 ;; esac } # ============================================================================= # REMOVE ARDUPILOT SITL # ============================================================================= remove_ardupilot() { if [ -d "$ARDUPILOT_HOME" ]; then echo "[INFO] Removing ArduPilot SITL: $ARDUPILOT_HOME" rm -rf "$ARDUPILOT_HOME" echo "[OK] ArduPilot SITL removed" else echo "[SKIP] ArduPilot SITL not found" fi } # ============================================================================= # REMOVE ARDUPILOT GAZEBO PLUGIN # ============================================================================= remove_ardupilot_gazebo() { if [ -d "$ARDUPILOT_GZ" ]; then echo "[INFO] Removing ArduPilot Gazebo plugin: $ARDUPILOT_GZ" rm -rf "$ARDUPILOT_GZ" echo "[OK] ArduPilot Gazebo plugin removed" else echo "[SKIP] ArduPilot Gazebo plugin not found" fi } # ============================================================================= # REMOVE PROJECT VENV # ============================================================================= remove_venv() { if [ -d "$VENV_DIR" ]; then echo "[INFO] Removing Python virtual environment: $VENV_DIR" rm -rf "$VENV_DIR" echo "[OK] Python virtual environment removed" else echo "[SKIP] Virtual environment not found" fi } # ============================================================================= # REMOVE ACTIVATE SCRIPT # ============================================================================= remove_activate() { if [ -f "$ACTIVATE_SCRIPT" ]; then echo "[INFO] Removing activation script: $ACTIVATE_SCRIPT" rm -f "$ACTIVATE_SCRIPT" echo "[OK] Activation script removed" else echo "[SKIP] Activation script not found" fi } # ============================================================================= # REMOVE BASHRC CONFIGURATION # ============================================================================= remove_bashrc_config() { BASHRC_MARKER="# === RDC Simulation ArduPilot ===" OLD_MARKER="# === ArduPilot SITL ===" if grep -q "$BASHRC_MARKER" ~/.bashrc 2>/dev/null || grep -q "$OLD_MARKER" ~/.bashrc 2>/dev/null; then echo "[INFO] Removing ArduPilot configuration from ~/.bashrc" # Create backup cp ~/.bashrc ~/.bashrc.backup.$(date +%Y%m%d_%H%M%S) # Remove the configuration block (everything from marker to next blank line or EOF) # This handles both old and new markers sed -i "/$BASHRC_MARKER/,/^$/d" ~/.bashrc 2>/dev/null || true sed -i "/$OLD_MARKER/,/^$/d" ~/.bashrc 2>/dev/null || true echo "[OK] ArduPilot configuration removed from ~/.bashrc" echo "[INFO] Backup saved to ~/.bashrc.backup.*" else echo "[SKIP] No ArduPilot configuration found in ~/.bashrc" fi } # ============================================================================= # REMOVE ARDUPILOT ENV FILE # ============================================================================= remove_ardupilot_env() { if [ -f "$HOME/.ardupilot_env" ]; then echo "[INFO] Removing ArduPilot environment file: ~/.ardupilot_env" rm -f "$HOME/.ardupilot_env" echo "[OK] ArduPilot environment file removed" else echo "[SKIP] ArduPilot environment file not found" fi } # ============================================================================= # MAIN # ============================================================================= if [ "$REMOVE_ALL" = true ]; then echo "Removing all components..." echo "" remove_ardupilot remove_ardupilot_gazebo remove_venv remove_activate remove_bashrc_config remove_ardupilot_env elif [ "$REMOVE_ARDUPILOT" = true ]; then echo "Removing ArduPilot components..." echo "" remove_ardupilot remove_ardupilot_gazebo remove_bashrc_config remove_ardupilot_env else # Interactive mode echo "This will remove components installed by install_ubuntu.sh" echo "" if [ -d "$ARDUPILOT_HOME" ]; then echo "Found: $ARDUPILOT_HOME ($(du -sh "$ARDUPILOT_HOME" 2>/dev/null | cut -f1))" fi if [ -d "$ARDUPILOT_GZ" ]; then echo "Found: $ARDUPILOT_GZ ($(du -sh "$ARDUPILOT_GZ" 2>/dev/null | cut -f1))" fi if [ -d "$VENV_DIR" ]; then echo "Found: $VENV_DIR ($(du -sh "$VENV_DIR" 2>/dev/null | cut -f1))" fi echo "" if ask_yes_no "Remove ArduPilot SITL (~5GB)?"; then remove_ardupilot fi if ask_yes_no "Remove ArduPilot Gazebo plugin?"; then remove_ardupilot_gazebo fi if ask_yes_no "Remove Python virtual environment?"; then remove_venv fi if ask_yes_no "Remove activation script?"; then remove_activate fi if ask_yes_no "Remove ArduPilot configuration from ~/.bashrc?"; then remove_bashrc_config remove_ardupilot_env fi fi echo "" echo "==============================================" echo " Uninstall Complete" echo "==============================================" echo "" echo "Note: The following were NOT removed:" echo " - ROS 2 packages (sudo apt remove 'ros-*')" echo " - Gazebo packages (sudo apt remove 'gz-*')" echo " - System dependencies installed via apt" echo "" echo "To reinstall: ./setup/install_ubuntu.sh" echo ""