getprop("ro.product.device") == "sagit" || abort("E3004: This package is for \"sagit\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Xiaomi/sagit/sagit:8.0.0/OPR1.170623.027/9.3.5:user/release-keys");
ui_print("Target: Xiaomi/sagit/sagit:8.0.0/OPR1.170623.027/9.3.11:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Xiaomi/sagit/sagit:8.0.0/OPR1.170623.027/9.3.5:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Xiaomi/sagit/sagit:8.0.0/OPR1.170623.027/9.3.11:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Xiaomi/sagit/sagit:8.0.0/OPR1.170623.027/9.3.5:user/release-keys or Xiaomi/sagit/sagit:8.0.0/OPR1.170623.027/9.3.11:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
show_progress(0.100000, 5);
apply_patch_check("EMMC:/dev/block/bootdevice/by-name/boot:19166538:40624f91427189e34c4cec824183c9693af7e8ff:19170634:d90a2ee4b25469649eb3ab62c59ecd6a95889a9d") || abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:19166538:40624f91427189e34c4cec824183c9693af7e8ff:19170634:d90a2ee4b25469649eb3ab62c59ecd6a95889a9d\" has unexpected contents.");
apply_patch_space(42016768) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1("/dev/block/bootdevice/by-name/system", "234,1,320,321,825,828,829,20988,20993,20996,21000,21166,21167,21171,21175,21185,21186,21188,21192,21202,21203,21207,21211,21230,21231,21236,21240,21248,21249,21274,21280,22344,22345,22347,22351,22360,22361,22366,22370,22374,23798,23802,23808,23843,24680,24693,24697,24716,24717,24720,24724,24734,24735,24738,24742,24754,24755,24760,24764,24780,24781,24915,24921,25409,25410,25546,25550,25632,26291,26294,26298,26308,26309,26312,26316,26325,26326,26837,26841,27036,27037,27039,32767,32768,32770,33089,33090,65290,65293,65366,65535,65536,65537,66043,98302,98304,98306,98625,98626,109952,131071,131072,131073,131579,131585,163840,163842,164161,164162,164668,196607,196608,196609,197115,197121,229376,229378,229697,229698,230204,262143,262144,262145,262651,294911,294912,294914,295233,295234,323695,327679,327680,327681,328187,360447,360448,360449,360955,393215,393216,393217,393723,425983,425984,425985,426491,458751,458752,458753,459259,491519,491520,491521,492027,524287,524288,524289,524795,557055,557056,557057,557563,589823,589824,589825,590331,622591,622592,622593,623099,655358,655360,655361,655867,688126,688128,688129,688635,720894,720896,720897,721403,753662,753664,753665,754171,786430,786432,786433,786939,796504,819200,819202,819521,819522,821400,851966,851968,851969,884736,884738,885057,885058,885564,917502,917504,917505,950272,950273,983040,983041,1015808,1015809,1048576,1048577,1081344,1081345,1114112,1114113,1146880,1146881,1179648,1179649,1212416,1212417,1245184,1245185,1277952,1277953,1280377,1290267,1290268,1300430,1300437,1310720") == "4b71abfb7009efd02bf8e5d36d18c4be520c6c39" || block_image_verify("/dev/block/bootdevice/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat")) then
ui_print("Verified system image...");
else
check_first_block("/dev/block/bootdevice/by-name/system");
ifelse (block_image_recover("/dev/block/bootdevice/by-name/system", "234,1,320,321,825,828,829,20988,20993,20996,21000,21166,21167,21171,21175,21185,21186,21188,21192,21202,21203,21207,21211,21230,21231,21236,21240,21248,21249,21274,21280,22344,22345,22347,22351,22360,22361,22366,22370,22374,23798,23802,23808,23843,24680,24693,24697,24716,24717,24720,24724,24734,24735,24738,24742,24754,24755,24760,24764,24780,24781,24915,24921,25409,25410,25546,25550,25632,26291,26294,26298,26308,26309,26312,26316,26325,26326,26837,26841,27036,27037,27039,32767,32768,32770,33089,33090,65290,65293,65366,65535,65536,65537,66043,98302,98304,98306,98625,98626,109952,131071,131072,131073,131579,131585,163840,163842,164161,164162,164668,196607,196608,196609,197115,197121,229376,229378,229697,229698,230204,262143,262144,262145,262651,294911,294912,294914,295233,295234,323695,327679,327680,327681,328187,360447,360448,360449,360955,393215,393216,393217,393723,425983,425984,425985,426491,458751,458752,458753,459259,491519,491520,491521,492027,524287,524288,524289,524795,557055,557056,557057,557563,589823,589824,589825,590331,622591,622592,622593,623099,655358,655360,655361,655867,688126,688128,688129,688635,720894,720896,720897,721403,753662,753664,753665,754171,786430,786432,786433,786939,796504,819200,819202,819521,819522,821400,851966,851968,851969,884736,884738,885057,885058,885564,917502,917504,917505,950272,950273,983040,983041,1015808,1015809,1048576,1048577,1081344,1081345,1114112,1114113,1146880,1146881,1179648,1179649,1212416,1212417,1245184,1245185,1277952,1277953,1280377,1290267,1290268,1300430,1300437,1310720") && block_image_verify("/dev/block/bootdevice/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat"), ui_print("system recovered successfully."), abort("E1004: system partition fails to recover"));
endif;

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

ui_print("Patching boot image...");
show_progress(0.100000, 10);
apply_patch("EMMC:/dev/block/bootdevice/by-name/boot:19166538:40624f91427189e34c4cec824183c9693af7e8ff:19170634:d90a2ee4b25469649eb3ab62c59ecd6a95889a9d",
            "-", d90a2ee4b25469649eb3ab62c59ecd6a95889a9d, 19170634,
            40624f91427189e34c4cec824183c9693af7e8ff, package_extract_file("patch/boot.img.p")) || abort("Failed to apply patch \"patch/boot.img.p \".");

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

ui_print("Patching firmware images...");
show_progress(0.600000, 600);
ui_print("Patching system image after verification.");
block_image_update("/dev/block/bootdevice/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat") ||
  abort("E1001: Failed to update system image.");
show_progress(0.100000, 2);
