@REM set local scope for the variables with windows NT shell
@if "%OS%"=="Windows_NT" @setlocal
@if "%OS%"=="WINNT" @setlocal
@echo off

if "%OS%"=="Windows_NT" SET "OPOOPRESS_HOME=%~dp0.opoopress"
if "%OS%"=="WINNT" SET "OPOOPRESS_HOME=%~dp0.opoopress"

rem set OPOOPRESS_HOME=%~dp0%.opoopress


rem set JAVA_HOME=
rem set M2_HOME=

if not exist "%OPOOPRESS_DIST%" mkdir %OPOOPRESS_DIST%

:chkJHome
if not "%JAVA_HOME%" == "" goto okJHome
goto installJDK

:okJHome
if exist "%JAVA_HOME%\bin\java.exe" goto chkMHome

echo.
echo ERROR: JAVA_HOME is set to an invalid directory.
echo JAVA_HOME = "%JAVA_HOME%"
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation
echo.
goto error

:chkMHome
if not "%M2_HOME%"=="" goto okMHome
goto installMaven

:okMHome
if exist "%M2_HOME%\bin\mvn.bat" goto runm2

echo.
echo ERROR: M2_HOME is set to an invalid directory.
echo M2_HOME = "%M2_HOME%"
echo Please set the M2_HOME variable in your environment to match the
echo location of the Maven installation
echo.
goto error


:installJDK
 

:setJHome
set JAVA_HOME=%OPOOPRESS_HOME%\tools\jdk1.6
set CLASSPATH=.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar
goto okJHome


:installMaven


rem : change settings.xml
move %OPOOPRESS_HOME%\tools\apache-maven-3.1.1\conf\settings.xml %OPOOPRESS_HOME%\tools\apache-maven-3.1.1\conf\settings_bak.xml 
copy %OPOOPRESS_HOME%\conf\maven.settings.xml.part1 %OPOOPRESS_HOME%\tools\apache-maven-3.1.1\conf\settings.xml
echo ^<localRepository^>%OPOOPRESS_HOME%\tools\m2.repo^</localRepository^> >>%OPOOPRESS_HOME%\tools\apache-maven-3.1.1\conf\settings.xml
type %OPOOPRESS_HOME%\conf\maven.settings.xml.part2>>%OPOOPRESS_HOME%\tools\apache-maven-3.1.1\conf\settings.xml

:setMHome
set M2_HOME=%OPOOPRESS_HOME%\tools\apache-maven-3.1.1
goto okMHome


rem =============================

:runm2
if "%1" == "new-post"	goto runm22
if "%1" == "new-page"	goto runm22
if "%1" == "install"	goto runm22
if "%1" == "build"	goto runm22
if "%1" == "generate"	goto runm22
if "%1" == "deploy"	goto runm22
if "%1" == "preview"	goto runm22
if "%1" == "-version"   goto version
if "%1" == "-jv"	goto jversion
if "%1" == "-mv"	goto mversion
if "%1" == ""		goto jversion

echo Unkown command: %*
goto end

:jversion
echo Java home: %JAVA_HOME%
"%JAVA_HOME%\bin\java.exe" -version
goto end

:mversion
echo %M2_HOME%
"%M2_HOME%\bin\mvn.bat" -version
goto end

:version
echo Java home: %JAVA_HOME%
"%JAVA_HOME%\bin\java.exe" -version
echo %M2_HOME%
"%M2_HOME%\bin\mvn.bat" -version
goto end

:runm22

:checkM2repo
if exist "%USERPROFILE%\.m2\settings.xml" goto runm222
if exist "%OPOOPRESS_HOME%\tools\m2.repo" goto runm222

rem download m2.repo


:runm222
"%M2_HOME%\bin\mvn.bat" op:%*
goto end

:error
if "%OS%"=="Windows_NT" @endlocal
if "%OS%"=="WINNT" @endlocal
set ERROR_CODE=1

:end
@REM set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" goto endNT
if "%OS%"=="WINNT" goto endNT

@REM For old DOS remove the set variables from ENV - we assume they were not set
@REM before we started - at least we don't leave any baggage around
set OPOOPRESS_HOME=
set OPOOPRESS_BIN=
set OPOOPRESS_DIST=
goto postExec

:endNT
endlocal & set ERROR_CODE=%ERROR_CODE%

:postExec

cmd /C exit /B %ERROR_CODE%
