@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
    echo Solicitando permissões administrativas...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd.exe", "/c %~s0", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:gotAdmin
CD /D "%~dp0"
SET "Servicio=postgresql94"

"%~dp0bin\pg_ctl.exe" register -N "%Servicio%" -D "%~dp0data"
sc config "%Servicio%" start= auto
net start "%Servicio%"


REM === ADICIONAR bin NO PATH DO SISTEMA ===
SET "PG_BIN=%~dp0bin"

REM Verifica se já está no PATH
reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path | find /I "%PG_BIN%" >nul
IF %ERRORLEVEL% NEQ 0 (
    echo Adicionando %PG_BIN% ao PATH do sistema...
    for /f "tokens=2*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| find "REG_"') do (
        set "OLD_PATH=%%B"
    )
    reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%OLD_PATH%;%PG_BIN%" /f
    echo Caminho adicionado com sucesso. Pode ser necessário reiniciar para aplicar.
) ELSE (
    echo Caminho %PG_BIN% já está presente no PATH do sistema.
)


--pause
