#!/bin/bash
# =============================================================================
# Cha-Ching Automate! — Installer for Linux / ChromeOS
# Run this after extracting the downloaded .tar.gz:
#   tar -xzf ChaChingAutomate-Linux.tar.gz
#   cd ChaChingAutomate
#   bash install_linux.sh
# =============================================================================

set -e

INSTALL_DIR="$HOME/.local/share/ChaChingAutomate"
BIN_LINK="$HOME/.local/bin/chaching"
DESKTOP_DIR="$HOME/.local/share/applications"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo ""
echo "=============================================="
echo "  Cha-Ching Automate! Installer"
echo "=============================================="
echo ""

# ── 1. System dependencies ────────────────────────────────────────────────────
echo "[1/5] Checking system dependencies..."
sudo apt-get update -qq
sudo apt-get install -y \
    python3 \
    libglib2.0-0 \
    libnss3 \
    libnspr4 \
    libdbus-1-3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libasound2 \
    libpango-1.0-0 \
    libcairo2 \
    python3-tk 2>/dev/null || true

# ── 2. Install Playwright browser (first-time only) ──────────────────────────
echo ""
echo "[2/5] Setting up browser (this may take a minute the first time)..."
if command -v playwright &>/dev/null; then
    playwright install chromium 2>/dev/null || true
    playwright install-deps chromium 2>/dev/null || true
elif python3 -c "from playwright.sync_api import sync_playwright" &>/dev/null 2>&1; then
    python3 -m playwright install chromium 2>/dev/null || true
    python3 -m playwright install-deps chromium 2>/dev/null || true
fi

# ── 3. Copy app files ─────────────────────────────────────────────────────────
echo ""
echo "[3/5] Installing Cha-Ching Automate!..."
mkdir -p "$INSTALL_DIR"
# Copy everything except the installer itself
rsync -a --exclude="install_linux.sh" "$SCRIPT_DIR/" "$INSTALL_DIR/" 2>/dev/null || \
    cp -r "$SCRIPT_DIR/." "$INSTALL_DIR/"

chmod +x "$INSTALL_DIR/ChaChingAutomate"
mkdir -p "$INSTALL_DIR/data"

# ── 4. Create launcher shortcut ───────────────────────────────────────────────
echo ""
echo "[4/5] Creating launcher shortcut..."
mkdir -p "$DESKTOP_DIR"
mkdir -p "$HOME/.local/bin"

# Create a wrapper launcher script that ensures DISPLAY is set
LAUNCHER="$INSTALL_DIR/launch.sh"
printf '#!/bin/bash\nif [ -z "$DISPLAY" ]; then export DISPLAY=":0"; fi\nif [ -z "$XDG_RUNTIME_DIR" ]; then export XDG_RUNTIME_DIR="/run/user/$(id -u)"; fi\nexec "INSTALL_DIR_PLACEHOLDER/ChaChingAutomate" "$@"\n' > "$LAUNCHER"
sed -i "s|INSTALL_DIR_PLACEHOLDER|$INSTALL_DIR|g" "$LAUNCHER"
chmod +x "$LAUNCHER"

# Write the .desktop file pointing to the launcher wrapper
cat > "$DESKTOP_DIR/ChaChingAutomate.desktop" << EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=Cha-Ching Automate!
Comment=Sync Amazon Influencer videos to YouTube automatically
Exec=$LAUNCHER
Icon=$INSTALL_DIR/logo.png
Terminal=false
Categories=Utility;Network;
StartupNotify=true
StartupWMClass=ChaChingAutomate
EOF

chmod +x "$DESKTOP_DIR/ChaChingAutomate.desktop"

# Also create a command-line shortcut
cat > "$BIN_LINK" << EOF
#!/bin/bash
exec "$LAUNCHER" "\$@"
EOF
chmod +x "$BIN_LINK"

# Update desktop database so ChromeOS app launcher picks it up
update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true

# ── 5. Done ───────────────────────────────────────────────────────────────────
echo ""
echo "[5/5] Done!"
echo ""
echo "=============================================="
echo "  Cha-Ching Automate! is installed!"
echo "=============================================="
echo ""
echo "You can launch it in three ways:"
echo "  1. Search for 'Cha-Ching' in the ChromeOS app launcher"
echo "  2. Type 'chaching' in the Linux terminal"
echo "  3. Run directly: $INSTALL_DIR/ChaChingAutomate"
echo ""
echo "Your settings and data are stored in:"
echo "  $INSTALL_DIR/data/"
echo ""
echo "To uninstall, run:"
echo "  rm -rf \"$INSTALL_DIR\""
echo "  rm -f \"$BIN_LINK\" \"$DESKTOP_DIR/ChaChingAutomate.desktop\""
echo ""
