@echo off
setlocal EnableDelayedExpansion
title AZ Computer Guru - ClaudeTools Setup
color 0A

echo ============================================
echo   AZ Computer Guru - ClaudeTools Bootstrap
echo ============================================
echo.
echo This sets up the Claude Code workspace on
echo this machine. Takes about 5 minutes.
echo.
echo Press any key to start, or Ctrl+C to cancel.
pause >nul

:: Determine target drive (same drive as this script)
set "DRIVE=%~d0"
set "BASE=%DRIVE%\claudetools"
set "VAULT=%DRIVE%\vault"
set "AGE_DIR=%APPDATA%\sops\age"
set "SCRIPT_DIR=%~dp0"

echo.
echo [1/7] Checking prerequisites...

:: Check git
where git >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo   [!] git not found. Installing via winget...
    winget install --id Git.Git -e --accept-package-agreements --accept-source-agreements
    echo   [!] Please close and reopen this script after git installs.
    pause
    exit /b 1
)
echo   [OK] git found

:: Check claude
where claude >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo   [!] Claude Code not found. Installing...
    winget install --id Anthropic.ClaudeCode -e --accept-package-agreements --accept-source-agreements 2>nul
    if %ERRORLEVEL% neq 0 (
        echo   [!] winget install failed. Try: npm install -g @anthropic-ai/claude-code
        echo       OR download from https://claude.ai/download
        pause
        exit /b 1
    )
)
echo   [OK] Claude Code found

:: Check sops
where sops >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo   [!] SOPS not found. Installing via winget...
    winget install --id Mozilla.sops -e --accept-package-agreements --accept-source-agreements
)
echo   [OK] SOPS found

:: Check python
where python >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo   [!] Python not found. Installing via winget...
    winget install --id Python.Python.3.12 -e --accept-package-agreements --accept-source-agreements
    echo   [!] Please close and reopen this script after Python installs.
    pause
    exit /b 1
)
echo   [OK] Python found

echo.
echo [2/7] Setting up age decryption key...

if not exist "%AGE_DIR%" mkdir "%AGE_DIR%"
if exist "%SCRIPT_DIR%keys.txt" (
    copy /Y "%SCRIPT_DIR%keys.txt" "%AGE_DIR%\keys.txt" >nul
    echo   [OK] Age key installed from bootstrap package
) else if exist "%AGE_DIR%\keys.txt" (
    echo   [OK] Age key already present
) else (
    echo   [!!] Age decryption key not found!
    echo.
    echo   Ask Mike for the keys.txt file and place it at:
    echo     %AGE_DIR%\keys.txt
    echo.
    echo   Without this file, credential vault access won't work.
    echo   Setup will continue but vault commands will fail until
    echo   the key is in place.
    echo.
    pause
)

echo.
echo [3/7] Cloning ClaudeTools repo...

if not exist "%BASE%\.git" (
    git clone https://howard@git.azcomputerguru.com/azcomputerguru/claudetools.git "%BASE%"
    if %ERRORLEVEL% neq 0 (
        echo   [!] Clone failed. Check your Gitea credentials.
        echo   Username: howard
        echo   Password: ask Mike for initial password
        pause
        exit /b 1
    )
    echo   [OK] Cloned to %BASE%
) else (
    echo   [OK] Already exists, pulling latest...
    cd /d "%BASE%" && git pull
)

echo.
echo [4/7] Cloning Vault repo...

if not exist "%VAULT%\.git" (
    git clone https://howard@git.azcomputerguru.com/azcomputerguru/vault.git "%VAULT%"
    if %ERRORLEVEL% neq 0 (
        echo   [!] Vault clone failed. Check credentials.
        pause
        exit /b 1
    )
    echo   [OK] Cloned to %VAULT%
) else (
    echo   [OK] Already exists, pulling latest...
    cd /d "%VAULT%" && git pull
)

echo.
echo [5/7] Configuring git identity...

cd /d "%BASE%"
git config user.name "Howard Enos"
git config user.email "howard@azcomputerguru.com"
cd /d "%VAULT%"
git config user.name "Howard Enos"
git config user.email "howard@azcomputerguru.com"
echo   [OK] Git identity set to Howard Enos

echo.
echo [6/7] Creating desktop shortcut...

set "SHORTCUT=%USERPROFILE%\Desktop\ClaudeTools.bat"
(
echo @echo off
echo title ClaudeTools - AZ Computer Guru
echo cd /d "%BASE%"
echo claude
) > "%SHORTCUT%"
echo   [OK] Created: %SHORTCUT%

echo.
echo [7/7] Verifying setup...

echo   Repo:     %BASE%
echo   Vault:    %VAULT%
echo   Age key:  %AGE_DIR%\keys.txt
if exist "%AGE_DIR%\keys.txt" (
    echo   Vault:    [OK] key present
    cd /d "%BASE%"
    bash "%VAULT%/scripts/vault.sh" list 2>nul | find /c ".sops.yaml" >nul 2>&1 && echo   Decrypt:  [OK] vault accessible || echo   Decrypt:  [!] vault test failed
) else (
    echo   Vault:    [!] key missing - ask Mike
)

echo.
echo ============================================
echo   Setup Complete!
echo ============================================
echo.
echo Next steps:
echo   1. Double-click "ClaudeTools" on your desktop
echo   2. Claude will ask who you are - say "Howard"
echo   3. Claude will walk you through the system
echo.
echo   If you need the vault key, ask Mike.
echo   Your Gitea login: howard / (ask Mike for password)
echo.
pause
