#!/bin/bash

# Script to safely remove Google Chrome and all its data
# This will remove the Chrome application, cache, and user data

# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Print with color
print_info() {
    echo -e "${BLUE}INFO: $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}WARNING: $1${NC}"
}

print_error() {
    echo -e "${RED}ERROR: $1${NC}"
}

print_success() {
    echo -e "${GREEN}SUCCESS: $1${NC}"
}

# Check if Chrome is running
if pgrep "Google Chrome" > /dev/null; then
    print_error "Google Chrome is currently running."
    print_info "Please close Chrome completely before running this script."
    exit 1
fi

# Paths to Chrome resources
CHROME_APP="/Applications/Google Chrome.app"
CHROME_CACHE="$HOME/Library/Caches/Google/Chrome"
CHROME_USER_DATA="$HOME/Library/Application Support/Google/Chrome"

# Check if Chrome is installed
if [ ! -d "$CHROME_APP" ]; then
    print_error "Google Chrome is not installed at $CHROME_APP."
    exit 1
fi

# Calculate space before deletion
print_info "Calculating space used by Chrome..."
CHROME_APP_SIZE=$(du -sk "$CHROME_APP" | cut -f1)
CHROME_CACHE_SIZE=0
CHROME_USER_DATA_SIZE=0

if [ -d "$CHROME_CACHE" ]; then
    CHROME_CACHE_SIZE=$(du -sk "$CHROME_CACHE" | cut -f1)
fi

if [ -d "$CHROME_USER_DATA" ]; then
    CHROME_USER_DATA_SIZE=$(du -sk "$CHROME_USER_DATA" | cut -f1)
fi

TOTAL_SIZE_KB=$((CHROME_APP_SIZE + CHROME_CACHE_SIZE + CHROME_USER_DATA_SIZE))
TOTAL_SIZE_MB=$((TOTAL_SIZE_KB / 1024))
TOTAL_SIZE_GB=$(echo "scale=2; $TOTAL_SIZE_KB/1048576" | bc)

print_info "Chrome is currently using approximately $TOTAL_SIZE_GB GB of space:"
echo "  - Chrome Application: $((CHROME_APP_SIZE / 1024)) MB"
echo "  - Chrome Cache: $((CHROME_CACHE_SIZE / 1024)) MB"
echo "  - Chrome User Data: $((CHROME_USER_DATA_SIZE / 1024)) MB"

# Warning about data loss
print_warning "This will permanently delete Chrome and all its data, including:"
echo "  - Browsing history"
echo "  - Saved passwords (unless synced with your Google account)"
echo "  - Bookmarks (unless synced with your Google account)"
echo "  - Extensions and their data"
echo "  - Any other user preferences and data"

# Ask for confirmation
echo
echo "Are you sure you want to proceed with deletion? (yes/no)"
read -r CONFIRM

if [[ ! "$CONFIRM" =~ ^[Yy][Ee][Ss]$ ]]; then
    print_info "Operation cancelled. No changes were made."
    exit 0
fi

# Begin deletion
print_info "Beginning Chrome cleanup..."

# Remove Chrome application
print_info "Removing Chrome application..."
print_info "This requires administrator privileges. You'll be prompted for your password."
if sudo rm -rf "$CHROME_APP"; then
    print_success "Chrome application removed successfully."
else
    print_error "Failed to remove Chrome application."
fi

# Remove Chrome cache
if [ -d "$CHROME_CACHE" ]; then
    print_info "Removing Chrome cache..."
    if rm -rf "$CHROME_CACHE"; then
        print_success "Chrome cache removed successfully."
    else
        print_error "Failed to remove Chrome cache."
    fi
else
    print_info "Chrome cache directory not found. Skipping."
fi

# Remove Chrome user data
if [ -d "$CHROME_USER_DATA" ]; then
    print_info "Removing Chrome user data..."
    if rm -rf "$CHROME_USER_DATA"; then
        print_success "Chrome user data removed successfully."
    else
        print_error "Failed to remove Chrome user data."
    fi
else
    print_info "Chrome user data directory not found. Skipping."
fi

# Final cleanup
print_info "Cleaning up any remaining Chrome preferences..."
rm -rf "$HOME/Library/Preferences/com.google.Chrome"*
rm -rf "$HOME/Library/Saved Application State/com.google.Chrome.savedState"

# Success message
print_success "Chrome cleanup completed!"
print_success "Reclaimed approximately $TOTAL_SIZE_GB GB of disk space."
print_info "If you want to reinstall Chrome, you can download it from https://www.google.com/chrome/"

exit 0

