@echo off

SET POOPFX=%~dp0

if NOT "%1"=="/b" GOTO PROC
SHIFT
SET EXECPFX=start /b /wait /belownormal

:PROC
 %POOPFX%binmay.exe -i "%~1" -o nul -s "00 00 00 08 61 63 54 4c" 2> nul
 if ERRORLEVEL 1 (
  %POOPFX%touch.exe -r "%~1" "%~1.time"
  %EXECPFX% %POOPFX%optipng.exe -zw32k -zc9 -zm9 -zs0,3 -f0,5 -i0 "%~1"
  if not ERRORLEVEL 1 (
   %EXECPFX% %POOPFX%advdef.exe -z -4 "%~1"
   %EXECPFX% %POOPFX%deflopt.exe -b "%~1"
   %EXECPFX% %POOPFX%pngout /y /b0 "%~1"
   if not ERRORLEVEL 2 (
    %EXECPFX% %POOPFX%advdef.exe -z -4 "%~1"
    %EXECPFX% %POOPFX%deflopt.exe -b "%~1"
   )
  )
  %POOPFX%touch.exe -r "%~1.time" "%~1"
  if exist "%~1.time" del "%~1.time"
 ) else (
  ren "%~1" "%~n1.apng"
 )

SET EXECPFX=
SET POOPFX=