@echo off
setlocal

set "ROOT_DIR=%~dp0.."
if "%~1"=="" (
    set "CONFIG_FILE=%USERPROFILE%\Saleculator.properties"
) else (
    set "CONFIG_FILE=%~1"
)

for /f "usebackq delims=" %%T in (`powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-Date -Format yyyyMMdd-HHmmss"`) do set "STAMP=%%T"
set "OUT_FILE=%ROOT_DIR%\support-info-%STAMP%.txt"
set "JAVA_INFO=%TEMP%\saleculator-java-%STAMP%.txt"

where java > "%JAVA_INFO%" 2>nul
if errorlevel 1 (
    echo java=missing > "%JAVA_INFO%"
) else (
    java -version >> "%JAVA_INFO%" 2>&1
)

(
echo Saleculator Support Info
echo Generated: %DATE% %TIME%
echo Root: %ROOT_DIR%
echo Config: %CONFIG_FILE%
echo.

echo [release]
if exist "%ROOT_DIR%\release-info.properties" (
    type "%ROOT_DIR%\release-info.properties"
) else (
    echo release-info.properties=missing
)
echo.

echo [os]
ver
echo PROCESSOR_ARCHITECTURE=%PROCESSOR_ARCHITECTURE%
echo COMPUTERNAME=%COMPUTERNAME%
echo USERNAME=%USERNAME%
echo.

echo [java]
type "%JAVA_INFO%"
echo.

echo [package]
if exist "%ROOT_DIR%\dist\Saleculator.jar" (
    dir "%ROOT_DIR%\dist\Saleculator.jar"
) else (
    echo jar=missing
)
if exist "%ROOT_DIR%\lib" (
    powershell -NoProfile -ExecutionPolicy Bypass -Command "(Get-ChildItem -Path '%ROOT_DIR%\lib' -File -Recurse | Measure-Object).Count | ForEach-Object { 'lib.count=' + $_ }"
) else (
    echo lib=missing
)
if exist "%ROOT_DIR%\lib\mysql-connector-java-5.1.7-bin.jar" (echo mysql.legacy=present) else (echo mysql.legacy=missing)
if exist "%ROOT_DIR%\lib\mysql-connector-j-8.4.0.jar" (echo mysql.current=present) else (echo mysql.current=missing)
if exist "%ROOT_DIR%\lib\jSerialComm-2.7.0.jar" (echo jSerialComm=present) else (echo jSerialComm=missing)
echo.

echo [config]
if exist "%CONFIG_FILE%" (
    echo config.exists=true
    powershell -NoProfile -ExecutionPolicy Bypass -Command "$keys='db.URL','db.driver','db.driverlib','db.user','db.password','machine.printer','machine.scale','machine.scanner','machine.screenmode','remoteaccess.webin.enabled','remoteaccess.webin.remotehost'; $p=@{}; Get-Content '%CONFIG_FILE%' | ForEach-Object { if ($_ -match '^\s*([^#][^=]*)\s*=\s*(.*)$') { $p[$matches[1].Trim()]=$matches[2] } }; foreach ($k in $keys) { $v=$p[$k]; if ($k -match '(?i)password|token|key|secret') { if ($v) { $v='<configured>' } else { $v='' } }; Write-Output ($k + '=' + $v) }"
) else (
    echo config.exists=false
)
echo.

echo [devices]
powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-CimInstance Win32_SerialPort -ErrorAction SilentlyContinue | Select-Object DeviceID,Name,Description | Format-Table -AutoSize | Out-String"
powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-CimInstance Win32_USBControllerDevice -ErrorAction SilentlyContinue | Select-Object -First 60 | Format-Table -AutoSize | Out-String"
echo.
) > "%OUT_FILE%"

if exist "%JAVA_INFO%" del /q "%JAVA_INFO%"
echo %OUT_FILE%
