@echo off

set Psave=%PATH%
set path=bin

set BIN=bin
set DIR=temp
set SRC=%1

if not exist "%SRC%" exit

for %%a in ("%SRC%") do set DST=%%~na.unp
del %DST% >nul 2>nul

echo input file = %SRC%
echo output file = %DST%

rd /S /Q temp  >nul 2>nul
md temp        >nul 2>nul

%BIN%\wrapper x %SRC% %DIR%\3c %DIR%\result.flif
%BIN%\plzma d %DIR%\3c - | %BIN%\shar x - temp

%BIN%\precomp64 -r -o%DIR%\begin %DIR%\begin_pcf.
%BIN%\precomp64 -r -o%DIR%\tail %DIR%\tail_pcf.

for %%a in (%DIR%/begin) do set START=%%~za
echo START=%START%

for /F "delims=;" %%a in ('%BIN%\flif -i %DIR%\result.flif ^| sed -e "s/^.*FLIF image, \([0-9]*\)x.*$/\1/"') do set HALF_WIDTH=%%a
echo HALF_WIDTH=%HALF_WIDTH%

set PADDING=00
for /F "delims=;" %%a in ('%BIN%\head -c1 %DIR%/padding ^| %BIN%\xxd -ps') do set PADDING=%%a
echo PADDING=%PADDING%

echo flif -d %DIR%/result.flif %DIR%/result.flif.png
%BIN%\flif -d %DIR%/result.flif %DIR%/result.flif.png

echo convert %DIR%/result.flif.png -depth 16 rgba:-
%BIN%\convert %DIR%/result.flif.png -depth 16 rgba:- >temp\1

echo arw_encode %HALF_WIDTH% %START% %DIR%\alarms.bin %PADDING%
%BIN%\arw_encode %HALF_WIDTH% %START% %DIR%\alarms.bin %PADDING% <temp\1 >temp\2

copy /b %DIR%\begin +temp\2 temp\3 >nul 2>nul

cut_tail.exe %DIR%\3 %DIR%\padding %DIR%\3a

copy /b %DIR%\3a +temp\tail %DIR%\3b >nul 2>nul

%BIN%\xdelta3.exe -f -d -s %DIR%\3b %DIR%\delta %DIR%\3d

move %DIR%\3d %DST% >nul 2>nul
rd /S /Q temp
echo See %DST%

set path=%Psave%

