Convert AOSP commands to ORS for encrypted devices

Change-Id: I6a76a51cd9efd7db67d7abeeb26f9bdd0eebf5a9
diff --git a/recovery.cpp b/recovery.cpp
index 581e67e..7d1dd14 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -822,6 +822,7 @@
 	// Load up all the resources
 	gui_loadResources();
 
+	PartitionManager.Mount_By_Path("/cache", true);
     get_args(&argc, &argv);
 
     int previous_runs = 0;
@@ -907,19 +908,23 @@
 #endif
 
     int status = INSTALL_SUCCESS;
+	string ORSCommand;
 
 	if (perform_backup) {
 		char empt[50];
 		gui_console_only();
 		strcpy(empt, "(Current Date)");
 		DataManager_SetStrValue(TW_BACKUP_NAME, empt);
-		if (OpenRecoveryScript::Backup_Command("BSDCAE") != 0)
+		if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
 			status = INSTALL_ERROR;
 	}
 	if (status == INSTALL_SUCCESS) { // Prevent other actions if backup failed
     if (update_package != NULL) {
-		gui_console_only();
-		if (OpenRecoveryScript::Install_Command(update_package) == 0)
+		ORSCommand = "install ";
+		ORSCommand += update_package;
+		ORSCommand += "\n";
+
+		if (OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
 			status = INSTALL_SUCCESS;
 		else
 			status = INSTALL_ERROR;
@@ -933,8 +938,8 @@
         if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
 		*/
     } else if (wipe_data) {
-		gui_console_only();
-		if (!PartitionManager.Factory_Reset()) status = INSTALL_ERROR;
+		if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
+			status = INSTALL_ERROR;
 		/*
         if (device->WipeData()) status = INSTALL_ERROR;
         if (erase_volume("/data")) status = INSTALL_ERROR;
@@ -942,8 +947,8 @@
 		*/
         if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
     } else if (wipe_cache) {
-		gui_console_only();
-        if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
+        if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
+			status = INSTALL_ERROR;
         if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
     } else if (!just_exit) {
         status = INSTALL_ERROR;  // No command specified
@@ -951,7 +956,7 @@
 	}
 
     //if (status != INSTALL_SUCCESS) ui->SetBackground(RecoveryUI::ERROR);
-    if (status != INSTALL_SUCCESS /*|| ui->IsTextVisible()*/) {
+    if (1) {
 		finish_recovery(NULL);
 		DataManager_ReadSettingsFile();
 		if (PartitionManager.Mount_By_Path("/system", false) && TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
@@ -959,14 +964,33 @@
 			ui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
 		}
 		PartitionManager.UnMount_By_Path("/system", false);
-		if (DataManager_GetIntValue(TW_IS_ENCRYPTED) == 0 && OpenRecoveryScript::check_for_script_file()) {
-			gui_console_only();
-			if (OpenRecoveryScript::run_script_file() != 0) {
-				// There was an error, boot the recovery
-				gui_start();
-			} else {
-				usleep(2000000); // Sleep for 2 seconds before rebooting
+		if (DataManager_GetIntValue(TW_IS_ENCRYPTED) == 0) {
+			// Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
+			// that we converted to ORS commands up above. Run those first.
+			int boot_recovery = 1, check = 1;
+			if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
+				boot_recovery = 0;
+				gui_console_only();
+				if (OpenRecoveryScript::run_script_file() != 0) {
+					// There was an error, boot the recovery
+					check = 0;
+					gui_start();
+				} else {
+					usleep(2000000); // Sleep for 2 seconds before rebooting
+				}
 			}
+			// Check for the ORS file in /cache and attempt to run those commands.
+			if (check && OpenRecoveryScript::check_for_script_file()) {
+				boot_recovery = 0;
+				if (OpenRecoveryScript::run_script_file() != 0) {
+					// There was an error, boot the recovery
+					gui_start();
+				} else {
+					usleep(2000000); // Sleep for 2 seconds before rebooting
+				}
+			}
+			if (boot_recovery)
+				gui_start();
 		} else
 			gui_start();
 		//prompt_and_wait(device);