@echo off
if (%~1)==() GOTO END
@echo Optimizing %~1...
md "%~dp0apkopt_temp_%~n1"
md "%~dp0original"
md "%~dp0optimized"
if %2 == z GOTO skip1
7za x -o"%~dp0apkopt_temp_%~n1" "../place-apk-here-to-batch-optimize/%~1"
mkdir temp
xcopy "%~dp0apkopt_temp_%~n1\res\*.9.png" "%~dp0temp" /S /Y
:: 
:: -o99 specifies how much the PNG is optimized. Lower numbers will make the optimization process faster, higher numbers will result in smaller PNGs.
::
roptipng -o99 "%~dp0apkopt_temp_%~n1\**\*.png"
copy "..\place-apk-here-to-batch-optimize\%~1" "%~dp0original"
del /q "..\place-apk-here-to-batch-optimize\%~1"

:: 
:: -mx5 indicates the ZIP's compression level. Set this from -mx0 to -mx9, higher compression levels may not work!
:: 
xcopy "%~dp0temp" "%~dp0apkopt_temp_%~n1\res" /S /Y
rmdir /S /Q "%~dp0temp" 
if %2 == p GOTO skip2
7za a -tzip "%~dp0optimized\%~n1.unaligned.apk" "%~dp0apkopt_temp_%~n1\*" -mx9 
rd /S /Q "%~dp0apkopt_temp_%~n1"
::
:: runs zipalign
::
zipalign -v 4 "%~dp0optimized\%~n1.unaligned.apk" "%~dp0optimized\%~n1"
del /q "%~dp0optimized\%~n1.unaligned.apk"
@echo Optimization complete for %~1
:END
call apkopt_all
:skip1
zipalign -v 4 "../place-apk-here-to-batch-optimize/%~1" "%~dp0optimized\%~n1.apk"
del /S /Q "../place-apk-here-to-batch-optimize/%~1"
rmdir /S /Q "%~dp0temp" 
rd /S /Q "%~dp0apkopt_temp_%~n1"
call apkopt_all
:skip2
7za a -tzip "%~dp0optimized\%~n1.apk" "%~dp0apkopt_temp_%~n1\*" -mx9 
rd /S /Q "%~dp0apkopt_temp_%~n1"
call apkopt_all
