ui_print("Upgrade : ALL include recovery");
show_progress(0.05, 3);

delete("/system/bin/ntx_hwconfig-static");
delete("/system/bin/upgrade.sh");
assert(package_extract_file("ntx_hwconfig-static", "/system/bin/ntx_hwconfig-static"));
assert(package_extract_file("META-INF/com/google/android/upgrade.sh", "/system/bin/upgrade.sh"));
set_perm(0, 2000, 0755, "/system/bin/ntx_hwconfig-static");
set_perm(0, 2000, 0755, "/system/bin/upgrade.sh");
assert(run_program("/system/bin/upgrade.sh","setprop"));

getprop("ro.product.device") == "ntx_6sl_eink" || getprop("ro.build.product") == "ntx_6sl_eink" || abort("This package is for tolino vision (ntx_6sl_eink) devices; this is a \"" + getprop("ro.recovery.device") +"!");
ui_print("Target device check: ok");

ui_print("mounting cache...");
show_progress(0.05, 2);
mount("ext4", "EMMC","/dev/block/mmcblk0p6", "/cache");

ui_print("installing recovery and firmware...");
show_progress(0.1, 5);
delete("/system/bin/ntx_hwconfig-static");
delete("/system/bin/upgrade.sh");
assert(package_extract_file("ntx_hwconfig-static", "/system/bin/ntx_hwconfig-static"));
assert(package_extract_file("META-INF/com/google/android/upgrade.sh", "/system/bin/upgrade.sh"));
set_perm(0, 2000, 0755, "/system/bin/ntx_hwconfig-static");
set_perm(0, 2000, 0755, "/system/bin/upgrade.sh");
assert(run_program("/system/bin/upgrade.sh"));

ui_print("installing system...");
ui_print("clean up system...");
show_progress(0.1, 15);
format("ext4","EMMC","/dev/block/mmcblk0p5","0");
ui_print("mounting system...");
show_progress(0.05, 2);
mount("ext4", "EMMC","/dev/block/mmcblk0p5", "/tmp_system");
ui_print("extract package...");
show_progress(0.50, 20);
assert(package_extract_dir("system", "/tmp_system"));
ui_print("setting symlinks and permissions...");
symlink("toolbox", "/tmp_system/bin/cat", "/tmp_system/bin/chmod", "/tmp_system/bin/chown",
        "/tmp_system/bin/mkdir", "/tmp_system/bin/mount", "/tmp_system/bin/mv",
        "/tmp_system/bin/rm", "/tmp_system/bin/umount", "/tmp_system/bin/lsmod",
        "/tmp_system/bin/cmp", "/tmp_system/bin/date", "/tmp_system/bin/dd",
        "/tmp_system/bin/df", "/tmp_system/bin/dmesg", "/tmp_system/bin/getevent",
        "/tmp_system/bin/getprop", "/tmp_system/bin/hd", "/tmp_system/bin/ls",
        "/tmp_system/bin/id", "/tmp_system/bin/ifconfig", "/tmp_system/bin/iftop",
        "/tmp_system/bin/insmod", "/tmp_system/bin/ioctl", "/tmp_system/bin/ionice",
        "/tmp_system/bin/kill", "/tmp_system/bin/ln", "/tmp_system/bin/log",
        "/tmp_system/bin/nandread", "/tmp_system/bin/netstat", "/tmp_system/bin/lsof",
        "/tmp_system/bin/newfs_msdos", "/tmp_system/bin/notify", "/tmp_system/bin/printenv",
        "/tmp_system/bin/ps", "/tmp_system/bin/reboot", "/tmp_system/bin/renice",
        "/tmp_system/bin/rmdir", "/tmp_system/bin/rmmod", "/tmp_system/bin/route",
        "/tmp_system/bin/schedtop", "/tmp_system/bin/sendevent",
        "/tmp_system/bin/setconsole", "/tmp_system/bin/setprop", "/tmp_system/bin/sleep",
        "/tmp_system/bin/smd", "/tmp_system/bin/start", "/tmp_system/bin/stop",
        "/tmp_system/bin/sync", "/tmp_system/bin/top", "/tmp_system/bin/uptime",
        "/tmp_system/bin/vmstat", "/tmp_system/bin/watchprops","/tmp_system/bin/wipe");
set_perm_recursive(0, 0, 0755, 0644, "/tmp_system");
set_perm_recursive(0, 0, 0755, 0644, "/tmp_system/app");
set_perm_recursive(0, 2000, 0755, 0755, "/tmp_system/bin");
set_perm(0, 3003, 02750, "/system/bin/netcfg");
set_perm(0, 3004, 02755, "/system/bin/ping");
set_perm(0, 2000, 06755, "/system/bin/reboot");
set_perm(0, 2000, 06750, "/system/bin/run-as");
set_perm_recursive(0, 0, 0755, 0644, "/tmp_system/etc");
set_perm(1002, 1002, 0440, "/tmp_system/etc/dbus.conf");
set_perm(0, 2000, 0550, "/tmp_system/etc/init.goldfish.sh");
set_perm(0, 2000, 0550, "/tmp_system/etc/init.gprs-pppd");
set_perm_recursive(1002, 1002, 0755, 0440, "/tmp_system/etc/bluetooth");
set_perm(0, 0, 0755, "/tmp_system/etc/bluetooth");
set_perm(3002, 3002, 0444, "/tmp_system/etc/bluetooth/blacklist.conf");
set_perm(1000, 1000, 0640, "/tmp_system/etc/bluetooth/auto_pairing.conf");
set_perm(1014, 2000, 0550, "/tmp_system/etc/dhcpcd/dhcpcd-run-hooks");
set_perm_recursive(0, 0, 0755, 0555, "/tmp_system/etc/ppp");
set_perm_recursive(0, 0, 0755, 0644, "/tmp_system/fonts");
set_perm_recursive(0, 0, 0755, 0644, "/tmp_system/framework");
set_perm_recursive(0, 0, 0755, 0644, "/tmp_system/lib");
set_perm_recursive(0, 0, 0755, 0644, "/tmp_system/usr");
set_perm_recursive(0, 2000, 0755, 0755, "/tmp_system/vendor");
set_perm_recursive(0, 2000, 0755, 0755, "/tmp_system/xbin");
set_perm(0, 0, 06755, "/tmp_system/xbin/tcpdump");
set_perm(0, 0, 06755, "/tmp_system/xbin/su");
set_perm(0, 0, 06755, "/tmp_system/xbin/procrank");
set_perm(0, 0, 06755, "/tmp_system/xbin/procmem");
set_perm(0, 0, 06755, "/tmp_system/xbin/librank");
set_perm(0, 0, 06755, "/tmp_system/xbin/attrib");

ui_print("unmount system...");
show_progress(0.15, 2);
unmount("/tmp_system");

delete("/extsd/update.zip");
delete("/sdcard/update.zip");

ui_print("install complete!");
show_progress(0.0, 0);


