###
#
# AROMA Installer
#   (C) 2011-2012 by Ahmad Amarullah
#       amarullz - xda-developers
#
###
#
# AROMA Installer Script for NexusHD2 ROM
#   (C) 2012-2014 by tytung - xda-developers (tytung @ gmail.com)
#   ( modded by macs18max added functionality to flash NativeSD boot image in cLK )
#
###

ui_print("------------------------------------------------");
ui_print("       KitKat-CM-11.0 ROM       ");
ui_print("------------------------------------------------");

################
# NAND Install #
################

if file_getprop("/tmp/aroma/installtype.prop", "selected.1") == "1" then

  ui_print("@  Installing to NAND...");
  ui_print("@  Mounting NAND partitions...");

  format("yaffs2", "MTD", "boot");
  format("yaffs2", "MTD", "system");
  mount("yaffs2", "MTD", "system", "/system");
  mount("yaffs2", "MTD", "userdata", "/data");

  # Wipe Dalvik-Cache
  if file_getprop("/tmp/aroma/mods.prop", "selected.2") == "1" then
    ui_print("@  Wiping Dalvik-Cache...");
    delete_recursive("/data/dalvik-cache");
    ui_print("  Dalvik-Cache wiped.");
  endif;

  # Wipe Data/Factory Reset
  if file_getprop("/tmp/aroma/mods.prop", "selected.2") == "2" then
    ui_print("@  Wiping Data/Factory Reset...");
    unmount("/data");
    format("yaffs2", "MTD", "userdata");
    mount("yaffs2", "MTD", "userdata", "/data");
    ui_print("  Data wiped.");
  endif;

  show_progress(0.9, 1400);
  ui_print("@  Copying files...");
  package_extract_dir("system", "/system");
  show_progress(0.1, 5);

endif;

#####################
# DataOnEXT Install #
#####################

if file_getprop("/tmp/aroma/installtype.prop", "selected.1") == "2" then

  ui_print("@  Installing to NAND and SD-EXT...");
  ui_print("@  Mounting NAND and SD-EXT partitions...");

  package_extract_file("boot/mount_DataOnEXT.sh", "/tmp/mount_DataOnEXT.sh");
  set_perm(0, 0, 0777, "/tmp/mount_DataOnEXT.sh");
  run_program("/tmp/mount_DataOnEXT.sh");

  if file_getprop("/tmp/nfo.prop", "DataOnEXT") == "true" then

    format("yaffs2", "MTD", "boot");
    format("yaffs2", "MTD", "system");
    mount("yaffs2", "MTD", "system", "/system");

    # Wipe Dalvik-Cache
    if file_getprop("/tmp/aroma/mods.prop", "selected.2") == "1" then
      ui_print("@  Wiping Dalvik-Cache...");
      delete_recursive("/data/dalvik-cache");
      ui_print("  Dalvik-Cache wiped.");
    endif;

    # Wipe Data/Factory Reset
    if file_getprop("/tmp/aroma/mods.prop", "selected.2") == "2" then
      ui_print("@  Wiping Data/Factory Reset...");
      delete_recursive("/data");
      ui_print("  Data wiped.");
    endif;

    show_progress(0.9, 1400);
    ui_print("@  Copying files...");
    package_extract_dir("system", "/system");
    show_progress(0.1, 5);

    # Append " [DataOnEXT]" to the ROM name
    package_extract_file("mods/set_build_prop_DataOnEXT.sh", "/tmp/set_build_prop_DataOnEXT.sh");
    set_perm(0, 0, 0777, "/tmp/set_build_prop_DataOnEXT.sh");
    run_program("/tmp/set_build_prop_DataOnEXT.sh");

  endif;

endif;

#####################################
# NAND MAGLDR or cLK Kernel install #
#####################################

if file_getprop("/tmp/aroma/kernel.prop", "NAND") == "yes" then

  # check bootloader
  package_extract_file("boot/checksys.sh", "/tmp/checksys.sh");
  set_perm(0, 0, 755, "/tmp/checksys.sh");
  run_program("/tmp/checksys.sh");

  if file_getprop("/tmp/nfo.prop", "bootloader") == "cLK" then

    ui_print("  cLK Bootloader found.");
    ui_print("  Flashing cLK kernel...");

    # initrd.gz for NAND boot
    if file_getprop("/tmp/aroma/installtype.prop", "selected.1") == "1" then
      package_extract_file("boot/boot_CLK_NAND/initrd.gz", "/tmp/initrd.gz");
    endif;

    # initrd.gz for NAND with DataOnEXT boot
    if file_getprop("/tmp/aroma/installtype.prop", "selected.1") == "2" then
      package_extract_file("boot/boot_CLK_NAND_with_DataOnEXT/initrd.gz", "/tmp/initrd.gz");
    endif;

    assert(
      package_extract_file("boot/zImage", "/tmp/zImage"),
      package_extract_file("boot/mkbootimg", "/tmp/mkbootimg"),
      package_extract_file("boot/mkbootimg.sh", "/tmp/mkbootimg.sh"));
    set_perm(0, 0, 0755, "/tmp/mkbootimg");
    set_perm(0, 0, 0755, "/tmp/mkbootimg.sh");
    assert(
      run_program("/tmp/mkbootimg.sh"),
      write_raw_image("/tmp/boot.img", "boot"),
      delete("/tmp/boot.img"));

  else

    ui_print("  MAGLDR Bootloader found.");
    ui_print("  Flashing MAGLDR kernel...");

    mount("yaffs2", "MTD", "boot", "/boot");

    # initrd.gz for NAND boot
    if file_getprop("/tmp/aroma/installtype.prop", "selected.1") == "1" then
      package_extract_file("boot/boot_MAGLDR_NAND/initrd.gz", "/boot/initrd.gz");
    endif;

    # initrd.gz for NAND with DataOnEXT boot
    if file_getprop("/tmp/aroma/installtype.prop", "selected.1") == "2" then
      package_extract_file("boot/boot_MAGLDR_NAND_with_DataOnEXT/initrd.gz", "/boot/initrd.gz");
    endif;

    package_extract_file("boot/zImage", "/boot/zImage");

  endif;

endif;

####################
# NativeSD Install #
####################

if file_getprop("/tmp/aroma/installtype.prop", "selected.1") == "3" then

  ui_print("@  Installing to SD-EXT (NativeSD)...");
  ui_print("@  Checking & Mounting SD-EXT Partition...");

  package_extract_file("boot/mount_NativeSD.sh", "/tmp/mount_NativeSD.sh");
  set_perm(0, 0, 0777, "/tmp/mount_NativeSD.sh");
  run_program("/tmp/mount_NativeSD.sh");

  if file_getprop("/tmp/nfo.prop", "NativeSD") == "true" then

    # Wipe Dalvik-Cache
    if file_getprop("/tmp/aroma/mods.prop", "selected.2") == "1" then
      ui_print("@  Wiping Dalvik-Cache...");
      delete_recursive("/data/dalvik-cache");
      ui_print("  Dalvik-Cache wiped.");
    endif;

    # Wipe Data/Factory Reset
    if file_getprop("/tmp/aroma/mods.prop", "selected.2") == "2" then
      ui_print("@  Wiping Data/Factory Reset...");
      delete_recursive("/data");
      ui_print("  Data wiped.");
    endif;

    show_progress(0.9, 1400);
    ui_print("@  Copying files...");

    # check bootloader
    package_extract_file("boot/checksys.sh", "/tmp/checksys.sh");
    set_perm(0, 0, 755, "/tmp/checksys.sh");
    run_program("/tmp/checksys.sh");

    if file_getprop("/tmp/nfo.prop", "bootloader") == "cLK" then
      package_extract_file("boot/boot_CLK_NativeSD/initrd.gz", "/boot/initrd.gz");
      package_extract_file("boot/boot_CLK_NativeSD/initrd.gz", "/boot_dir/initrd.gz");
    else
      package_extract_file("boot/boot_MAGLDR_NativeSD/initrd.gz", "/boot/initrd.gz");
      package_extract_file("boot/boot_MAGLDR_NativeSD/initrd.gz", "/boot_dir/initrd.gz");
    endif;

    package_extract_file("boot/zImage", "/boot/zImage");
    package_extract_file("boot/zImage", "/boot_dir/zImage");
    package_extract_dir("sdcard", "/sdcard");
    package_extract_dir("system", "/system");
    show_progress(0.1, 5);

    # Append " [NativeSD]" to the ROM name
    package_extract_file("mods/set_build_prop_NativeSD.sh", "/tmp/set_build_prop_NativeSD.sh");
    set_perm(0, 0, 0777, "/tmp/set_build_prop_NativeSD.sh");
    run_program("/tmp/set_build_prop_NativeSD.sh");


  endif;

endif;

######################
# Softkeys Selection #
######################

ui_print("@  Setting Softkeys...");
if file_getprop("/tmp/aroma/mods.prop", "selected.1") == "1" then

  package_extract_file("mods/enable_softkeys.sh", "/tmp/enable_softkeys.sh");
  set_perm(0, 0, 755, "/tmp/enable_softkeys.sh");
  run_program("/tmp/enable_softkeys.sh");
  ui_print("  Softkeys enabled.");

endif;

if file_getprop("/tmp/aroma/mods.prop", "selected.1") == "2" then

  package_extract_file("mods/disable_softkeys.sh", "/tmp/disable_softkeys.sh");
  set_perm(0, 0, 755, "/tmp/disable_softkeys.sh");
  run_program("/tmp/disable_softkeys.sh");
  ui_print("  Softkeys disabled.");

endif;

################################
# ROM Symlinks and Permissions #
################################

ui_print("@  Setting symlinks and permissions...");
ui_print("  Setting symlinks...");
symlink("../bin/fsck.f2fs", "/system/bin/dump.f2fs");
symlink("../xbin/su", "/system/bin/su");
symlink("Roboto-Bold.ttf", "/system/fonts/DroidSans-Bold.ttf");
symlink("Roboto-Regular.ttf", "/system/fonts/DroidSans.ttf");
symlink("busybox", "/system/xbin/[", "/system/xbin/[[",
        "/system/xbin/adjtimex", "/system/xbin/arp", "/system/xbin/ash",
        "/system/xbin/awk", "/system/xbin/base64", "/system/xbin/basename",
        "/system/xbin/bbconfig", "/system/xbin/blkid", "/system/xbin/blockdev",
        "/system/xbin/brctl", "/system/xbin/bunzip2", "/system/xbin/bzcat",
        "/system/xbin/bzip2", "/system/xbin/cal", "/system/xbin/cat",
        "/system/xbin/catv", "/system/xbin/chattr", "/system/xbin/chcon",
        "/system/xbin/chgrp", "/system/xbin/chmod", "/system/xbin/chown",
        "/system/xbin/chroot", "/system/xbin/chvt", "/system/xbin/clear",
        "/system/xbin/cmp", "/system/xbin/comm", "/system/xbin/cp",
        "/system/xbin/cpio", "/system/xbin/crond", "/system/xbin/crontab",
        "/system/xbin/cut", "/system/xbin/date", "/system/xbin/dc",
        "/system/xbin/dd", "/system/xbin/deallocvt", "/system/xbin/depmod",
        "/system/xbin/devmem", "/system/xbin/df", "/system/xbin/diff",
        "/system/xbin/dirname", "/system/xbin/dmesg", "/system/xbin/dnsd",
        "/system/xbin/dos2unix", "/system/xbin/du", "/system/xbin/echo",
        "/system/xbin/ed", "/system/xbin/egrep", "/system/xbin/env",
        "/system/xbin/expand", "/system/xbin/expr", "/system/xbin/false",
        "/system/xbin/fbsplash", "/system/xbin/fdisk", "/system/xbin/fgconsole",
        "/system/xbin/fgrep", "/system/xbin/find", "/system/xbin/findfs",
        "/system/xbin/flash_lock", "/system/xbin/flash_unlock",
        "/system/xbin/flashcp", "/system/xbin/flock", "/system/xbin/fold",
        "/system/xbin/free", "/system/xbin/freeramdisk", "/system/xbin/fstrim",
        "/system/xbin/fsync", "/system/xbin/ftpget", "/system/xbin/ftpput",
        "/system/xbin/fuser", "/system/xbin/getenforce", "/system/xbin/getopt",
        "/system/xbin/getsebool", "/system/xbin/grep", "/system/xbin/groups",
        "/system/xbin/gunzip", "/system/xbin/gzip", "/system/xbin/halt",
        "/system/xbin/head", "/system/xbin/hexdump", "/system/xbin/hwclock",
        "/system/xbin/id", "/system/xbin/ifconfig", "/system/xbin/inetd",
        "/system/xbin/insmod", "/system/xbin/install", "/system/xbin/ionice",
        "/system/xbin/iostat", "/system/xbin/ip", "/system/xbin/kill",
        "/system/xbin/killall", "/system/xbin/killall5", "/system/xbin/less",
        "/system/xbin/ln", "/system/xbin/losetup", "/system/xbin/ls",
        "/system/xbin/lsattr", "/system/xbin/lsmod", "/system/xbin/lspci",
        "/system/xbin/lsusb", "/system/xbin/lzcat", "/system/xbin/lzma",
        "/system/xbin/lzop", "/system/xbin/lzopcat", "/system/xbin/man",
        "/system/xbin/matchpathcon", "/system/xbin/md5sum", "/system/xbin/mesg",
        "/system/xbin/mkdir", "/system/xbin/mke2fs", "/system/xbin/mkfifo",
        "/system/xbin/mkfs.ext2", "/system/xbin/mkfs.vfat",
        "/system/xbin/mknod", "/system/xbin/mkswap", "/system/xbin/mktemp",
        "/system/xbin/modinfo", "/system/xbin/modprobe", "/system/xbin/more",
        "/system/xbin/mount", "/system/xbin/mountpoint", "/system/xbin/mpstat",
        "/system/xbin/mv", "/system/xbin/nanddump", "/system/xbin/nandwrite",
        "/system/xbin/nbd-client", "/system/xbin/netstat", "/system/xbin/nice",
        "/system/xbin/nmeter", "/system/xbin/nohup", "/system/xbin/nslookup",
        "/system/xbin/ntpd", "/system/xbin/od", "/system/xbin/openvt",
        "/system/xbin/patch", "/system/xbin/pgrep", "/system/xbin/pidof",
        "/system/xbin/ping", "/system/xbin/pipe_progress", "/system/xbin/pkill",
        "/system/xbin/pmap", "/system/xbin/poweroff", "/system/xbin/printenv",
        "/system/xbin/printf", "/system/xbin/ps", "/system/xbin/pstree",
        "/system/xbin/pwd", "/system/xbin/pwdx", "/system/xbin/rdate",
        "/system/xbin/rdev", "/system/xbin/readlink", "/system/xbin/realpath",
        "/system/xbin/renice", "/system/xbin/reset", "/system/xbin/resize",
        "/system/xbin/restorecon", "/system/xbin/rev", "/system/xbin/rm",
        "/system/xbin/rmdir", "/system/xbin/rmmod", "/system/xbin/route",
        "/system/xbin/run-parts", "/system/xbin/runcon", "/system/xbin/rx",
        "/system/xbin/sed", "/system/xbin/selinuxenabled", "/system/xbin/seq",
        "/system/xbin/sestatus", "/system/xbin/setconsole",
        "/system/xbin/setenforce", "/system/xbin/setfiles",
        "/system/xbin/setkeycodes", "/system/xbin/setsebool",
        "/system/xbin/setserial", "/system/xbin/setsid", "/system/xbin/sh",
        "/system/xbin/sha1sum", "/system/xbin/sha256sum",
        "/system/xbin/sha3sum", "/system/xbin/sha512sum", "/system/xbin/sleep",
        "/system/xbin/smemcap", "/system/xbin/sort", "/system/xbin/split",
        "/system/xbin/stat", "/system/xbin/strings", "/system/xbin/stty",
        "/system/xbin/sum", "/system/xbin/swapoff", "/system/xbin/swapon",
        "/system/xbin/sync", "/system/xbin/sysctl", "/system/xbin/tac",
        "/system/xbin/tail", "/system/xbin/tar", "/system/xbin/taskset",
        "/system/xbin/tee", "/system/xbin/telnet", "/system/xbin/telnetd",
        "/system/xbin/test", "/system/xbin/tftp", "/system/xbin/tftpd",
        "/system/xbin/time", "/system/xbin/timeout", "/system/xbin/top",
        "/system/xbin/touch", "/system/xbin/tr", "/system/xbin/traceroute",
        "/system/xbin/true", "/system/xbin/ttysize", "/system/xbin/tune2fs",
        "/system/xbin/umount", "/system/xbin/uname", "/system/xbin/uncompress",
        "/system/xbin/unexpand", "/system/xbin/uniq", "/system/xbin/unix2dos",
        "/system/xbin/unlzma", "/system/xbin/unlzop", "/system/xbin/unxz",
        "/system/xbin/unzip", "/system/xbin/uptime", "/system/xbin/usleep",
        "/system/xbin/uudecode", "/system/xbin/uuencode", "/system/xbin/vi",
        "/system/xbin/watch", "/system/xbin/wc", "/system/xbin/wget",
        "/system/xbin/which", "/system/xbin/whoami", "/system/xbin/xargs",
        "/system/xbin/xz", "/system/xbin/xzcat", "/system/xbin/yes",
        "/system/xbin/zcat");
symlink("libGLESv2.so", "/system/lib/libGLESv3.so");
symlink("logcat", "/system/bin/lolcat");
symlink("mksh", "/system/bin/sh");
symlink("mount.exfat", "/system/bin/fsck.exfat",
        "/system/bin/mkfs.exfat");
symlink("pigz", "/system/xbin/unpigz");
symlink("toolbox", "/system/bin/cat", "/system/bin/chcon",
        "/system/bin/chmod", "/system/bin/chown", "/system/bin/clear",
        "/system/bin/cmp", "/system/bin/cp", "/system/bin/date",
        "/system/bin/dd", "/system/bin/df", "/system/bin/dmesg",
        "/system/bin/du", "/system/bin/getenforce", "/system/bin/getevent",
        "/system/bin/getprop", "/system/bin/getsebool", "/system/bin/grep",
        "/system/bin/hd", "/system/bin/id", "/system/bin/ifconfig",
        "/system/bin/iftop", "/system/bin/insmod", "/system/bin/ioctl",
        "/system/bin/ionice", "/system/bin/kill", "/system/bin/ln",
        "/system/bin/load_policy", "/system/bin/log", "/system/bin/ls",
        "/system/bin/lsmod", "/system/bin/lsof", "/system/bin/md5",
        "/system/bin/mkdir", "/system/bin/mkswap", "/system/bin/mount",
        "/system/bin/mv", "/system/bin/nandread", "/system/bin/netstat",
        "/system/bin/newfs_msdos", "/system/bin/notify", "/system/bin/printenv",
        "/system/bin/ps", "/system/bin/readlink", "/system/bin/renice",
        "/system/bin/restart", "/system/bin/restorecon", "/system/bin/rm",
        "/system/bin/rmdir", "/system/bin/rmmod", "/system/bin/route",
        "/system/bin/runcon", "/system/bin/schedtop", "/system/bin/sendevent",
        "/system/bin/setconsole", "/system/bin/setenforce",
        "/system/bin/setfattr", "/system/bin/setprop", "/system/bin/setsebool",
        "/system/bin/sleep", "/system/bin/smd", "/system/bin/start",
        "/system/bin/stop", "/system/bin/swapoff", "/system/bin/swapon",
        "/system/bin/sync", "/system/bin/top", "/system/bin/touch",
        "/system/bin/umount", "/system/bin/uptime", "/system/bin/vmstat",
        "/system/bin/watchprops",
        "/system/bin/wipe");
ui_print("  Setting permissions...");
set_perm_recursive(0, 0, 0755, 0644, "/system");
set_perm_recursive(0, 2000, 0755, 0755, "/system/bin");
set_perm(0, 3003, 02750, "/system/bin/netcfg");
set_perm(0, 3004, 02755, "/system/bin/ping");
set_perm(0, 2000, 06750, "/system/bin/run-as");
set_perm(1014, 2000, 0550, "/system/etc/dhcpcd/dhcpcd-run-hooks");
set_perm_recursive(0, 2000, 0755, 0644, "/system/vendor");
set_perm(0, 2000, 0755, "/system/vendor/lib");
set_perm(0, 0, 0644, "/system/vendor/lib/libbt-vendor.so");
set_perm_recursive(0, 2000, 0755, 0755, "/system/xbin");
set_perm(0, 0, 06755, "/system/xbin/tcpdump");
set_perm(0, 0, 06755, "/system/xbin/busybox");
set_perm(0, 0, 06755, "/system/xbin/su");
set_perm_recursive(0, 2000, 0755, 0750, "/system/etc/init.d");
set_perm(0, 0, 0755, "/system/etc/init.d");
set_perm_recursive(0, 1001, 0755, 0775, "/system/etc/ppp");
set_perm(0, 0, 04755, "/system/bin/pppd");
set_perm_recursive(1000, 1000, 0771, 0644, "/data/app");
ui_print("  Symlinks and permissions done.");

###############
# Cleaning up #
###############

set_progress(0.95);
ui_print("@  Cleaning up...");
delete("/tmp");
ui_print("  Unmounting...");
unmount("/boot");
unmount("/system");
unmount("/data");
unmount("/boot_dir");
ui_print("  Done.");

set_progress(1.0);
ui_print("@  Installation Complete");
ui_print("  Please reboot.");
