@echo off
setlocal EnableExtensions EnableDelayedExpansion
REM -------------------------------------------------------------------------
REM
REM Author: nakca
REM
REM ==================================================================
REM If more then one service is needed, copy this file to another name
REM and use unique names for SHORTNAME, DISPLAYNAME, DESCRIPTION
REM ==================================================================
set SHORTNAME=TurksatImza
set DISPLAYNAME="TurksatImza"
set DESCRIPTION="TurksatImza Imza Servisi"
REM ========================================================

set "DIRNAME=%~dp0%"
cd > nul
pushd %DIRNAME%..\..
set "RESOLVED_BELGENET_SERVICE_HOME=%CD%"
popd
set DIRNAME=

if "x%BELGENET_SERVICE_HOME%" == "x" (
  set "BELGENET_SERVICE_HOME=%RESOLVED_BELGENET_SERVICE_HOME%" 
)

pushd "%BELGENET_SERVICE_HOME%"
set "SANITIZED_BELGENET_SERVICE_HOME=%CD%"
popd

if "%RESOLVED_BELGENET_SERVICE_HOME%" NEQ "%SANITIZED_BELGENET_SERVICE_HOME%" (
    echo WARNING BELGENET_SERVICE_HOME may be pointing to a different installation - unpredictable results may occur.
    echo Please check that this script is located in the bin\services\ directory.
    goto cmdEnd
)
rem Find singNativeOsService.jar to check BELGENET_SERVICE_HOME
if not exist "%BELGENET_SERVICE_HOME%\singNativeOsService.jar" (
  echo Could not locate "%BELGENET_SERVICE_HOME%\singNativeOsService.jar".
  echo Please check that this script is located in the bin\services\ directory.
  goto cmdEnd
)

java.exe /?  2> NUL
IF %ERRORLEVEL%==9009 (
    ECHO JAVA not exists in path
)

rem if "x%JAVA_HOME%" == "x" (
rem   echo JAVA_HOME is not set.
rem   echo Set JAVA_HOME to the directory of your local JDK to avoid this message.
rem   goto cmdEnd
rem )

if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
  echo Using the X86-64bit version of prunsrv
  set PRUNSRV="%BELGENET_SERVICE_HOME%\bin\service\amd64\belgenet-service"
) else (
  echo Using the X86-32bit version of prunsrv
  set PRUNSRV="%BELGENET_SERVICE_HOME%\bin\service\belgenet-service"
)


rem echo Using the X86-32bit version of prunsrv
rem set PRUNSRV="%BELGENET_SERVICE_HOME%\bin\service\belgenet-service"
set PRUNSRVMGR="%BELGENET_SERVICE_HOME%\bin\service\belgenet-mgr"

if /I "%1" == "install"   goto cmdInstall
if /I "%1" == "uninstall" goto cmdUninstall
if /I "%1" == "start"     goto cmdStart
if /I "%1" == "stop"      goto cmdStop
if /I "%1" == "restart"   goto cmdRestart
if /I "%1" == "ed"        goto cmdEd

:cmdUsage
echo Usage:
echo   service install 
echo   service uninstall
echo   service start
echo   service stop
echo   service restart
echo   service ed

goto endBatch

:cmdInstall
shift

set CONTROLLER=localhost:9000
set DC_HOST=master
set IS_DOMAIN=false
set LOGLEVEL=INFO
set JBOSSUSER=
set PASSWORD=



:doInstall

set STARTPARAM="tr.com.turksat.Main"
set STOPPARAM="tr.com.turksat.Main"
set LOGPATH=%BELGENET_SERVICE_HOME%\log
set CLASS_PATH="%BELGENET_SERVICE_HOME%"\singNativeOsService.jar


rem echo %STARTPARAM%
rem echo %STOPPARAM%
rem echo %LOGLEVEL%
rem goto endBatch

%PRUNSRV% install %SHORTNAME% --DisplayName=%DISPLAYNAME% --Install=%PRUNSRV% --Description %DESCRIPTION% --Classpath=%CLASS_PATH% --LogLevel=%LOGLEVEL% --LogPath="%LOGPATH%" --LogPrefix=service --StdOutput=auto --StdError=auto --StartMode=jvm --StartPath="%BELGENET_SERVICE_HOME%" --StartClass=%STARTPARAM% --StopMode=jvm --StopPath="%BELGENET_SERVICE_HOME%"  --StopClass=%STOPPARAM%  --StartMethod=start --StopMethod=stop
goto cmdStart

:cmdUninstall
%PRUNSRV% stop %SHORTNAME%
if "%errorlevel%" == "0" (
  %PRUNSRV% delete %SHORTNAME%
) else (
  echo Unable to stop the service
)
goto cmdEnd

:cmdStart
%PRUNSRV% start %SHORTNAME%
goto cmdEnd

:cmdStop
%PRUNSRV% stop %SHORTNAME%
goto cmdEnd

:cmdRestart
%PRUNSRV% stop %SHORTNAME%
if "%errorlevel%" == "0" (
  %PRUNSRV% start %SHORTNAME%
) else (
  echo Unable to stop the service
)
goto cmdEnd

:cmdEd
%PRUNSRVMGR% //ES//%SHORTNAME%
goto cmdEnd

:cmdEnd
REM need to add other error messages (list higher nr first !)
if errorlevel 8 (
  echo ERROR: The service %SHORTNAME% already exists
  goto endBatch
)
if errorlevel 2 (
  echo ERROR: Failed to load service configuration
  goto endBatch
)
if errorlevel 0 (
  echo Success
  goto endBatch
)
echo errorlevel=%errorlevel%

rem nothing below, exit
:endBatch
