assert(getprop("ro.product.device") == "foster" || getprop("ro.build.product") == "foster" || 
       getprop("ro.product.device") == "darcy" || getprop("ro.build.product") == "darcy" || 
       getprop("ro.product.device") == "jetson" || getprop("ro.build.product") == "jetson" || 
       getprop("ro.product.device") == "loki" || getprop("ro.build.product") == "loki" || 
       getprop("ro.product.device") == "mdarcy" || getprop("ro.build.product") == "mdarcy" || 
       getprop("ro.product.device") == "nx" || getprop("ro.build.product") == "nx" || abort("E3004: This package is for device: foster,darcy,jetson,loki,mdarcy,nx; this device is " + getprop("ro.product.device") + "."););

ifelse(
  read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/odm_production_mode") == "0x00000001
",
  (


    ifelse(
      getprop("ro.hardware") == "foster_e" || getprop("ro.hardware") == "foster_e_hdd",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-ffbbebe4",
          (
            ui_print("Correct bootloader already installed for fused " + getprop(ro.hardware));
          ),
          ifelse(
            read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") == "0x0256ff42f60a5159edad8029772f1d5f848477fb9d33cd2b2e8f0b4d85c8d78a
",
            (
              ui_print("Flashing updated bootloader for fused " + getprop(ro.hardware));
              package_extract_file("firmware-update/" + getprop(ro.hardware) + ".blob", "/dev/block/by-name/USP");
            ),
            (
              ui_print("Unknown public key " + read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") + " for " + getprop("ro.hardware") + " detected.");
              ui_print("This is not supported. Please report to LineageOS Maintainer.");
              abort();
            )
          )
        );
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "baracus",
      (
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "darcy",
      (
        ifelse(
          is_substring("tegra210b01", read_file("/proc/device-tree/nvidia,dtsfilename")),
          (
            ifelse(
              getprop("ro.bootloader") == "32.00.2019.50-t210-da5c2b1b",
              (
                ui_print("Correct bootloader already installed for fused mdarcy");
              ),
              (
                ifelse(
                  read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") == "0xc5ae4221f0f4f5113c0271b3519cac7f0bcb0cb860381a4648e9eee350e97f89
",
                  (
                    ui_print("Flashing updated bootloader for fused mdarcy");
                    package_extract_file("firmware-update/mdarcy.blob", "/dev/block/by-name/USP");
                  ),
                  (
                    ui_print("Unknown public key " + read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") + " for mdarcy detected.");
                    ui_print("This is not supported. Please report to LineageOS Maintainer.");
                    abort();
                  )
                );
              )
            );
            package_extract_file("install/mdarcy.dtb.img", "/dev/block/by-name/DTB");
            package_extract_file("install/vbmeta_skip.img", "/dev/block/by-name/vbmeta");
          ),
          (
            ifelse(
              getprop("ro.bootloader") == "32.00.2019.50-t210-da5c2b1b",
              (
                ui_print("Correct bootloader already installed for fused darcy");
              ),
              (
                ifelse(
                  read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") == "0x435a807e9187c53eae50e2dcab521e0ea41458a9b18d31db553ceb711c21fb52
",
                  (
                    ui_print("Flashing updated bootloader for fused darcy");
                    package_extract_file("firmware-update/darcy.blob", "/dev/block/by-name/USP");
                  ),
                  (
                    ui_print("Unknown public key " + read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") + " for darcy detected.");
                    ui_print("This is not supported. Please report to LineageOS Maintainer.");
                    abort();
                  )
                );
              )
            );
            package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
          )
        )
      )
    );


    ifelse(
      getprop("ro.hardware") == "loki_e_base" || getprop("ro.hardware") == "loki_e_lte" || getprop("ro.hardware") == "loki_e_wifi",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-ffbbebe4",
          (
            ui_print("Correct bootloader already installed for fused " + getprop(ro.hardware));
          ),
          ifelse(
            read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") == "0x4261f1714a7d71c7e810c9b33769878128cc54ae3c5736646496104b643aed09
",
            (
              ui_print("Fused " + getprop(ro.hardware) + " is not currently supported.");
              ui_print("There has not been a signed bootloader update since rel-24.");
              ui_print("The newer kernel will not boot on the old bootloader.");
              abort();
            ),
            (
              ui_print("Unknown public key " + read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") + " for " + getprop("ro.hardware") + " detected.");
              ui_print("This is not supported. Please report to LineageOS Maintainer.");
              abort();
            )
          )
        );
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "jetson_cv",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-ffbbebe4",
          (
            ui_print("Correct bootloader already installed for fused " + getprop(ro.hardware));
          ),
          (
            ui_print("You fused your Jetson TX1?!? Seriously, why would you do this?");
            ui_print("You are on your own. The LineageOS Maintainer does not want to hear about this.");
            abort();
          )
        );
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "nx",
      (
        ifelse(
          getprop("ro.bootloader") == "2020.04-03755-gf4d532d00d-rev3",
          (
            ui_print("Correct bootloader already installed for fused " + getprop(ro.hardware));
          ),
          ifelse(
            read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") == "0x7e39e100d1135918ceedfe5d66e66496eed21ecb3486d72095cc0b7c60b8bd4f
",
            (
              ui_print("Flashing updated bootloader for fused " + getprop(ro.hardware));
              package_extract_file("firmware-update/nx_bl31.bin", "/external_sd/switchroot/android/bl31.bin");
              package_extract_file("firmware-update/nx_u-boot-dtb.bin", "/external_sd/switchroot/android/'bl33.bin");
            ),
            (
              ui_print("Unknown public key for icosa detected.");
              ui_print("This is not supported. Please report to LineageOS Maintainer.");
              abort();
            )
          )
        );
        package_extract_file("install/nx.dtb.img", "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "sif",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-da5c2b1b",
          (
            ui_print("Correct bootloader already installed for fused sif");
          ),
          ifelse(
            read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") == "0x26646fe375375e39410853f75e59e2c4ca8440926fa37604a280b5c8a25a2c3e
",
            (
              ui_print("Flashing updated bootloader for fused sif");
              package_extract_file("firmware-update/sif.blob", "/dev/block/by-name/USP");
            ),
            (
              ui_print("Unknown public key " + read_file("/sys/devices/7000f800.efuse/7000f800.efuse:efuse-burn/public_key") + " for sif detected.");
              ui_print("This is not supported. Please report to LineageOS Maintainer.");
              abort();
            )
          )
        );
        package_extract_file("install/sif.dtb.img", "/dev/block/by-name/DTB");
        package_extract_file("install/vbmeta_skip.img", "/dev/block/by-name/vbmeta");
      )
    );

  ),
  (

    ifelse(
      getprop("ro.hardware") == "foster_e" || getprop("ro.hardware") == "foster_e_hdd",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-ffbbebe4",
          (
            ui_print("Correct bootloader already installed for unfused " + getprop(ro.hardware));
          ),
          (
            ui_print("This is an unfused foster_e or foster_e_hdd.");
            ui_print("This is not supported. Please report to LineageOS Maintainer.");
            abort();
          )
        );
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "baracus",
      (
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
        ui_print("This is an unfused baracus.");
      )
    );


    ifelse(
      getprop("ro.hardware") == "darcy",
      (
        ifelse(
          is_substring("tegra210b01", read_file("/proc/device-tree/nvidia,dtsfilename")),
          (
            ifelse(
              getprop("ro.bootloader") == "32.00.2019.50-t210-da5c2b1b",
              (
                ui_print("Correct bootloader already installed for unfused mdarcy");
              ),
              (
                ui_print("This is an unfused mdarcy.");
                ui_print("This is not supported. Please report to LineageOS Maintainer.");
                abort();
              )
            );
            package_extract_file("install/mdarcy.dtb.img", "/dev/block/by-name/DTB");
            package_extract_file("install/vbmeta_skip.img", "/dev/block/by-name/vbmeta");
          ),
          (
            ifelse(
              getprop("ro.bootloader") == "32.00.2019.50-t210-da5c2b1b",
              (
                ui_print("Correct bootloader already installed for unfused darcy");
              ),
              (
                ui_print("This is an unfused darcy.");
                ui_print("This is not supported. Please report to LineageOS Maintainer.");
                abort();
              )
            );
            package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
          )
        )
      )
    );


    ifelse(
      getprop("ro.hardware") == "loki_e_base" || getprop("ro.hardware") == "loki_e_lte" || getprop("ro.hardware") == "loki_e_wifi",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-ffbbebe4",
          (
            ui_print("Correct bootloader already installed for unfused " + getprop(ro.hardware));
          ),
          (
            ui_print("This is an unfused " + getprop(ro.hardware) + ".");
            ui_print("Updating the bootloader is not currently supported.");
            ui_print("There has not been a working bootloader since rel-29.");
            abort();
          )
        );
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "jetson_cv",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-ffbbebe4",
          (
            ui_print("Correct bootloader already installed for " + getprop(ro.hardware));
          ),
          (
            ui_print("Flashing updated bootloader for unfused Jetson TX1");
            package_extract_file("firmware-update/jetson_cv.blob", "/dev/block/by-name/USP");
          )
        );
        package_extract_file("install/" + tegra_get_dtbname(), "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "nx",
      (
        ifelse(
          getprop("ro.bootloader") == "2020.04-03755-gf4d532d00d-rev3",
          (
            ui_print("Correct bootloader already installed for unfused " + getprop(ro.hardware));
          ),
          (
            ui_print("This is an unfused nx. Many devlopers would kill for this unit.");
            ui_print("This is not supported. Please report to LineageOS Maintainer.");
            abort();
          )
        );
        package_extract_file("install/nx.dtb.img", "/dev/block/by-name/DTB");
      )
    );


    ifelse(
      getprop("ro.hardware") == "sif",
      (
        ifelse(
          getprop("ro.bootloader") == "32.00.2019.50-t210-da5c2b1b",
          (
            ui_print("Correct bootloader already installed for unfused sif");
          ),
          (
            ui_print("This is an unfused sif.");
            ui_print("This is not supported. Please report to LineageOS Maintainer.");
            abort();
          )
        );
        package_extract_file("install/sif.dtb.img", "/dev/block/by-name/DTB");
        package_extract_file("install/vbmeta_skip.img", "/dev/block/by-name/vbmeta");
      )
    );

  )
);
ui_print("Target: NVIDIA/foster_e/foster:11/RQ1A.210105.003/13961456_3871.0251:user/release-keys");
package_extract_dir("install", "/tmp/install");
set_metadata_recursive("/tmp/install", "uid", 0, "gid", 0, "dmode", 0755, "fmode", 0644);
set_metadata_recursive("/tmp/install/bin", "uid", 0, "gid", 0, "dmode", 0755, "fmode", 0755);
run_program("/tmp/install/bin/backuptool.sh", "backup", "/dev/block/by-name/APP", "ext4");
ui_print("Patching system image unconditionally...");
show_progress(0.800000, 0);
block_image_update("/dev/block/by-name/APP", package_extract_file("system.transfer.list"), "system.new.dat.br", "system.patch.dat") ||
  abort("E1001: Failed to update system image.");
ui_print("Patching vendor image unconditionally...");
show_progress(0.100000, 0);
block_image_update("/dev/block/by-name/vendor", package_extract_file("vendor.transfer.list"), "vendor.new.dat.br", "vendor.patch.dat") ||
  abort("E2001: Failed to update vendor image.");
run_program("/tmp/install/bin/resize2fs_static", "/dev/block/by-name/APP");
run_program("/tmp/install/bin/resize2fs_static", "/dev/block/by-name/vendor");
show_progress(0.020000, 10);
run_program("/tmp/install/bin/backuptool.sh", "restore", "/dev/block/by-name/APP", "ext4");
package_extract_file("boot.img", "/dev/block/by-name/LNX");
show_progress(0.100000, 10);
set_progress(1.000000);
