#!/bin/bash

# Zync Uninstall Script (AppImage install only)
# Removes files placed by the one-line install script.
# Usage: curl -fsSL https://zync.thesudoer.in/uninstall.sh | sh

set -e

APP_NAME="Zync"
BIN_DIR="$HOME/.local/bin"
APP_DIR="$HOME/.local/share/zync"
DESKTOP_DIR="$HOME/.local/share/applications"
ICON_PATH="$HOME/.local/share/icons/hicolor/512x512/apps/zync.png"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${BLUE}⚡ Uninstalling ${APP_NAME}...${NC}"

REMOVED=0

# 1. Remove symlink
if [ -L "$BIN_DIR/zync" ]; then
    rm -f "$BIN_DIR/zync"
    echo "   Removed symlink: $BIN_DIR/zync"
    REMOVED=1
fi

# 2. Remove AppImage directory
if [ -d "$APP_DIR" ]; then
    rm -rf "$APP_DIR"
    echo "   Removed app directory: $APP_DIR"
    REMOVED=1
fi

# 3. Remove desktop entry
if [ -f "$DESKTOP_DIR/zync.desktop" ]; then
    rm -f "$DESKTOP_DIR/zync.desktop"
    echo "   Removed desktop entry: $DESKTOP_DIR/zync.desktop"
    REMOVED=1
fi
# Also remove legacy capitalised entry if present
if [ -f "$DESKTOP_DIR/Zync.desktop" ]; then
    rm -f "$DESKTOP_DIR/Zync.desktop"
    REMOVED=1
fi

# 4. Remove icon
if [ -f "$ICON_PATH" ]; then
    rm -f "$ICON_PATH"
    echo "   Removed icon: $ICON_PATH"
    REMOVED=1
fi

# 5. Refresh desktop & icon databases
if command -v update-desktop-database >/dev/null 2>&1; then
    update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true
fi
if command -v gtk-update-icon-cache >/dev/null 2>&1; then
    gtk-update-icon-cache -f -t "$HOME/.local/share/icons/hicolor" 2>/dev/null || true
fi

if [ "$REMOVED" -eq 1 ]; then
    echo -e "${GREEN}✔ ${APP_NAME} has been uninstalled.${NC}"
else
    echo -e "${RED}✘ No ${APP_NAME} installation found (script-based install only).${NC}"
    echo "   If you installed via .deb / .rpm / APT, use your package manager to remove it."
fi
