assert(getprop("ro.product.device") == "m76" || getprop("ro.product.device") == "espresso5430" || getprop("ro.product.device") == "mx4pro");

show_progress(0.10, 2);
package_extract_file("META-INF/machine_match", "/tmp/machine_match");
set_perm(0, 0, 0755, "/tmp/machine_match");
package_extract_file("META-INF/imei.dat", "/tmp/imei.dat");

if (run_program("/tmp/machine_match", "/tmp/imei.dat") != "0") then
    ui_print("machine not match");
    delete(/tmp/imei.dat);
    abort();
endif;
delete(/tmp/imei.dat);

ui_print("extract uboot...");
package_extract_file("boot.img", "/tmp/boot.img");
ui_print("extract dtb...");
package_extract_file("dtb", "/tmp/dtb");
ui_print("extract zImage...");
package_extract_file("zImage", "/tmp/zImage");
ui_print("extract ramdisk...");
package_extract_file("ramdisk-uboot.img", "/tmp/ramdisk-uboot.img");
ui_print("extract recovery...");
package_extract_file("recovery-uboot.img","/tmp/recovery-uboot.img");

ui_print("Update uboot...");
write_raw_image("/tmp/boot.img", "/dev/block/mmcblk0boot0");
delete("/tmp/boot.img");
ui_print("Update recovery...");
write_raw_image("/tmp/recovery-uboot.img", "/dev/block/platform/15540000.dwmmc0/by-name/recovery");
delete("/tmp/recovery-uboot.img");
ui_print("Update ramdisk...");
write_raw_image("/tmp/ramdisk-uboot.img", "/dev/block/platform/15540000.dwmmc0/by-name/ramdisk");
delete("/tmp/ramdisk-uboot.img");
ui_print("Update kernel...");
write_raw_image("/tmp/zImage", "/dev/block/platform/15540000.dwmmc0/by-name/kernel");
delete("/tmp/zImage");
ui_print("Update dtb...");
write_raw_image("/tmp/dtb", "/dev/block/platform/15540000.dwmmc0/by-name/dtb");
delete("/tmp/dtb");
set_progress(1.0);

show_progress(0.85, 40);
ui_print("Update Android system...");
block_image_update("/dev/block/platform/15540000.dwmmc0/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat");

# install custom partition
show_progress(0.05, 5);
ui_print("Mount custom partition");
mount("ext4", "EMMC", "/dev/block/platform/15540000.dwmmc0/by-name/custom", "/custom");

ui_print("Delete /custom/3rd-party directory...");
delete_recursive("/custom/3rd-party");

ui_print("Update custom partition...");
package_extract_dir("custom", "/custom");

ui_print("set_perm_recursive custom");
set_perm_recursive(0, 0, 0555, 0555, "/custom");

unmount("/custom");

set_progress(1.0);
