@echo off
SET CMD_NAME=
set num_sizes=
set delta=


echo start to pack system.img

if  not exist du.exe (
set cmd_name=du
goto error)

if not exist ..\temp\recovery\recovery\file_contexts (
    echo file_contexts not exist,convert
    fct.exe -o ..\temp\recovery\recovery\file_contexts -e ..\temp\recovery\recovery\file_contexts.bin
) else (
    echo file_contexts exist
)

echo start to estimate num of blocks
cd ..\temp
for /f  %%a IN ('..\bin\du -sb system') DO set num_sizes=%%a
cd ..\bin
set /a "num_sizes>>=20"
echo num_sizes = %num_sizes%M
if %num_sizes% LSS 400 set delta=108
if %num_sizes% gtr 400 set delta=216
if %num_sizes% gtr 800 set delta=324
if %num_sizes% gtr 1000 set delta=440
echo delta = %delta%M

set /A num_sizes= %num_sizes%+%delta%

echo num_sizes = %num_sizes%M

make_ext4fs -l %num_sizes%M -s -S ..\temp\recovery\recovery\file_contexts -a system ..\temp\system.img ..\temp\system 

echo Info:Pack system.img success.

@echo on