@setlocal

@set HACKABLE=
@if "%H-TRAC-ROOT%"=="" (call tracvars.cmd) ELSE (set HACKABLE=true)

@echo.
@echo == PostgreSQL-based environment creation tool
@echo.
@echo First parameter : environment name
@echo Second parameter: PostgreSQL schema name (or leave blank to use the environment name)
@echo.

@if "%H-TRAC-PGSQL%"=="" (echo Error: please set the "H-TRAC-PGSQL" environment variable in the "tracvars.cmd" file. && echo. && goto EXIT)



@if not "%1" == "" (set ENVNAME=%1 && goto ENVSET)

@echo Please enter the environment name:
@set /p ENVNAME=
@echo.

@if "%ENVNAME%" == "" (echo Error: You must set an environment name. && echo. && goto EXIT)

:ENVSET

@echo == Environment name: %ENVNAME%
@echo.



@if not "%2" == "" (set PGSCHEMANAME=%2 && goto PGSCHEMASET)
@if "%HACKABLE%" == "true" if not "%1" == "" (set PGSCHEMANAME=%1 && goto PGSCHEMASET)

@echo Please enter the PostgreSQL schema name (or leave blank if you want to use the environment name):
@set /p PGSCHEMANAME=
@echo.

@if "%PGSCHEMANAME%" == "" (set PGSCHEMANAME=%ENVNAME% && goto PGSCHEMASET)

:PGSCHEMASET

@echo == PostgreSQL schema: %PGSCHEMANAME%
@echo.

@set DBNAME=%H-TRAC-PGSQL%%PGSCHEMANAME%

@echo == Database connection string: %DBNAME%
@echo.



@echo == Creating environment ...
@echo.

@trac-admin "%H-TRAC-DATA%\%ENVNAME%" initenv "%ENVNAME%" %DBNAME%

@echo == Granting admin rights to the anonymous user ...
@echo.

@trac-admin "%H-TRAC-DATA%\%ENVNAME%" permission add anonymous TRAC_ADMIN

@echo == Enabling Mercurial plugin ...
@echo.

@trac-admin "%H-TRAC-DATA%\%ENVNAME%" config set components "tracext.hg.*" "enabled"

@echo == Enabling tags plugin ...
@echo.

@trac-admin "%H-TRAC-DATA%\%ENVNAME%" config set components "tractags.*" "enabled"

@echo == Database and wiki upgrade ...
@echo.

@trac-admin "%H-TRAC-DATA%\%ENVNAME%" upgrade
@echo.

@trac-admin "%H-TRAC-DATA%\%ENVNAME%" wiki upgrade
@echo.

@echo == done.
@echo.



:EXIT

@pause

@endlocal
