@echo off
setlocal enabledelayedexpansion

:: =====================================================
:: NomadArch - Clean Copy Script for Public Release
:: Creates a sanitized copy without sensitive data
:: =====================================================

title NomadArch Clean Copy for GitHub

echo.
echo =====================================================
echo   NomadArch - Prepare Clean Public Release
echo =====================================================
echo.

set SCRIPT_DIR=%~dp0
set SCRIPT_DIR=%SCRIPT_DIR:~0,-1%
set DEST_DIR=%USERPROFILE%\Desktop\NomadArch-Public-Release

echo [INFO] Source: %SCRIPT_DIR%
echo [INFO] Destination: %DEST_DIR%
echo.

if exist "%DEST_DIR%" (
    echo [WARN] Destination exists. Removing old copy...
    rmdir /s /q "%DEST_DIR%"
)

echo [STEP 1/6] Creating destination directory...
mkdir "%DEST_DIR%"

echo [STEP 2/6] Copying core project files...

:: Copy essential files
copy "%SCRIPT_DIR%\package.json" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\package-lock.json" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\.gitignore" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\README.md" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\BUILD.md" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\AGENTS.md" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\PROGRESS.md" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\manual_test_guide.md" "%DEST_DIR%\" >nul

:: Copy launchers and installers
copy "%SCRIPT_DIR%\Install-*.bat" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\Install-*.sh" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\Launch-*.bat" "%DEST_DIR%\" >nul
copy "%SCRIPT_DIR%\Launch-*.sh" "%DEST_DIR%\" >nul

echo [STEP 3/6] Copying packages directory (source only)...

:: Use robocopy to exclude unwanted items
robocopy "%SCRIPT_DIR%\packages" "%DEST_DIR%\packages" /E /NFL /NDL /NJH /NJS /NC /NS ^
    /XD node_modules dist out release .vite .electron-vite _backup_original __pycache__ ^
    /XF *.log *.bak *.tmp *.map

echo [STEP 4/6] Copying additional directories...

:: Copy docs if exists
if exist "%SCRIPT_DIR%\docs" (
    robocopy "%SCRIPT_DIR%\docs" "%DEST_DIR%\docs" /E /NFL /NDL /NJH /NJS /NC /NS /XD node_modules
)

:: Copy images if exists
if exist "%SCRIPT_DIR%\images" (
    robocopy "%SCRIPT_DIR%\images" "%DEST_DIR%\images" /E /NFL /NDL /NJH /NJS /NC /NS
)

:: Copy dev-docs if exists
if exist "%SCRIPT_DIR%\dev-docs" (
    robocopy "%SCRIPT_DIR%\dev-docs" "%DEST_DIR%\dev-docs" /E /NFL /NDL /NJH /NJS /NC /NS
)

:: Copy scripts directory if exists
if exist "%SCRIPT_DIR%\scripts" (
    robocopy "%SCRIPT_DIR%\scripts" "%DEST_DIR%\scripts" /E /NFL /NDL /NJH /NJS /NC /NS
)

:: Copy .github directory (workflows, templates)
if exist "%SCRIPT_DIR%\.github" (
    robocopy "%SCRIPT_DIR%\.github" "%DEST_DIR%\.github" /E /NFL /NDL /NJH /NJS /NC /NS
)

echo [STEP 5/6] Creating clean MCP config template...

:: Create a template .mcp.json with placeholders
(
echo {
echo     "mcpServers": {
echo         "sequential-thinking": {
echo             "command": "npx",
echo             "args": ["-y", "@modelcontextprotocol/server-sequential-thinking"]
echo         },
echo         "desktop-commander": {
echo             "command": "npx",
echo             "args": ["-y", "@modelcontextprotocol/server-desktop-commander"]
echo         },
echo         "web-reader": {
echo             "command": "npx",
echo             "args": ["-y", "@modelcontextprotocol/server-web-reader"]
echo         },
echo         "github": {
echo             "command": "npx",
echo             "args": ["-y", "@modelcontextprotocol/server-github"],
echo             "env": {
echo                 "GITHUB_TOKEN": "YOUR_GITHUB_TOKEN_HERE"
echo             }
echo         }
echo     }
echo }
) > "%DEST_DIR%\.mcp.json.example"

echo [STEP 6/6] Final cleanup...

:: Remove any accidentally copied sensitive files
if exist "%DEST_DIR%\.opencode" rmdir /s /q "%DEST_DIR%\.opencode"
if exist "%DEST_DIR%\.trae" rmdir /s /q "%DEST_DIR%\.trae"
if exist "%DEST_DIR%\.backup" rmdir /s /q "%DEST_DIR%\.backup"
if exist "%DEST_DIR%\.tmp-qwen-code" rmdir /s /q "%DEST_DIR%\.tmp-qwen-code"
if exist "%DEST_DIR%\.agent" rmdir /s /q "%DEST_DIR%\.agent"
if exist "%DEST_DIR%\install.log" del "%DEST_DIR%\install.log"

:: Delete any .bak files that got through
for /r "%DEST_DIR%" %%f in (*.bak) do del "%%f" 2>nul
for /r "%DEST_DIR%" %%f in (*.log) do del "%%f" 2>nul
for /r "%DEST_DIR%" %%f in (*.tmp) do del "%%f" 2>nul

:: Remove _backup_original directories
for /d /r "%DEST_DIR%" %%d in (_backup_original) do (
    if exist "%%d" rmdir /s /q "%%d"
)

echo.
echo =====================================================
echo   Clean Copy Complete!
echo =====================================================
echo.
echo   Location: %DEST_DIR%
echo.
echo   Next Steps:
echo   1. Review the contents of %DEST_DIR%
echo   2. cd %DEST_DIR%
echo   3. git init
echo   4. git add .
echo   5. git commit -m "Initial public release"
echo   6. git remote add origin https://github.com/YOUR_USER/NomadArch.git
echo   7. git push -u origin main
echo.
echo =====================================================

pause
