@echo off
setlocal

REM DorsanDesk last log copier - ASCII safe BAT, no BOM.
REM Source log folder: %APPDATA%\DorsanDesk\log
REM Output folder name is created by PowerShell from Unicode codes.

set "BAT_DIR=%~dp0"
set "PS1=%TEMP%\dorsandesk_send_log_%RANDOM%.ps1"

> "%PS1%" echo $ErrorActionPreference = 'Stop'
>> "%PS1%" echo $batDir = $env:BAT_DIR
>> "%PS1%" echo $logFolder = Join-Path $env:APPDATA 'DorsanDesk\log'
>> "%PS1%" echo $folderName = -join ([char[]]@(0x0627,0x0631,0x0633,0x0627,0x0644,0x0020,0x0628,0x0647,0x0020,0x067E,0x0634,0x062A,0x06CC,0x0628,0x0627,0x0646,0x06CC,0x0020,0x062F,0x0631,0x0633,0x0627,0x0646))
>> "%PS1%" echo $outFolder = Join-Path $batDir $folderName
>> "%PS1%" echo New-Item -ItemType Directory -Path $outFolder -Force ^| Out-Null
>> "%PS1%" echo if (!(Test-Path $logFolder)) {
>> "%PS1%" echo   'DorsanDesk log folder not found.' ^| Out-File -FilePath (Join-Path $outFolder 'README.txt') -Encoding UTF8
>> "%PS1%" echo   Start-Process explorer.exe $outFolder
>> "%PS1%" echo   exit 1
>> "%PS1%" echo }
>> "%PS1%" echo $latest = Get-ChildItem -Path $logFolder -File -ErrorAction SilentlyContinue ^| Sort-Object LastWriteTime -Descending ^| Select-Object -First 1
>> "%PS1%" echo if (!$latest) {
>> "%PS1%" echo   'No log file was found in DorsanDesk log folder.' ^| Out-File -FilePath (Join-Path $outFolder 'README.txt') -Encoding UTF8
>> "%PS1%" echo   Start-Process explorer.exe $outFolder
>> "%PS1%" echo   exit 1
>> "%PS1%" echo }
>> "%PS1%" echo $destPath = Join-Path $outFolder $latest.Name
>> "%PS1%" echo Copy-Item -Path $latest.FullName -Destination $destPath -Force
>> "%PS1%" echo @('Send this folder to Dorsan support.','',('Source: ' + $latest.FullName),('Copied file: ' + $latest.Name),('Last modified: ' + $latest.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'))) ^| Out-File -FilePath (Join-Path $outFolder 'README.txt') -Encoding UTF8
>> "%PS1%" echo Start-Process explorer.exe $outFolder

powershell -NoProfile -ExecutionPolicy Bypass -File "%PS1%"

del "%PS1%" >nul 2>nul

echo.
echo Done. Send the created folder to Dorsan support.
echo.
pause
