From b114ada4f83f9267b2382de60b7833ebc77c2950 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Tue, 03 Oct 2023 02:53:40 +0000
Subject: [PATCH] Install now checks that installed file is different to file before asking

---
 INSTALL.sh |  107 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 60 insertions(+), 47 deletions(-)

diff --git a/INSTALL.sh b/INSTALL.sh
index 0b10323..dd5bfb3 100755
--- a/INSTALL.sh
+++ b/INSTALL.sh
@@ -1,48 +1,58 @@
 #!/bin/sh
 
+install_conflict() {
+    file="$1";
+    loc="$2";
+    if [ -n "$3" ]; then
+        SUPERUSER="sudo"
+    else
+        SUPERUSER="";
+    fi
+    while true; do
+        CHOICE="overwrite"
+        if [ -e "$loc" ] && [ "$FORCE" = "0" ] && [ $(diff "$loc" "$file") ]; then
+            echo "WARNING: \"$loc\" exists, (overwrite, change, nothing): "
+            read CHOICE
+        fi
+        case "$CHOICE" in
+            o|overwrite)
+	            dir=$(dirname "$loc")
+	            $SUPERUSER mkdir -p "$dir"
+	            $SUPERUSER ln -sf "$(pwd)/$file" "$loc"; break;;
+            c|change)
+                tmpfile=$(mktemp)
+                echo "$loc" >> "$tmpfile"
+                $EDITOR "$tmpfile"
+                loc=$(cat "$tmpfile")
+                rm "$tmpfile"
+                unset tmpfile;;
+            n|nothing)
+                break;;
+            *)
+                echo "INVALID CHOICE \"$CHOICE\", (overwrite, change, nothing)"
+                read CHOICE
+        esac
+    done
+
+}
+
 install_links() {
     . ./DICT
     if [ -n "$FILES" ]; then
 	for i in $(seq 1 "$(echo "$FILES" | wc -w)"); do
 	    file=$(echo "$FILES" | cut -d' ' -f "$i")
 	    loc=$(echo "$LOCATIONS" | cut -d' ' -f "$i")
-        while true; do
-            CHOICE="overwrite"
-            if [ -e "$loc" ] && [ "$FORCE" = "0" ]; then
-                echo "WARNING: \"$loc\" exists, (overwrite, change, nothing): "
-                read CHOICE
-            fi
-            case "$CHOICE" in
-                o|overwrite)
-	                dir=$(dirname "$loc")
-	                mkdir -p "$dir"
-	                ln -sf "$(pwd)/$file" "$loc"; break;;
-                c|change)
-                    tmpfile=$(mktemp)
-                    echo "$loc" >> "$tmpfile"
-                    $EDITOR "$tmpfile"
-                    loc=$(cat "$tmpfile")
-                    rm "$tmpfile"
-                    unset tmpfile;;
-                n|nothing)
-                    break;;
-                *)
-                    echo "INVALID CHOICE \"$CHOICE\", (overwrite, change, nothing)"
-                    read CHOICE
-            esac
-        done
+        install_conflict "$file" "$loc"
 	done
     fi
     if [ -n "$SUDO_FILES" ]; then
-	for i in $(seq 1 "$(echo "$SUDO_FILES" | wc -w)"); do
-	    file=$(echo "$SUDO_FILES" | cut -d' ' -f "$i")
-	    loc=$(echo "$SUDO_LOCATIONS" | cut -d' ' -f "$i")
-	    dir=$(echo "$loc" | rev | cut -d'/' -f2- | rev)
-	    sudo mkdir -p "$dir"
-	    sudo ln -sf "$(pwd)/$file" "$loc"
-	done
+	    for i in $(seq 1 "$(echo "$SUDO_FILES" | wc -w)"); do
+	        file=$(echo "$SUDO_FILES" | cut -d' ' -f "$i")
+	        loc=$(echo "$SUDO_LOCATIONS" | cut -d' ' -f "$i")
+            install_conflict "$file" "$loc" 1
+	    done
     fi
-    if type custom | grep -q "function" ; then
+    if type custom 2>/dev/null | grep -q "function" ; then
 	    custom install
     fi
 }
@@ -77,7 +87,6 @@
 }
 
 handle_package() {
-    echo "$1"
     if [ -d "$1" ]; then
 	unset DEPS
 	eval "$(grep "DEPS=" "$1/DICT")"
@@ -88,6 +97,7 @@
             fi
 	    done
     fi
+    echo "$1"
     echo "$1" >> "$PACKAGE_CACHE"
 	(cd "$1" || exit; "$([ "$INSTALL" = "1" ] && echo install || echo remove)_links")
     else
@@ -96,15 +106,16 @@
 }
 
 usage() {
-    echo "Install dotfiles with symlinks"
-    echo "Usage with -h|--help"
-    echo "Install with -i|--install (default)"
-    echo "Remove with -r|--remove"
-    echo "Force install with -f|--force"
-    echo "Directories have the configurations for their programs/use cases"
-    echo "List all configurations to be installed in the command line"
-    echo "If there is a file at the install location, you will be prompted to change it"
-    echo "Example usage: $0 -i bash zsh"
+    cat << EOF
+USAGE
+    $(basename "$0") [options] configs... ([option] [configs])...
+
+OPTIONS
+    -h, --help          Display this help message
+    -i, --install       Install packages following this flag (default)
+    -r, --remove        Remove packages following this flag
+    -f, --force         Force install all packages following this flag
+EOF
 }
 
 INSTALL=1
@@ -123,13 +134,15 @@
 while [ -n "$1" ]; do
       case "$1" in
 	  -h|--help|"") usage; exit;;
-	  -i|--install) INSTALL=1;;
-	  -r|--remove) INSTALL=0;;
-      -d|--deps) REMOVE_DEPS=1;;
+	  -i|--install) INSTALL=1; FORCE=0;;
       -f|--force) FORCE=1;;
+      -if|-fi) INSTALL=1; FORCE=1;;
+	  -r|--remove) INSTALL=0; REMOVE_DEPS=0;;
+      -d|--deps) REMOVE_DEPS=1;;
+      -rd|-dr) INSTALL=0; REMOVE_DEPS=1;;
 	  --) shift; break;;
 	  -*) echo "Invalid argument"; usage; exit;;
-	  *) handle_package "$1" "$([ \"$INSTALL\" = \"1\" ] && echo install || echo remove)_links";;
+	  *) handle_package "$1";; # "$([ \"$INSTALL\" = \"1\" ] && echo install || echo remove)_links";;
       esac
       shift
 done

--
Gitblit v1.9.3