@echo off
chcp 65001 >nul 2>&1
echo Windows Installation Script
echo =========================
echo.

REM Check administrator privileges
net session >nul 2>&1
if %errorLevel% == 0 (
    set "INSTALL_DIR=C:\Program Files\utxo_compiler"
    echo Administrator privileges detected, installing to system directory
) else (
    set "INSTALL_DIR=%USERPROFILE%\utxo_compiler"
    echo Installing to user directory
)

REM Create installation directory
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"

REM Copy executable file
copy "utxo_compiler.exe" "%INSTALL_DIR%\"
if %errorLevel% == 0 (
    echo Executable file copied successfully
) else (
    echo Error: Failed to copy executable file
    pause
    exit /b 1
)

REM Copy dependency DLL files
echo Copying dependency libraries...
copy *.dll "%INSTALL_DIR%\" >nul 2>&1
if %errorLevel% == 0 (
    echo Dependency libraries copied successfully
) else (
    echo Warning: No dependency libraries found, program may not run properly
)

REM Copy standard library (<install>\stdlib\ hit by resolver candidate exeDir\stdlib)
if exist "stdlib" (
    echo Copying standard library...
    if exist "%INSTALL_DIR%\stdlib" rmdir /s /q "%INSTALL_DIR%\stdlib"
    xcopy /E /I /Q "stdlib" "%INSTALL_DIR%\stdlib" >nul
    if %errorLevel% == 0 (
        echo Standard library copied successfully
    ) else (
        echo Warning: Failed to copy standard library
    )
) else (
    echo Warning: stdlib directory not found in package
)

echo.
echo Installation completed!
echo Installation location: %INSTALL_DIR%
echo Executable file: %INSTALL_DIR%\utxo_compiler.exe
echo.
echo Usage:
echo   "%INSTALL_DIR%\utxo_compiler.exe" --version
echo   "%INSTALL_DIR%\utxo_compiler.exe" --help
echo.
echo Note: If the program fails to run, please check DEPENDENCIES.txt file
echo.

REM Add to PATH (optional)
echo.
echo Do you want to add the installation directory to PATH? (Y/N)
set /p ADD_PATH="This will allow you to run 'utxo_compiler.exe' from anywhere: "
if /i "%ADD_PATH%"=="Y" (
    echo.
    echo Adding to PATH...
    
    REM Get current user PATH (safer than system PATH)
    for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "USER_PATH=%%B"
    
    REM Check if already in PATH
    echo %USER_PATH% | findstr /i "%INSTALL_DIR%" >nul
    if %errorLevel% == 0 (
        echo Directory already in PATH, skipping...
    ) else (
        REM Add to user PATH (safer than system PATH)
        if defined USER_PATH (
            setx PATH "%USER_PATH%;%INSTALL_DIR%" >nul 2>&1
        ) else (
            setx PATH "%INSTALL_DIR%" >nul 2>&1
        )
        
        if %errorLevel% == 0 (
            echo.
            echo ✓ PATH updated successfully!
            echo.
            echo IMPORTANT: You must restart your command prompt for changes to take effect.
            echo After restarting, you can use 'utxo_compiler.exe' from anywhere.
            echo.
            echo Alternative: Run this command in a new command prompt to test:
            echo   "%INSTALL_DIR%\utxo_compiler.exe" --version
        ) else (
            echo.
            echo ✗ Failed to update PATH automatically.
            echo.
            echo Manual solution:
            echo 1. Press Win+R, type 'sysdm.cpl', press Enter
            echo 2. Click 'Environment Variables'
            echo 3. Under 'User variables', find 'Path' and click 'Edit'
            echo 4. Click 'New' and add: %INSTALL_DIR%
            echo 5. Click OK to save
        )
    )
) else (
    echo.
    echo PATH not modified. You can run the program using:
    echo   "%INSTALL_DIR%\utxo_compiler.exe" --version
)

echo.
pause
