@echo off

set SERVICE_NAME=dbXML Server

if "%1" EQU "install" goto install
if "%1" EQU "uninstall" goto uninstall
goto usage

:install
if "%DBXML_HOME%"=="" goto nodbXMLHome
if "%JAVA_HOME%"=="" goto noJavaHome

set LOCALCLASSPATH=
for %%i in ("%DBXML_HOME%\java\lib\*.jar") do call "%DBXML_HOME%\bin\lcp.bat" %%i

set JAVA_PARAMS=
set JAVA_PARAMS=%JAVA_PARAMS% -Xms64m 
set JAVA_PARAMS=%JAVA_PARAMS% -Xmx384m

set JAVA_PROPS=
set JAVA_PROPS=%JAVA_PROPS% -Ddbxml.home="%DBXML_HOME%"
set JAVA_PROPS=%JAVA_PROPS% -Dlabrador.quiet=true
set JAVA_PROPS=%JAVA_PROPS% -Dlabrador.config="%DBXML_HOME%\config\labrador.xml" 
set JAVA_PROPS=%JAVA_PROPS% -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl 
set JAVA_PROPS=%JAVA_PROPS% -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl

set START_CLASS=com.dbxml.db.server.Server
set START_PARAMS=%DBXML_HOME%\config\system.xml

set STOP_CLASS=com.dbxml.db.server.Server
set STOP_METHOD=stop

ntsvc.exe -install "%SERVICE_NAME%" "%JAVA_HOME%\jre\bin\client\jvm.dll" %JAVA_PARAMS% %JAVA_PROPS% -Djava.class.path="%LOCALCLASSPATH%" -start %START_CLASS% -params "%START_PARAMS%" -stop %STOP_CLASS% -method %STOP_METHOD%
net start "%SERVICE_NAME%"
goto end

:uninstall
net stop "%SERVICE_NAME%"
ntsvc.exe -uninstall "%SERVICE_NAME%"
goto end

:usage
echo "Usage:  install-ntsvc install|uninstall"
echo "Where:  install|uninstall = the action to be taken"
goto end

:nodbXMLHome
echo Please set DBXML_HOME to the root directory of the dbXML installation
goto end

:noJavaHome
echo Please set JAVA_HOME to the root directory of the Java installation
goto end

:end
set SERVICE_NAME=
set LOCALCLASSPATH=
set JAVA_PROPS=
set JAVA_PARAMS=
set START_CLASS=
set START_PARAMS=
set STOP_CLASS=
set STOP_METHOD=
