#updater-script by show-p1984

set_progress(0.01);

ui_print("-Checking Model ID");
assert(getprop("ro.product.device") == "hammerhead" || getprop("ro.build.product") == "hammerhead" || getprop("ro.product.board") == "hammerhead");
ui_print("-Success!");
ui_print("-Model ID - Nexus 5 (hammerhead)");
ui_print("-Ok to proceed");

ifelse(is_mounted("/system") == "/system", unmount("/system"));
ifelse(is_mounted("/cache") == "/cache", unmount("/cache"));

ui_print("  ");
ui_print("Welcome to hells-Core by hells");
ui_print("  ");
set_progress(0.01);

################EXTRACT SETUP FILES
ui_print("Extracting files ...");
package_extract_dir("kernel", "/tmp");
set_perm(0, 0, 0777, "/tmp/compatibility.sh");
set_perm(0, 0, 0777, "/tmp/edit_ramdisk.sh");
set_perm(0, 0, 0777, "/tmp/mkbootimg.sh");
set_perm(0, 0, 0777, "/tmp/mkbootimg");
set_perm(0, 0, 0777, "/tmp/unpackbootimg");
set_perm(0, 0, 0777, "/tmp/busybox");
set_progress(0.1);

################MOUNT SYSTEM
ui_print("Mount /system...");
mount("ext4", "EMMC", "/dev/block/platform/msm_sdcc.1/by-name/system", "/system");
set_progress(0.2);

################COPY SCRIPTS
ui_print("Copying Scripts...");
run_program("/sbin/busybox", "mount", "/system");
package_extract_dir("system", "/system");
set_perm_recursive(0, 2000, 0755, 0755, "/system/etc/init.d");
set_progress(0.4);

################KERNEL PULL/EXTRACT
ui_print("Pull the boot.img from your phone...");
run_program("/sbin/busybox", "dd", "if=/dev/block/platform/msm_sdcc.1/by-name/boot", "of=/tmp/boot.img");
set_progress(0.5);

ui_print("Extract boot.img...");
run_program("/tmp/unpackbootimg", "-i", "/tmp/boot.img", "-o", "/tmp/");
set_progress(0.55);

################CMDLINE SETTINGS
#ui_print("Applying cmdline settings...");
#run_program("/tmp/cmdline.sh");
#set_progress(0.6);

################RAMDISK EDITS
ui_print("Fixing your ramdisk...");
ui_print("  removing governor overrides...");
ui_print("  removing min_freq overrides...");
ui_print("  Looking for busybox...");
ui_print("  adding init.d support (if not already supported)...");
ui_print("  adding better ondemand settings to the ramdisk...");
run_program("/tmp/edit_ramdisk.sh");
set_progress(0.65);

################RENAME thermald, mpdecision & power.msm8974.so
ui_print("Renaming files...");
ui_print("  /system/bin/thermal-engine-hh -> thermal-engine-hh_bck");
ui_print("  /system/bin/mpdecision -> mpdecision_bck");
ui_print("  /system/lib/hw/power.msm8974.so -> power.msm8974.so_bck");
run_program("/tmp/compatibility.sh");

################UNMOUNT SYSTEM
unmount("/system");
set_progress(0.8);

################REPACK
ui_print("Repack boot.img with hells-Core &");
#ui_print("  changing the cmdline of the kernel...");
ui_print("Installing kernel...");
run_program("/tmp/mkbootimg.sh");
set_progress(0.9);

################KERNEL FLASH
ui_print("Flashing New boot.img...");
run_program("/sbin/busybox", "dd", "if=/tmp/newboot.img", "of=/dev/block/platform/msm_sdcc.1/by-name/boot");
set_progress(0.95);

################WIPE CACHE
ui_print("Wiping /cache");
run_program("/sbin/busybox", "mount", "/cache");
delete_recursive("/cache/");
run_program("/sbin/busybox", "umount", "/cache");
set_progress(1.0);

ui_print("######################");
ui_print("#      Kernel flashed, enjoy!");
ui_print("######################");

