@echo off
title MyAI Setup
setlocal EnableDelayedExpansion

echo.
echo  ============================================
echo   MyAI — the last cloud download you need
echo  ============================================
echo.
echo  This installs a real AI on YOUR computer.
echo  No API. No subscription. No cloud. Yours.
echo.
echo  Download size: about 2 GB total, one time.
echo  Works on an ordinary 8 GB computer. No GPU needed.
echo.
pause

set "OLLAMA_EXE=ollama"
where ollama >nul 2>&1
if errorlevel 1 (
    echo.
    echo  [1/4] Downloading the engine ^(Ollama, ~700 MB^)...
    powershell -NoProfile -Command "Invoke-WebRequest -Uri 'https://ollama.com/download/OllamaSetup.exe' -OutFile '%TEMP%\OllamaSetup.exe'"
    if not exist "%TEMP%\OllamaSetup.exe" (
        echo  Download failed. Check your internet connection and run this file again.
        pause
        exit /b 1
    )
    echo  [1/4] Installing the engine...
    "%TEMP%\OllamaSetup.exe" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART
    set "OLLAMA_EXE=%LOCALAPPDATA%\Programs\Ollama\ollama.exe"
) else (
    echo  [1/4] Engine already installed. Good.
)

echo  [2/4] Waking the engine...
start "" /min "%LOCALAPPDATA%\Programs\Ollama\ollama app.exe" 2>nul
set /a tries=0
:waitloop
"!OLLAMA_EXE!" list >nul 2>&1
if not errorlevel 1 goto ready
set /a tries+=1
if !tries! geq 30 (
    echo  The engine did not start. Open the Ollama app from your Start menu, then run this file again.
    pause
    exit /b 1
)
timeout /t 2 /nobreak >nul
goto waitloop

:ready
echo  [3/4] Downloading your AI ^(Llama 3.2, 1.3 GB^) — one time only...
"!OLLAMA_EXE!" pull llama3.2:1b
if errorlevel 1 (
    echo  Model download failed. Run this file again — it resumes where it left off.
    pause
    exit /b 1
)

echo  [4/4] Setting up your Table link — talk to your AI from any phone...
set "MYAI_DIR=%LOCALAPPDATA%\MyAI"
if not exist "%MYAI_DIR%" mkdir "%MYAI_DIR%"
powershell -NoProfile -Command "Invoke-WebRequest -Uri 'https://myai.developumaiengine.com/download/bridge-windows' -OutFile '%MYAI_DIR%\MyAI-Bridge.ps1'"
if not exist "%MYAI_DIR%\MyAI-Bridge.ps1" (
    echo  Could not fetch the Table bridge. Your AI still works on this computer.
    echo  Run this file again later to add the phone link.
    goto finish
)

rem Reset link tool — burns a leaked URL, issues a fresh one
(
echo @echo off
echo powershell -NoProfile -ExecutionPolicy Bypass -File "%MYAI_DIR%\MyAI-Bridge.ps1" -NewTable
echo pause
) > "%MYAI_DIR%\Reset My Table Link.bat"
copy /y "%MYAI_DIR%\Reset My Table Link.bat" "%USERPROFILE%\Desktop\" >nul 2>&1

rem Start the bridge at every logon (no admin needed)
(
echo @echo off
echo start "" /min powershell -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File "%MYAI_DIR%\MyAI-Bridge.ps1"
) > "%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\MyAI-Bridge.bat"

rem Pair now and show the link, then start the bridge in the background
powershell -NoProfile -ExecutionPolicy Bypass -File "%MYAI_DIR%\MyAI-Bridge.ps1" -ShowLink
start "" /min powershell -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File "%MYAI_DIR%\MyAI-Bridge.ps1"

:finish
echo.
echo  ============================================
echo   Done. Your AI lives on this computer now.
echo  ============================================
echo.
echo  Talk to it here: open the Ollama app from your
echo  Start menu, or type "ollama run llama3.2:1b".
echo.
echo  From your phone: use the table link printed above.
echo  Leaked link? Double-click "Reset My Table Link" on your Desktop.
echo.
echo  Unplug the internet if you want. Your AI still works.
echo.
"!OLLAMA_EXE!" run llama3.2:1b "In two sentences, welcome your new operator to fully local AI. You run on their machine, no cloud, no API."
echo.
pause
