@echo off
::
:: ilink TeamCall CA NFON (Windows)
:: windows-installer.bat
::
:: Copyright (c) 2026 by ilink Kommunikationssysteme GmbH.  All Rights Reserved.
::
:: This script will run the Java based installer for CA NFON
::
:: Requirement: java.exe needs to be on the search path.
::


:: Check for administrative privileges

net session >nul 2>&1
if %errorLevel% neq 0 (
	echo This script must be run as Administrator.
	echo Please right-click the script and select "Run as administrator".
	pause
	exit /b 1
)

echo Running with administrative privileges - continuing...



:: Set script log

:: "%~dp0" expands to the folder of this batch file
set LOG_FILE=""%~dp0"\CANFON-install.log"

echo.
echo Starting CA NFON Windows install script
echo. > "%LOG_FILE%"
echo Starting CA NFON Windows install script >> "%LOG_FILE%"



:: Set variables

:: java executable used for running the IzPack installer
:: (needs to be on the search path)
set JAVA_BIN=java.exe


set DEBUGOPTIONS=-DSTACKTRACE=true
:: Use this for debugging:
::set DEBUGOPTIONS=-DTRACE=true -DSTACKTRACE=true


:: Unlike bash, the Windows batch processor does not expand wildcards in arguments.
:: So we use the following to determine the exact name of the installer jar file:
:: (%~dp0 expands to the path of the script's folder, including a trailing backslash)
for %%A in ("%~dp0"ca_nfon_install*.jar) do (
    set INSTALLER=%%A
)



:: Run the installer

echo.
echo Starting CA NFON installer
echo. >> "%LOG_FILE%"
echo Starting CA NFON installer >> "%LOG_FILE%"

echo.
echo ^> "%JAVA_BIN%" %DEBUGOPTIONS% -jar "%INSTALLER%"
echo. >> "%LOG_FILE%"
echo ^> "%JAVA_BIN%" %DEBUGOPTIONS% -jar "%INSTALLER%" >> "%LOG_FILE%"

"%JAVA_BIN%" %DEBUGOPTIONS% -jar "%INSTALLER%" 2>&1 >> "%LOG_FILE%"
set RETVAL=%ERRORLEVEL%
if %RETVAL% NEQ 0 (
	echo "The installer failed (code %RETVAL%)."
	echo "Check log file %LOG_FILE% for details."
	echo "The installer failed (code %RETVAL%)." >> %LOG_FILE%
	goto Error
)



:: Consolidate the logs

echo. >> "%LOG_FILE%"
echo. >> "%LOG_FILE%"
echo Installer warnings and errors: >> "%LOG_FILE%"
echo ---------------------------------------------------------------- >> "%LOG_FILE%"
type izpack.log >> "%LOG_FILE%"
echo ---------------------------------------------------------------- >> "%LOG_FILE%"

echo. >> "%LOG_FILE%"
echo. >> "%LOG_FILE%"
echo installFirstNode log: >> "%LOG_FILE%"
echo ---------------------------------------------------------------- >> "%LOG_FILE%"
type installFirstNode.log >> "%LOG_FILE%"
echo ---------------------------------------------------------------- >> "%LOG_FILE%"

echo. >> "%LOG_FILE%"
echo. >> "%LOG_FILE%"
echo registerService log: >> "%LOG_FILE%"
echo ---------------------------------------------------------------- >> "%LOG_FILE%"
type registerService.log >> "%LOG_FILE%"
echo ---------------------------------------------------------------- >> "%LOG_FILE%"
echo. >> "%LOG_FILE%"
echo. >> "%LOG_FILE%"


:: Remove consolidated logs (these have been written to $INSTALLLOGFILE above)

del izpack.log >nul 2>&1
del installFirstNode.log >nul 2>&1
del registerService.log >nul 2>&1



:: End

echo.
echo.
echo NOTE: The service has been installed and has been started
echo. >> "%LOG_FILE%"
echo. >> "%LOG_FILE%"
echo NOTE: The service has been installed and has been started >> "%LOG_FILE%"

goto ScriptEnd



rem ###########################################################################################################################
:Error

echo. >> "%LOG_FILE%"
echo ERROR! >> "%LOG_FILE%"



rem ###########################################################################################################################
:ScriptEnd

echo. >> "%LOG_FILE%"
echo. >> "%LOG_FILE%"


echo.
echo.
echo Generated installer log file:
echo %LOG_FILE%
echo.
