From b350fb1b83ed1b686cc6e753bd398e28be4649d4 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Sat, 18 Jun 2022 10:40:43 +0300
Subject: [PATCH] Update DevEx for the win.

Replace the *-daily scripts with a nicer update script with tab
completion.
---
 .bash_completion.d/update   | 15 ++++++++
 Documents/bin/apt-daily     |  2 -
 Documents/bin/flatpak-daily |  6 ---
 Documents/bin/update        | 77 +++++++++++++++++++++++++++++++++++++
 4 files changed, 92 insertions(+), 8 deletions(-)
 create mode 100644 .bash_completion.d/update
 delete mode 100755 Documents/bin/apt-daily
 delete mode 100755 Documents/bin/flatpak-daily
 create mode 100755 Documents/bin/update

diff --git a/.bash_completion.d/update b/.bash_completion.d/update
new file mode 100644
index 0000000..0b89c97
--- /dev/null
+++ b/.bash_completion.d/update
@@ -0,0 +1,15 @@
+# vim: ft=bash
+
+_update() {
+    local cur prev words cword opts
+    _init_completion || return
+    opts='-h -d'
+    if [[ $cur == -* ]]
+    then
+        COMPREPLY=($(compgen -W "$opts" -- "$cur"))
+    else
+        COMPREPLY=($(compgen -W "all apt flatpak workbench" -- "$cur"))
+    fi
+}
+
+complete -F _update update
diff --git a/Documents/bin/apt-daily b/Documents/bin/apt-daily
deleted file mode 100755
index 65258e0..0000000
--- a/Documents/bin/apt-daily
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/sh
-exec sudo /bin/sh -c 'apt-get update && apt-get dist-upgrade --download-only --yes && apt-get autoclean'
diff --git a/Documents/bin/flatpak-daily b/Documents/bin/flatpak-daily
deleted file mode 100755
index bf88308..0000000
--- a/Documents/bin/flatpak-daily
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/sh
-set -eu
-
-flatpak --system update --appstream
-flatpak --system update --assumeyes
-flatpak --system uninstall --unused --assumeyes
diff --git a/Documents/bin/update b/Documents/bin/update
new file mode 100755
index 0000000..62bb92a
--- /dev/null
+++ b/Documents/bin/update
@@ -0,0 +1,77 @@
+#!/bin/sh
+set -eu
+
+usage () {
+    echo "Usage: $0 [-d] [all|apt|flatpak|workbench]"
+}
+
+header () {
+    printf '\n\e[1;92m=== %s ====\e[0m\n\n' "$*" >&2
+}
+
+_apt () {
+    header Updating APT
+    if [ -n "${DOWNLOAD_ONLY:-}" ]
+    then
+        sudo /bin/sh -c 'apt-get update && apt-get dist-upgrade --download-only --yes && apt-get autoclean'
+    else
+        sudo /bin/sh -c 'apt-get update && apt-get dist-upgrade --yes --auto-remove --purge && apt-get autoclean'
+    fi
+}
+
+_flatpak() {
+    header Updating Flatpak
+    flatpak --system update --appstream
+
+    if [ -n "${DOWNLOAD_ONLY:-}" ]
+    then
+        flatpak --system update --assumeyes --no-deploy
+    else
+        flatpak --system update --assumeyes
+        flatpak --system uninstall --unused --assumeyes
+    fi
+}
+
+_workbench() {
+    header Updating Workbench
+    if [ -n "${DOWNLOAD_ONLY:-}" ]
+    then
+        wb --dry-update
+    else
+        wb --update
+    fi
+}
+
+_all() {
+    _apt
+    _flatpak
+    _workbench
+}
+
+while getopts 'dh' opt
+do
+    case "$opt" in
+        d) DOWNLOAD_ONLY=1;;
+        h) usage;;
+        *) usage; exit 1;;
+    esac
+done
+
+shift "$(( OPTIND - 1 ))"
+
+if [ "$#" -eq 0 ]
+then
+    _all
+elif [ "$#" -gt 1 ]
+then
+    usage
+    exit 1
+else
+    case "$1" in
+        all) _all;;
+        apt) _apt;;
+        flatpak) _flatpak;;
+        workbench|wb) _workbench;;
+        *) usage; exit 1;;
+    esac
+fi
-- 
GitLab