#!/bin/bash
# NoWiFiCamera — SD Card Downloader + Google Drive Export
# Recordings go to YOUR OWN Google Drive

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$SCRIPT_DIR/download_sd.py"
CREDS="$SCRIPT_DIR/credentials.json"
SERVER_BASE="https://nowificamera.com/js"

echo ""
echo " ====================================================="
echo "   NoWiFiCamera SD Card Downloader"
echo "   Download + Export to YOUR Google Drive"
echo " ====================================================="
echo ""

# ── 1. Check Python ────────────────────────────────────────────────────────────
if command -v python3 &>/dev/null; then
    PYTHON="python3"
elif command -v python &>/dev/null; then
    PYTHON="python"
else
    echo " [!] Python not found. Installing..."
    if [[ "$OSTYPE" == "darwin"* ]]; then
        if command -v brew &>/dev/null; then
            brew install python3
        else
            echo " Install Python from: https://www.python.org/downloads/"
            open "https://www.python.org/downloads/" 2>/dev/null || true
            exit 1
        fi
    else
        sudo apt-get update -qq && sudo apt-get install -y python3 python3-pip
    fi
    PYTHON="python3"
fi

echo " [OK] Found $($PYTHON --version 2>&1)"
echo ""

# ── 2. Auto-download download_sd.py if missing ────────────────────────────────
if [ ! -f "$SCRIPT" ]; then
    echo " [*] Downloading download_sd.py from server ..."
    curl -fsSL "$SERVER_BASE/download_sd.py" -o "$SCRIPT"
    echo " [OK] download_sd.py downloaded."
    echo ""
fi

# ── 3. Auto-fetch credentials.json from server ────────────────────────────────
#      Identifies the app only — NOT your Google account.
#      You sign in with YOUR OWN Google account in the next step.
if [ ! -f "$CREDS" ]; then
    echo " [*] Fetching app credentials from server ..."
    curl -fsSL "$SERVER_BASE/credentials.json" -o "$CREDS"
    if [ $? -ne 0 ]; then
        echo " [X] Could not download credentials.json. Check internet connection."
        exit 1
    fi
    echo " [OK] Credentials fetched."
    echo ""
fi

# ── 4. Hotspot check ───────────────────────────────────────────────────────────
echo " ====================================================="
echo "  Connect this computer to your PHONE HOTSPOT"
echo "  (not your home Wi-Fi router) before continuing."
echo " ====================================================="
echo ""
read -p "  Connected to phone hotspot? (y/n): " CONFIRM
echo ""
[[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]] && echo "  Connect first, then re-run." && exit 0

# ── 5. Run — sign in with YOUR Google account ─────────────────────────────────
echo " Browser will open — sign in with YOUR Google account."
echo " Recordings will go to YOUR Google Drive."
echo ""
$PYTHON "$SCRIPT" --gdrive

echo ""
echo " ====================================================="
echo "  Done! Check YOUR Google Drive for:"
echo "  'NoWiFiCamera Recordings'"
echo " ====================================================="
echo ""
