dotfiles/uninstall.sh

188 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# Dotfiles Uninstall Script
# Author: q280969
# Description: Remove dotfiles symlinks created by GNU Stow
set -e # Exit on any error
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AVAILABLE_PACKAGES=(bash zsh git ssh starship netrc)
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
show_help() {
cat << EOF
Dotfiles Uninstall Script
USAGE:
$0 [OPTIONS]
OPTIONS:
-h, --help Show this help message
-d, --dry-run Show what would be removed without making changes
-v, --verbose Enable verbose output
-q, --quiet Suppress non-essential output
--only=PACKAGES Remove only specified packages (comma-separated)
Available: ${AVAILABLE_PACKAGES[*]}
EXAMPLES:
$0 # Remove all configurations
$0 --only=git,zsh # Remove only git and zsh configurations
$0 --dry-run # Preview what would be removed
AVAILABLE PACKAGES:
EOF
for package in "${AVAILABLE_PACKAGES[@]}"; do
echo " - $package"
done
}
check_dependencies() {
log_info "Checking dependencies..."
if ! command -v stow &> /dev/null; then
log_error "GNU Stow is not installed"
log_error "Stow is required to safely remove symlinks"
exit 1
fi
log_success "All dependencies satisfied"
}
uninstall_package() {
local package=$1
if [[ ! -d "$SCRIPT_DIR/$package" ]]; then
log_error "Package '$package' not found in $SCRIPT_DIR"
return 1
fi
log_info "Removing $package configuration..."
if [[ "$DRY_RUN" == "true" ]]; then
log_info "[DRY RUN] Would run: stow -D $package"
log_info "[DRY RUN] Would remove symlinks for files in $SCRIPT_DIR/$package"
return 0
fi
if stow -D "$package"; then
log_success "Successfully removed $package"
else
log_error "Failed to remove $package"
return 1
fi
}
# Parse command line arguments
DRY_RUN=false
VERBOSE=false
QUIET=false
SELECTED_PACKAGES=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-d|--dry-run)
DRY_RUN=true
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
-q|--quiet)
QUIET=true
shift
;;
--only=*)
IFS=',' read -ra SELECTED_PACKAGES <<< "${1#*=}"
shift
;;
*)
log_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
# Set packages to remove
if [[ ${#SELECTED_PACKAGES[@]} -eq 0 ]]; then
PACKAGES_TO_REMOVE=("${AVAILABLE_PACKAGES[@]}")
else
PACKAGES_TO_REMOVE=("${SELECTED_PACKAGES[@]}")
# Validate selected packages
for package in "${PACKAGES_TO_REMOVE[@]}"; do
if [[ ! " ${AVAILABLE_PACKAGES[*]} " =~ " ${package} " ]]; then
log_error "Unknown package: $package"
log_info "Available packages: ${AVAILABLE_PACKAGES[*]}"
exit 1
fi
done
fi
# Suppress output if quiet mode
if [[ "$QUIET" == "true" ]]; then
exec > /dev/null 2>&1
fi
# Main execution
main() {
log_info "Starting dotfiles removal..."
if [[ "$DRY_RUN" == "true" ]]; then
log_info "DRY RUN MODE - No changes will be made"
fi
cd "$SCRIPT_DIR"
check_dependencies
local failed_packages=()
for package in "${PACKAGES_TO_REMOVE[@]}"; do
if ! uninstall_package "$package"; then
failed_packages+=("$package")
fi
done
echo
if [[ ${#failed_packages[@]} -eq 0 ]]; then
log_success "All packages removed successfully!"
log_info "Your original files (if any) should now be restored"
else
log_error "Some packages failed to remove: ${failed_packages[*]}"
exit 1
fi
}
# Run main function
main