#!/bin/bash

# Corixa — Chrome Extension Setup
# Double-click this file to set up the Chrome extension.

set -e
clear

EXTENSION_ID="igipccpnifeonlneicchmoepockjiifh"
HOST_NAME="io.halldor.corixa.chrome"
INSTALL_DIR="$HOME/.corixa"
NATIVE_HOST_DIR="$HOME/Library/Application Support/Google/Chrome/NativeMessagingHosts"
DOWNLOAD_URL="https://corixa.io/chrome/ClearDayNativeHost"

echo ""
echo "  ================================"
echo "   Corixa — Chrome Extension Setup"
echo "  ================================"
echo ""

# 1. Check Corixa is installed
if [ ! -d "/Applications/Corixa.app" ]; then
    echo "  Corixa.app not found in /Applications."
    echo "  Please install it from the Mac App Store first."
    echo ""
    echo "  Press any key to close."
    read -n 1 -s
    exit 1
fi
echo "  [1/3] Corixa.app found"

# 2. Download and install native host
mkdir -p "$INSTALL_DIR"

echo "  [2/3] Downloading Chrome helper..."
curl -fsSL "$DOWNLOAD_URL" -o "$INSTALL_DIR/ClearDayNativeHost"
chmod +x "$INSTALL_DIR/ClearDayNativeHost"

# 3. Create Chrome native messaging manifest
mkdir -p "$NATIVE_HOST_DIR"

cat > "$NATIVE_HOST_DIR/$HOST_NAME.json" << EOF
{
    "name": "$HOST_NAME",
    "description": "Corixa Chrome Extension Native Messaging Host",
    "path": "$INSTALL_DIR/ClearDayNativeHost",
    "type": "stdio",
    "allowed_origins": [
        "chrome-extension://$EXTENSION_ID/"
    ]
}
EOF

# Clean up old manifests
rm -f "$NATIVE_HOST_DIR/com.clearday.chrome.json" 2>/dev/null
rm -f "$NATIVE_HOST_DIR/com.halldor.clearday.json" 2>/dev/null
echo "  [3/3] Chrome extension configured"

echo ""
echo "  Setup complete!"
echo ""
echo "  You can close this window."
echo ""
read -n 1 -s -p "  Press any key to close."
echo ""
