working
authorIan Kelling <ian@iankelling.org>
Sat, 7 May 2016 21:52:17 +0000 (14:52 -0700)
committerIan Kelling <ian@iankelling.org>
Sat, 7 May 2016 21:52:17 +0000 (14:52 -0700)
conflink [new file with mode: 0755]

diff --git a/conflink b/conflink
new file mode 100755 (executable)
index 0000000..ddf9901
--- /dev/null
+++ b/conflink
@@ -0,0 +1,64 @@
+#!/bin/bash -l
+
+set -eE -o pipefail
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
+
+
+sysv() {
+    e "$@"
+    "$@"
+}
+
+
+shopt -s nullglob
+
+# if we make a link back to the root, traversing the subdirs stops.
+# This makes it so we can do subdir directories. Must call with absolute
+# paths.
+
+subdir-link-r() {
+    local root="$1"
+    local targets=()
+    if [[ $2 ]]; then
+        targets=( "$2"/* )
+    else
+        for f in "$1"/*; do
+            [[ -d $f ]] && targets+=("$f") ||:
+        done
+    fi
+    local below="$( readlink -f "$root/..")"
+    for path in "${targets[@]}"; do
+        local fullpath="$(readlink -f "$path")"
+        #e $fullpath $below # debug
+        if [[ -f $path || $(dirname $(readlink -f "$fullpath")) == "$below" ]]; then
+            if [[ $dir == /p/* ]]; then
+                homes=(/home/ian)
+            else
+                homes=(/home/*)
+            fi
+            for user in ${homes[@]}; do
+                sysv lnf -T "$path" "$user/${path#$root/}"
+            done
+        elif [[ -d "$path" ]]; then
+            subdir-link-r "$root" "$path"
+        fi
+    done
+}
+
+common-file-setup() {
+    if [[ -e $1/subdir_files ]]; then
+        subdir-link-r $1/subdir_files
+    fi
+    local x=( $1/!(subdir_files|filesystem) )
+    (( ${#x[@]} >= 1 )) || return 0
+    sysv lnf ${x[@]} ~
+    # as of this writing, it doesn't exist in /a/c, but I've used it in the past.
+    if [[ -e $1/filesystem ]]; then
+        sysv s cp -R $1/filesystem/* /
+    fi
+}
+
+# p needs to go first so .ssh link is created, then config link inside it
+for dir in {/a/c,/p/c}{,/machine_specific/$HOSTNAME}; do
+    common-file-setup $dir
+done