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", "ba5b68432f514b50a27906f22dc25d064262eab1", "060f95df0d0feb8ba8314a4ba54dafde468244b0") || abort("E3005: \"/system/usr/bin/atfwd_daemon\" has unexpected contents.");
apply_patch_check("/system/var/lib/opkg/status", "f49d8bb72e50bf7d4fd9d8898fcee969dc9b9814", "52780052fde3ec8095cd75b457cec3a15263e408") || abort("E3005: \"/system/var/lib/opkg/status\" has unexpected contents.");
apply_patch_space(102700) || 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", "818c7193ce72c4e2457126cf64b7806f1404951c", "c520d49525412b44a4cb0ab537e28c76c28d192d") || abort("E3005: \"/firmware/image/modem.b01\" has unexpected contents.");
apply_patch_check("/firmware/image/modem.b21", "c0fa1e006cde72b6f384e9ececce633fb4cb30f8", "0e1c6943d4202382000bfada3d234e3e3b743e2c") || abort("E3005: \"/firmware/image/modem.b21\" has unexpected contents.");
apply_patch_check("/firmware/image/modem.b20", "4d889ae1f0679f57e8ce545c7995a1f9cde44a4f", "ee55a92e1ab8ce2a9c145eb02f3d637791c976f6") || abort("E3005: \"/firmware/image/modem.b20\" has unexpected contents.");
apply_patch_check("/firmware/image/modem.mdt", "75396dc1c3afc7c6f1a2bf87caf0d26b0d5f6867", "fb41c3120b9aaca6378d13630e84df422d3f5ad7") || abort("E3005: \"/firmware/image/modem.mdt\" has unexpected contents.");
apply_patch_check("/firmware/image/modem.b24", "8e23e816684fe75d677a0f30ea3ed86294d2cdef", "796836315c20628f9910ebc1cc48b76c3984d355") || abort("E3005: \"/firmware/image/modem.b24\" 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", "-",
            ba5b68432f514b50a27906f22dc25d064262eab1, 102700,
            060f95df0d0feb8ba8314a4ba54dafde468244b0,
            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.516467);
apply_patch("/system/var/lib/opkg/status", "-",
            f49d8bb72e50bf7d4fd9d8898fcee969dc9b9814, 96150,
            52780052fde3ec8095cd75b457cec3a15263e408,
            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", "-",
            818c7193ce72c4e2457126cf64b7806f1404951c, 7856,
            c520d49525412b44a4cb0ab537e28c76c28d192d,
            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.b21", "-",
            c0fa1e006cde72b6f384e9ececce633fb4cb30f8, 6020762,
            0e1c6943d4202382000bfada3d234e3e3b743e2c,
            package_extract_file("patch/firmware-update/modem.b21.p")) ||
    abort("E3008: Failed to apply patch to /firmware/image/modem.b21");
apply_patch("/firmware/image/modem.b20", "-",
            4d889ae1f0679f57e8ce545c7995a1f9cde44a4f, 12311808,
            ee55a92e1ab8ce2a9c145eb02f3d637791c976f6,
            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", "-",
            75396dc1c3afc7c6f1a2bf87caf0d26b0d5f6867, 8868,
            fb41c3120b9aaca6378d13630e84df422d3f5ad7,
            package_extract_file("patch/firmware-update/modem.mdt.p")) ||
    abort("E3008: Failed to apply patch to /firmware/image/modem.mdt");
apply_patch("/firmware/image/modem.b24", "-",
            8e23e816684fe75d677a0f30ea3ed86294d2cdef, 41017408,
            796836315c20628f9910ebc1cc48b76c3984d355,
            package_extract_file("patch/firmware-update/modem.b24.p")) ||
    abort("E3008: Failed to apply patch to /firmware/image/modem.b24");
ui_print("Patching remaining system files...");
set_metadata("/system/build.prop", "uid", 0, "gid", 0, "mode", 0644, "capabilities", 0x0);
unmount("/system");
