From dc995e1bae2fffd995309b5227d8f940e3ddf888 Mon Sep 17 00:00:00 2001
From: Dmitry Isaenko <developer.su@gmail.com>
Date: Mon, 6 Jul 2020 14:27:35 +0300
Subject: [PATCH] Add CLI support for Tinfoil/Awoo USB-install mode.

---
 .../nsusbloader/cli/CommandLineInterface.java | 13 +++++
 src/main/java/nsusbloader/cli/TinfoilNet.java |  2 +-
 src/main/java/nsusbloader/cli/TinfoilUsb.java | 51 +++++++++++++++++++
 3 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 src/main/java/nsusbloader/cli/TinfoilUsb.java

diff --git a/src/main/java/nsusbloader/cli/CommandLineInterface.java b/src/main/java/nsusbloader/cli/CommandLineInterface.java
index 7541e30..5cdd9c0 100644
--- a/src/main/java/nsusbloader/cli/CommandLineInterface.java
+++ b/src/main/java/nsusbloader/cli/CommandLineInterface.java
@@ -61,6 +61,11 @@ public class CommandLineInterface {
                 new TinfoilNet(tfnArguments);
                 return;
             }
+            if (cli.hasOption("t") || cli.hasOption("tinfoil")){
+                final String[] tfArguments = cli.getOptionValues("tinfoil");
+                new TinfoilUsb(tfArguments);
+                return;
+            }
         }
         catch (ParseException pe){
             System.out.println(pe.getLocalizedMessage() +
@@ -116,6 +121,13 @@ public class CommandLineInterface {
                 .hasArgs()
                 .argName("...")
                 .build();
+        /* Tinfoil/Awoo USB */
+        final Option tinfoilOption = Option.builder("t")
+                .longOpt("tinfoil")
+                .desc("Install via Tinfoil/Awoo USB mode.")
+                .hasArgs()
+                .argName("FILE1 ...")
+                .build();
 
 
         final OptionGroup group = new OptionGroup();
@@ -124,6 +136,7 @@ public class CommandLineInterface {
         group.addOption(cleanSettingsOption);
         group.addOption(versionOption);
         group.addOption(helpOption);
+        group.addOption(tinfoilOption);
 
         options.addOptionGroup(group);
 
diff --git a/src/main/java/nsusbloader/cli/TinfoilNet.java b/src/main/java/nsusbloader/cli/TinfoilNet.java
index 3230bd6..1818a1b 100644
--- a/src/main/java/nsusbloader/cli/TinfoilNet.java
+++ b/src/main/java/nsusbloader/cli/TinfoilNet.java
@@ -123,7 +123,7 @@ public class TinfoilNet {
         }
 
         if (filesList.size() == 0) {
-            throw new IncorrectSetupException("File(s) doesn't exists.\n" +
+            throw new IncorrectSetupException("File(s) doesn't exist.\n" +
                     "Try 'ns-usbloader -n help' for more information.");
         }
     }
diff --git a/src/main/java/nsusbloader/cli/TinfoilUsb.java b/src/main/java/nsusbloader/cli/TinfoilUsb.java
new file mode 100644
index 0000000..faf46f5
--- /dev/null
+++ b/src/main/java/nsusbloader/cli/TinfoilUsb.java
@@ -0,0 +1,51 @@
+package nsusbloader.cli;
+
+import nsusbloader.COM.ICommunications;
+import nsusbloader.COM.USB.UsbCommunications;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+public class TinfoilUsb {
+
+    private final String[] arguments;
+    private List<File> filesList;
+
+    public TinfoilUsb(String[] arguments) throws InterruptedException, IncorrectSetupException{
+        this.arguments = arguments;
+        checkArguments();
+        parseFilesArguments();
+        runTinfoilBackend();
+    }
+
+    private void checkArguments() throws IncorrectSetupException{
+        if (arguments == null || arguments.length == 0) {
+            throw new IncorrectSetupException("No files?\n" +
+                    "Try 'ns-usbloader -h' for more information.");
+        }
+    }
+
+    private void parseFilesArguments() throws IncorrectSetupException{
+        filesList = new ArrayList<>();
+        File file;
+
+        for (String arg : arguments) {
+            file = new File(arg);
+            if (file.exists())
+                filesList.add(file);
+        }
+
+        if (filesList.size() == 0) {
+            throw new IncorrectSetupException("File(s) doesn't exist.\n" +
+                    "Try 'ns-usbloader -n help' for more information.");
+        }
+    }
+
+    private void runTinfoilBackend() throws InterruptedException{
+        ICommunications task = new UsbCommunications(filesList, "TinFoil", false);
+        Thread thread = new Thread(task);
+        thread.start();
+        thread.join();
+    }
+}