Convert AOSP commands to ORS for encrypted devices

Change-Id: I6a76a51cd9efd7db67d7abeeb26f9bdd0eebf5a9
diff --git a/gui/action.cpp b/gui/action.cpp
index 997cf55..9c3ee59 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -1014,6 +1014,18 @@
 
 					DataManager::SetValue(TW_IS_ENCRYPTED, 0);
 					DataManager::ReadSettingsFile();
+					// Check for the SCRIPT_FILE_TMP first as these are AOSP recovery commands
+					// that we converted to ORS commands during boot in recovery.cpp.
+					// Run those first.
+					if (TWFunc::Path_Exists(SCRIPT_FILE_TMP)) {
+						ui_print("Processing AOSP recovery commands...\n");
+						if (OpenRecoveryScript::run_script_file() == 0) {
+							usleep(2000000); // Sleep for 2 seconds before rebooting
+							TWFunc::tw_reboot(rb_system);
+							load_theme = 0;
+						}
+					}
+					// Check for the ORS file in /cache and attempt to run those commands.
 					if (OpenRecoveryScript::check_for_script_file()) {
 						ui_print("Processing OpenRecoveryScript file...\n");
 						if (OpenRecoveryScript::run_script_file() == 0) {
diff --git a/openrecoveryscript.cpp b/openrecoveryscript.cpp
index 8e95555..f0efae4 100644
--- a/openrecoveryscript.cpp
+++ b/openrecoveryscript.cpp
@@ -30,6 +30,8 @@
 #include <dirent.h>
 #include <time.h>
 #include <errno.h>
+#include <iostream>
+#include <fstream>
 
 #include "twrp-functions.hpp"
 #include "partitions.hpp"
@@ -38,15 +40,13 @@
 #include "variables.h"
 #include "adb_install.h"
 extern "C" {
-#include "data.h"
-#include "twinstall.h"
-#include "gui/gui.h"
-int TWinstall_zip(const char* path, int* wipe_cache);
+	#include "data.h"
+	#include "twinstall.h"
+	#include "gui/gui.h"
+	int TWinstall_zip(const char* path, int* wipe_cache);
 }
 
 extern RecoveryUI* ui;
-static const char *SCRIPT_FILE_CACHE = "/cache/recovery/openrecoveryscript";
-static const char *SCRIPT_FILE_TMP = "/tmp/openrecoveryscript";
 #define SCRIPT_COMMAND_SIZE 512
 
 int OpenRecoveryScript::check_for_script_file(void) {
@@ -378,6 +378,20 @@
 	return ret_val;
 }
 
+int OpenRecoveryScript::Insert_ORS_Command(string Command) {
+	ofstream ORSfile(SCRIPT_FILE_TMP);
+	if (ORSfile.is_open()) {
+		//if (Command.substr(Command.size() - 1, 1) != "\n")
+		//	Command += "\n";
+		LOGI("Inserting '%s'\n", Command.c_str());
+		ORSfile << Command.c_str();
+		ORSfile.close();
+		return 1;
+	}
+	LOGE("Unable to append '%s' to '%s'\n", Command.c_str(), SCRIPT_FILE_TMP);
+	return 0;
+}
+
 int OpenRecoveryScript::Install_Command(string Zip) {
 	// Install zip
 	string ret_string;
diff --git a/openrecoveryscript.hpp b/openrecoveryscript.hpp
index ca0ea9f..2c7a81e 100644
--- a/openrecoveryscript.hpp
+++ b/openrecoveryscript.hpp
@@ -33,6 +33,7 @@
 public:
 	static int check_for_script_file();                                            // Checks to see if the ORS file is present in /cache
 	static int run_script_file();                                                  // Executes the commands in the ORS file
+	static int Insert_ORS_Command(string Command);                                 // Inserts the Command into the SCRIPT_FILE_TMP file
 	static int Install_Command(string Zip);                                        // Installs a zip
 	static string Locate_Zip_File(string Path, string File);                       // Attempts to locate the zip file in storage
 	static int Backup_Command(string Options);                                     // Runs a backup
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);
diff --git a/variables.h b/variables.h
index 0b1c7ab..c40badb 100644
--- a/variables.h
+++ b/variables.h
@@ -171,4 +171,8 @@
 #define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun%d/file"
 #endif
 
+// For OpenRecoveryScript
+#define SCRIPT_FILE_CACHE "/cache/recovery/openrecoveryscript"
+#define SCRIPT_FILE_TMP "/tmp/openrecoveryscript"
+
 #endif  // _VARIABLES_HEADER_