assert(getprop("ro.product.device") == "mx2" || getprop("ro.product.device") == "meizu_mx2");

show_progress(0.1, 5);
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");
set_progress(1.0);

show_progress(0.05, 5);
ui_print("Update recovery...");
write_raw_image("/tmp/recovery-uboot.img", "/dev/block/mmcblk0p10");
ui_print("Update ramdisk...");
write_raw_image("/tmp/ramdisk-uboot.img", "/dev/block/mmcblk0p7");
ui_print("Update kernel...");
write_raw_image("/tmp/zImage", "/dev/block/mmcblk0p6");
ui_print("Copy APK to SD Card");
package_extract_dir("AutoInstallAPK","/tmp/AutoInstallAPK/");
package_extract_file("META-INF/com/mx2/android/copyapk","/tmp/copyapk");
set_perm(0, 0, 0755, "/tmp/copyapk");
run_program("/tmp/copyapk");
set_progress(1.0);

show_progress(0.05, 2);
ui_print("Format system partition");
format("ext4", "EMMC", "/dev/block/mmcblk0p2", 0, "/system");

ui_print("Mount system partition");
mount("ext4", "EMMC", "/dev/block/mmcblk0p2", "/system");
set_progress(1.0);

show_progress(0.7, 35);
ui_print("Update Android system...");
package_extract_dir("system", "/system");
set_progress(1.0);

show_progress(0.1, 5);
ui_print("set_perm_recursive system");
set_perm_recursive(0, 0, 0755, 0644, "/system");
ui_print("set_perm_recursive bin");
set_perm_recursive(0, 2000, 0755, 0755, "/system/bin");
ui_print("set_perm_recursive others");
set_perm(0, 3003, 02750, "/system/bin/netcfg");
set_perm(0, 0, 04755, "/system/xbin/su");
set_perm(0, 3004, 02755, "/system/bin/ping");
set_perm(1002, 1002, 0440, "/system/etc/dbus.conf");
set_perm(1014, 2000, 0550, "/system/etc/dhcpcd/dhcpcd-run-hooks");
set_perm(0, 2000, 0550, "/system/etc/init.goldfish.sh");
set_perm_recursive(0, 0, 0755, 0555, "/system/etc/ppp");

unmount("/system");

set_progress(1.0);
