#!/usr/bin/env bash

set -euo pipefail

# Ensure $HOME exists
if [ -z "$HOME" ]; then
	echo "❌ HOME variable not set!"
	exit 1
fi

# Check if Proton VPN CLI is installed.
if ! command -v protonvpn >/dev/null 2>&1; then
	echo "Proton VPN CLI is not installed." >&2
	exit 1
fi

# Definitions
USER_NAME=NikoboiNFTB
REPO_NAME=Proton-VPN
REPO_DIR="$HOME/GitHub/$USER_NAME/$REPO_NAME"
REPO_URL="https://github.com/$USER_NAME/$REPO_NAME"
DESKTOP_DIR="$HOME/Desktop"

# Clone repo, update if already present.
if [ -d "$REPO_DIR/.git" ]; then
	echo "ℹ️  Updating repository..."
	if git -C "$REPO_DIR" pull --ff-only >/dev/null 2>&1; then
		echo "✅ Successfully updated repository!"
	else
		echo "❌ Failed to update repository."
		exit 1
	fi
elif [ -d "$REPO_DIR" ]; then
	echo "❌ Repository folder exists, but is not a repository."
	echo "   Why do you have that??"
	echo "   Please remove or move:"
	echo "     $REPO_DIR"
	exit 1
else
	echo "ℹ️  Cloning repository..."
	mkdir -p "$(dirname "$REPO_DIR")"
	if git clone "$REPO_URL" "$REPO_DIR"; then
		echo "✅ Successfully cloned repository!"
	else
		echo "❌ Clone failed."
		exit 1
	fi
fi

cd "$REPO_DIR"

# Copy .desktop file
echo "ℹ️  Preparing desktop file..."

REPO_FILE="$REPO_DIR/vpn-connect.desktop"
DESKTOP_FILE="$DESKTOP_DIR/vpn-connect.desktop"

if [ ! -f "$REPO_FILE" ]; then
	echo "❌ Source desktop file does not exist:"
	echo "   $REPO_FILE"
	echo "   Recommended: Delete the repository and run the install script again:"
	echo "     rm -rf '$REPO_DIR'"
	echo "     bash <(wget -qO- https://proton.nikoboi.dev/setup)"
	exit 1
fi

if
	sed -i "s|Icon=\$HOME|Icon=$HOME|g" "$REPO_FILE" &&
		sed -i "s|Exec=\$HOME|Exec=$HOME|g" "$REPO_FILE" &&
		mkdir -p "$DESKTOP_DIR" &&
		cp "$REPO_FILE" "$DESKTOP_FILE"
then
	echo "✅ Successfully copied desktop file"
	# Restore original placeholders in repo
	sed -i "s|Icon=$HOME|Icon=\$HOME|g" "$REPO_FILE"
	sed -i "s|Exec=$HOME|Exec=\$HOME|g" "$REPO_FILE"
else
	echo "❌ Failed during desktop file preparation or copy"
	# Attempt to restore repo file even if something failed
	sed -i "s|Icon=$HOME|Icon=\$HOME|g" "$REPO_FILE" 2>/dev/null
	sed -i "s|Exec=$HOME|Exec=\$HOME|g" "$REPO_FILE" 2>/dev/null
	exit 1
fi
