mount("ubifs", "UBI", "system", "/system", "");
ui_print("Source: None");
ui_print("Target: None");
ui_print("Verifying current system...");
show_progress(0.100000, 0);
apply_patch_check("/system/usr/bin/atfwd_daemon", "46dfccbaff93e3e47bace7208fc5cf2dd56aa9ed", "fb83d9985286ad899d2aab08d0c451c4f737ce86") || abort("E3005: \"/system/usr/bin/atfwd_daemon\" has unexpected contents.");
apply_patch_check("/system/var/lib/opkg/status", "786d62e1fb302a1a7c8a00944312e1c96c8f5645", "976bbfa2ff56997e2026cfca9b9444a019ef2593") || abort("E3005: \"/system/var/lib/opkg/status\" has unexpected contents.");
apply_patch_space(112596) || abort("E3006: Not enough free space on /cache to apply patches.");

# ---- radio update verification ----

ui_print("Verifying radio-update...");
apply_patch_check("/firmware/image/modem.b01", "93808d4ad61572c977349d8553170197e37c9963", "b120275a6740becd9a26b3a039da71d3f3a9a7ae") || abort("E3005: \"/firmware/image/modem.b01\" has unexpected contents.");
apply_patch_check("/firmware/image/modem.b20", "457f38356623d4278482a738d15d528402dd2e68", "103ad7db0dfee1d391cdc1a26a94dfd150d8ff3c") || abort("E3005: \"/firmware/image/modem.b20\" has unexpected contents.");
apply_patch_check("/firmware/image/modem.mdt", "443fdd87a964d3db92da68bffec41e6cc37fd37d", "83cbc9b25c17215fa03df74cfb7757f68b0fa661") || abort("E3005: \"/firmware/image/modem.mdt\" has unexpected contents.");

# ---- start making changes here ----

ui_print("Removing unneeded files...");
delete("/system/recovery.img", "/system/etc/version",
       "/system/etc/timestamp",
       "/system/build.prop");
show_progress(0.800000, 0);
ui_print("Patching system files...");
apply_patch("/system/usr/bin/atfwd_daemon", "-",
            46dfccbaff93e3e47bace7208fc5cf2dd56aa9ed, 112596,
            fb83d9985286ad899d2aab08d0c451c4f737ce86,
            package_extract_file("patch/system/usr/bin/atfwd_daemon.p")) ||
    abort("E3008: Failed to apply patch to /system/usr/bin/atfwd_daemon");
set_progress(0.537213);
apply_patch("/system/var/lib/opkg/status", "-",
            786d62e1fb302a1a7c8a00944312e1c96c8f5645, 96996,
            976bbfa2ff56997e2026cfca9b9444a019ef2593,
            package_extract_file("patch/system/var/lib/opkg/status.p")) ||
    abort("E3008: Failed to apply patch to /system/var/lib/opkg/status");
set_progress(0.999995);
show_progress(0.100000, 10);
ui_print("Unpacking new system files...");
package_extract_dir("system", "/system");
ui_print("Symlinks and permissions...");
set_metadata("/system", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:root_t:s0");
set_metadata("/system/build.prop", "uid", 0, "gid", 0, "mode", 0644, "capabilities", 0x0, "selabel", "system_u:object_r:leprop_system_data_t:s15:c0.c1023");
set_metadata("/system/etc", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:etc_t:s0");
set_metadata("/system/etc/timestamp", "uid", 0, "gid", 0, "mode", 0644, "capabilities", 0x0, "selabel", "system_u:object_r:etc_t:s0");
set_metadata("/system/etc/version", "uid", 0, "gid", 0, "mode", 0644, "capabilities", 0x0, "selabel", "system_u:object_r:etc_t:s0");
set_metadata("/system/usr", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:usr_t:s0");
set_metadata("/system/usr/bin", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:bin_t:s0");
set_metadata("/system/usr/bin/atfwd_daemon", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:atfwd_exec_t:s0");
set_metadata("/system/var", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:var_t:s0");
set_metadata("/system/var/lib", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:var_lib_t:s0");
set_metadata("/system/var/lib/opkg", "uid", 0, "gid", 0, "mode", 0755, "capabilities", 0x0, "selabel", "system_u:object_r:var_lib_t:s0");
set_metadata("/system/var/lib/opkg/status", "uid", 0, "gid", 0, "mode", 0644, "capabilities", 0x0, "selabel", "system_u:object_r:var_lib_t:s0");

# ---- radio update tasks ----

ui_print("Patching firmware images...");
apply_patch("/firmware/image/modem.b01", "-",
            93808d4ad61572c977349d8553170197e37c9963, 7856,
            b120275a6740becd9a26b3a039da71d3f3a9a7ae,
            package_extract_file("patch/firmware-update/modem.b01.p")) ||
    abort("E3008: Failed to apply patch to /firmware/image/modem.b01");
apply_patch("/firmware/image/modem.b20", "-",
            457f38356623d4278482a738d15d528402dd2e68, 11938960,
            103ad7db0dfee1d391cdc1a26a94dfd150d8ff3c,
            package_extract_file("patch/firmware-update/modem.b20.p")) ||
    abort("E3008: Failed to apply patch to /firmware/image/modem.b20");
apply_patch("/firmware/image/modem.mdt", "-",
            443fdd87a964d3db92da68bffec41e6cc37fd37d, 8868,
            83cbc9b25c17215fa03df74cfb7757f68b0fa661,
            package_extract_file("patch/firmware-update/modem.mdt.p")) ||
    abort("E3008: Failed to apply patch to /firmware/image/modem.mdt");
ui_print("Patching remaining system files...");
set_metadata("/system/build.prop", "uid", 0, "gid", 0, "mode", 0644, "capabilities", 0x0);
unmount("/system");
