From af7d8e0614d074f374a14a462eb7430a919d27b9 Mon Sep 17 00:00:00 2001
From: "flyingscorpio@arch-desktop" <tfranken@protonmail.com>
Date: Wed, 22 Apr 2020 15:57:04 +0200
Subject: [PATCH] Make install packages function

---
 install.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 56 insertions(+), 6 deletions(-)

diff --git a/install.py b/install.py
index a1999b1..02fc40b 100755
--- a/install.py
+++ b/install.py
@@ -6,6 +6,7 @@ Shamelessly copied from https://github.com/dmerejkowsky/dotfiles.
 """
 
 import argparse
+import shlex
 import subprocess
 from typing import List, Optional
 from urllib.request import urlretrieve
@@ -41,7 +42,7 @@ class Installer:
         if condition in conditions.keys():
             return conditions[condition]
 
-        command = subprocess.run(condition, check=False, shell=True)
+        command = subprocess.run(shlex.split(condition), check=False)
 
         return command.returncode == 0
 
@@ -72,7 +73,7 @@ class Installer:
 
         ui.info_2("Cloning", url, "->", pretty_dest)
         git_command = "git clone {} {} --branch {}".format(url, p_dest, branch)
-        subprocess.run(git_command, check=True, shell=True)
+        subprocess.run(shlex.split(git_command), check=True)
 
     def do_copy(self, src: str, dest: str) -> None:
         """Copy two files."""
@@ -107,6 +108,46 @@ class Installer:
         if executable:
             p_dest.chmod(0o755)
 
+    def do_install(self, *packages) -> None:
+        """Install packages with OS-dependent package manager."""
+
+        failed_installs = []
+
+        for package in packages:
+            ui.info_2("Installing", package, ui.ellipsis, end='')
+            if self.operating_system == "arch based":
+                command = "sudo pacman -S --needed {}".format(package)
+            elif self.operating_system == "debian based":
+                command = "sudo apt install {}".format(package)
+            else:
+                ui.fatal("Operating System not understood.")
+                return
+
+            install = subprocess.run(
+                shlex.split(command),
+                check=False,
+                stdout=subprocess.DEVNULL,
+                stderr=subprocess.DEVNULL,
+            )
+
+            if install.returncode != 0:
+                ui.info(ui.cross)
+                failed_installs.append(package)
+            else:
+                ui.info(ui.check)
+
+        if len(failed_installs) > 0:
+            ui.warning("These packages failed to install:")
+
+            for failed in failed_installs:
+                ui.info(" ", failed)
+
+            ui.info(
+                ui.yellow,
+                "Please check if the package was really meant for {} systems."
+                .format(self.operating_system)
+            )
+
     def do_run(self, command: str, condition: str = "true") -> None:
         """Run a command."""
 
@@ -115,7 +156,7 @@ class Installer:
             return
 
         ui.info_2("Running", "`{}`".format(command))
-        subprocess.run(command, check=True, shell=True)
+        subprocess.run(shlex.split(command), check=True)
         self.base_dir.chdir()
 
     def do_symlink(self, src: str, dest: str) -> None:
@@ -214,17 +255,26 @@ class Installer:
 
 
 def define_os() -> str:
-    """Define what OS we are using. Defaults to Arch Linux."""
+    """Define what OS we are using."""
 
     os_release = Path("/etc/os-release")
 
-    oses = ("debian", "arch")
+    oses = ("arch", "debian", "manjaro", "ubuntu")
 
     for operating_system in oses:
         if operating_system in os_release.read_text().lower():
             ui.info_3("Found", operating_system)
 
-            return operating_system
+            if operating_system in ("arch", "manjaro"):
+                return "arch based"
+
+            if operating_system in ("debian", "ubuntu"):
+                subprocess.run(
+                    shlex.split("sudo apt update"),
+                    stdout=subprocess.DEVNULL,
+                    check=True,
+                )
+                return "debian based"
 
     ui.warning("Operating system wasn't found")