echo off

net session >nul 2>&1
if %errorLevel% == 0 (
    echo Success: Administrative permissions are confirmed.
) else (
    echo Failure: Administrative permissions are required.
    echo Click the right mouse button on the file name and choose "Run As Administrator"
    goto :exit
)

echo ===================================================================
echo ============== Banalytics Box service installation ================
echo ==============                                     ================

set scriptpath=%~dp0
set packagepath=%scriptpath%

set b_box_version=0.1.0

echo ============== Downloading JDK 21 ================
%scriptpath%/third-party/curl -L -k https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.zip -o "%packagepath%\modules-download\jdk.zip"

powershell -Command "Expand-Archive -Path '%packagepath%\modules-download\jdk.zip' -DestinationPath '%packagepath%\jdk'; $src = Get-ChildItem '%packagepath%\jdk' | Where-Object {$_.PSIsContainer}; foreach ($item in Get-ChildItem $src.FullName) {Move-Item -LiteralPath $item.FullName -Destination '%packagepath%\jdk'}; Remove-Item -Force %packagepath%\jdk\$src"


echo ============== Downloading Banalytics Box module ================
%scriptpath%/third-party/curl -L -k https://europe-central2-maven.pkg.dev/banalytics-production/maven-repo/com/banalytics/box/core/%b_box_version%/core-%b_box_version%.jar -o "%packagepath%\banalytics-box.jar"
%scriptpath%/third-party/curl -L -k https://europe-central2-maven.pkg.dev/banalytics-production/maven-repo/com/banalytics/box/core/%b_box_version%/core-%b_box_version%-bin.zip -o "%packagepath%\modules-download\banalytics-box.jar-bin.zip"

tar -xf "%packagepath%\modules-download\banalytics-box.jar-bin.zip" -C "%packagepath%\modules"

echo ==========                                             ============
echo ========== Configure Banalytic Box Windows Service     ============

set nssmpath=%packagepath%\third-party\nssm

echo "Banalytics Box home: %packagepath%"

%nssmpath% install BanalyticsBox "%packagepath%BanalyticsBox_Win64-start.bat"
%nssmpath% set BanalyticsBox AppDirectory %packagepath%
%nssmpath% set BanalyticsBox AppEnvironmentExtra BANALYTICS_HOME=%packagepath%
%nssmpath% set BanalyticsBox DisplayName Banalytics Box
%nssmpath% set BanalyticsBox Description Banalytics Box Service
%nssmpath% set BanalyticsBox Start SERVICE_AUTO_START
%nssmpath% set BanalyticsBox ObjectName LocalSystem
%nssmpath% set BanalyticsBox Type SERVICE_WIN32_OWN_PROCESS
%nssmpath% set BanalyticsBox AppPriority NORMAL_PRIORITY_CLASS
%nssmpath% set BanalyticsBox AppNoConsole 0
%nssmpath% set BanalyticsBox AppAffinity All
%nssmpath% set BanalyticsBox AppStopMethodSkip 0
%nssmpath% set BanalyticsBox AppStopMethodConsole 1500
%nssmpath% set BanalyticsBox AppStopMethodWindow 1500
%nssmpath% set BanalyticsBox AppStopMethodThreads 1500
%nssmpath% set BanalyticsBox AppThrottle 1500
%nssmpath% set BanalyticsBox AppExit Default Restart
%nssmpath% set BanalyticsBox AppRestartDelay 0

echo ===================================================================
echo ==========              Starting Service...            ============
echo ==========                                             ============

%nssmpath% start BanalyticsBox

ping -n 2 127.0.0.1

echo ==========  Note: PIN code of the local console: default
echo ==========  Note: For safety, change the default pin to your own

echo ==========                                             ============
echo ==========     Banalytics Box service is installed     ============
echo ==========                                             ============
echo ==========        WINDOWS RESTART MAY REQUIRED         ============
echo ==========                                             ============
echo ===================================================================


ping -n 10 127.0.0.1

start http://localhost:8080

:exit

pause