###
#
# AROMA Installer
#   (C) 2011-2012 by Ahmad Amarullah
#       amarullz - xda-developers
#
###
#
# AROMA Installer Script for NativeSD Touch Recovery
#   (C) 2012 by tytung - xda-developers (tytung @ gmail.com)
#
###

ui_print("--------------------------------------------------");
ui_print("         NativeSD Touch Recovery         ");
ui_print("         tytung - xda-developers         ");
ui_print("--------------------------------------------------");

##########
# Backup #
##########

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

  ui_print("@  Backing up...");
  set_progress(0.1);
  delete("/tmp/backup_date.prop");

  # extract tool_backup.sh
  package_extract_file("scripts/tool_backup.sh", "/tmp/tool_backup.sh");
  set_perm(0, 0, 0777, "/tmp/tool_backup.sh");

  # backup boot
  if file_getprop("/tmp/aroma/backup_type.prop", "item.0.1") == "1" then
    ui_print("  Backing up your boot (zImage and initrd.gz)...");
    run_program("/tmp/tool_backup.sh", "boot");
    set_progress(0.3);
  endif;

  # backup system
  if file_getprop("/tmp/aroma/backup_type.prop", "item.0.2") == "1" then
    ui_print("  Backing up your system...");
    run_program("/tmp/tool_backup.sh", "system");
    set_progress(0.5);
  endif;

  # backup data
  if file_getprop("/tmp/aroma/backup_type.prop", "item.0.3") == "1" then
    ui_print("  Backing up your data...");
    run_program("/tmp/tool_backup.sh", "data");
    set_progress(0.7);
  endif;

  # calculate md5sum
  ui_print("  Calculating MD5 checksums...");
  package_extract_file("scripts/tool_md5sum.sh", "/tmp/tool_md5sum.sh");
  set_perm(0, 0, 0777, "/tmp/tool_md5sum.sh");
  run_program("/tmp/tool_md5sum.sh", "calculate");
  set_progress(0.9);

  ui_print("  Backup finished");

endif;

###########
# Restore #
###########

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

  ui_print("@  Restoring...");
  set_progress(0.1);
  ui_print("  Checking MD5 checksums...");

  # extract tool_md5sum.sh
  package_extract_file("scripts/tool_md5sum.sh", "/tmp/tool_md5sum.sh");
  set_perm(0, 0, 0777, "/tmp/tool_md5sum.sh");

  # check boot md5sum
  if file_getprop("/tmp/aroma/md5sum_check.prop", "status") != "mismatch" then
    if file_getprop("/tmp/aroma/restore_type.prop", "item.0.1") == "1" then
      run_program("/tmp/tool_md5sum.sh", "check", "boot");
      if file_getprop("/tmp/aroma/md5sum_check.prop", "status") == "mismatch" then
        ui_print("  boot.vfat.tar: FAILED");
      endif;
    endif;
  endif;

  # check system md5sum
  if file_getprop("/tmp/aroma/md5sum_check.prop", "status") != "mismatch" then
    if file_getprop("/tmp/aroma/restore_type.prop", "item.0.2") == "1" then
      run_program("/tmp/tool_md5sum.sh", "check", "system");
      if file_getprop("/tmp/aroma/md5sum_check.prop", "status") == "mismatch" then
        ui_print("  system.ext4.tar: FAILED");
      endif;
    endif;
  endif;

  # check data md5sum
  if file_getprop("/tmp/aroma/md5sum_check.prop", "status") != "mismatch" then
    if file_getprop("/tmp/aroma/restore_type.prop", "item.0.3") == "1" then
      run_program("/tmp/tool_md5sum.sh", "check", "data");
      if file_getprop("/tmp/aroma/md5sum_check.prop", "status") == "mismatch" then
        ui_print("  data.ext4.tar: FAILED");
      endif;
    endif;
  endif;

  set_progress(0.3);

  # Restore
  if file_getprop("/tmp/aroma/md5sum_check.prop", "status") != "mismatch" then

    ui_print("  MD5 checksums OK");

    # extract tool_restore.sh
    package_extract_file("scripts/tool_restore.sh", "/tmp/tool_restore.sh");
    set_perm(0, 0, 0777, "/tmp/tool_restore.sh");

    # restore boot
    if file_getprop("/tmp/aroma/restore_type.prop", "item.0.1") == "1" then
      ui_print("  Restoring your boot (zImage and initrd.gz)...");
      run_program("/tmp/tool_restore.sh", "boot");
      if file_getprop("/tmp/aroma/restore_status.prop", "status") == "ok" then
        ui_print("    boot.vfat.tar restored");
      else
        ui_print("    boot.vfat.tar not found");
      endif;
      set_progress(0.5);
    endif;

    # restore system
    if file_getprop("/tmp/aroma/restore_type.prop", "item.0.2") == "1" then
      ui_print("  Restoring your system...");
      run_program("/tmp/tool_restore.sh", "system");
      if file_getprop("/tmp/aroma/restore_status.prop", "status") == "ok" then
        ui_print("    system.ext4.tar restored");
      else
        ui_print("    system.ext4.tar not found");
      endif;
      set_progress(0.7);
    endif;

    # restore data
    if file_getprop("/tmp/aroma/restore_type.prop", "item.0.3") == "1" then
      ui_print("  Restoring your data...");
      run_program("/tmp/tool_restore.sh", "data");
      if file_getprop("/tmp/aroma/restore_status.prop", "status") == "ok" then
        ui_print("    data.ext4.tar restored");
      else
        ui_print("    data.ext4.tar not found");
      endif;
      set_progress(0.9);
    endif;

    ui_print("  Restore finished");

  else

    ui_print("  MD5 checksums mismatch");
    ui_print("  Restore failed");

  endif;

endif;

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

set_progress(0.95);
ui_print("@  Cleaning up...");
delete("/tmp");
ui_print("  Unmounting...");
unmount("/NativeSD");
ui_print("  Done.");

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