#!/bin/sh
# MyAI — the last cloud download you need.
# Installs a real AI on YOUR computer. No API. No subscription. Yours.
set -e

echo ""
echo " ============================================"
echo "  MyAI — the last cloud download you need"
echo " ============================================"
echo ""

if ! command -v ollama >/dev/null 2>&1; then
    OS="$(uname -s)"
    if [ "$OS" = "Linux" ]; then
        echo " [1/3] Installing the engine (Ollama)..."
        curl -fsSL https://ollama.com/install.sh | sh
    elif [ "$OS" = "Darwin" ]; then
        if command -v brew >/dev/null 2>&1; then
            echo " [1/3] Installing the engine (Ollama) via Homebrew..."
            brew install ollama
        else
            echo " Download the engine from https://ollama.com/download/mac"
            echo " Install it, then run this script again."
            exit 1
        fi
    else
        echo " Unsupported system: $OS"
        exit 1
    fi
else
    echo " [1/3] Engine already installed. Good."
fi

echo " [2/3] Waking the engine..."
if ! ollama list >/dev/null 2>&1; then
    (ollama serve >/dev/null 2>&1 &)
    i=0
    until ollama list >/dev/null 2>&1; do
        i=$((i+1))
        [ $i -ge 30 ] && echo " Engine did not start. Run 'ollama serve' then re-run this script." && exit 1
        sleep 2
    done
fi

echo " [3/3] Downloading your AI (Llama 3.2, 1.3 GB) — one time only..."
ollama pull llama3.2:1b

echo ""
echo " ============================================"
echo "  Done. Your AI lives on this computer now."
echo " ============================================"
echo ""
echo " Talk to it any time:  ollama run llama3.2:1b"
echo " Unplug the internet if you want. It still works."
echo ""
ollama run llama3.2:1b "In two sentences, welcome your new operator to fully local AI. You run on their machine, no cloud, no API."
