commit 3fc64c1b0faf683a504b714423131cd273d584ea
parent 9922c6096f632c3df3a7c1960f3ae7c36a95537c
Author: Jacob R. Edwards <n/a>
Date: Thu, 8 Dec 2022 10:15:54 -0600
Add destination substitution to modsync
While the script gets more and more janky, substitution can be nice
to have. (Primarily, or wholly, due to UNIX'es insistence on prefixing
dots onto filenames.)
Diffstat:
| M | lib/modsync | | | 40 | +++++++++++++++++++++++++++++++++++----- |
1 file changed, 35 insertions(+), 5 deletions(-)
diff --git a/lib/modsync b/lib/modsync
@@ -11,7 +11,7 @@ error() {
}
usererror() {
- error "usage: $name [push|pull|diff|stop] [-cdnop] dest module ..."
+ error "usage: $name [push|pull|diff|stop] [-cdnop] [-s subs] dest module ..."
test "${1:-}" &&
error "$@"
}
@@ -65,6 +65,26 @@ filesync() {
fi
}
+addsub() {
+ awk -vOFS="$IFS2" -F/ -vsubs="$1" 'BEGIN {
+ len = split(subs, a, ",");
+ for (i = 1; i <= len; ++i) {
+ if (!(n = index(a[i], "="))) {
+ print "Invalid substitution" > "/dev/stderr";
+ exit 1;
+ }
+ t[substr(a[i], 1, n - 1)] = substr(a[i], n + 1);
+ }
+ }
+
+ {
+ new = $0
+ if ($1 in t)
+ new = t[$1] substr(new, length($1) + 1);
+ print $0, new
+ }'
+}
+
modsync() {
cmd="$1"
mod="$2"
@@ -77,10 +97,16 @@ modsync() {
(*) usererror "$name: '$1': Invalid command" ;;
esac
- for file in $(cd "$mod" && find . -type f | cut -c 3-)
- do
- sync "$mod/$file" "$dir/$file"
- done
+ for files in $(cd "$mod" && find . -type f | cut -c 3- | addsub "$subs")
+ do (
+ IFS="$IFS2"
+ set -- $files
+ test $# -ne 2 && {
+ echo 'error: fs (034) character in path' 1>&2
+ exit 1
+ }
+ echo sync "$mod/$1" "$dir/$2"
+ ) done
}
# Unset variables are unchecked on purpose, nounset (-u) handles it
@@ -88,6 +114,8 @@ set -eu
IFS='
'
+# \034 (fs)
+IFS2=''
name="$(basename "$0")"
# Assume exit is due to user error, namely an unset variable
@@ -107,6 +135,7 @@ owner=
perms=
fdiff=false
fcopy=false
+subs=
while expr x"$1" : x- > /dev/null
do
case "$1" in
@@ -115,6 +144,7 @@ do
(-p) perms="$2"; shift ;;
(-d) fdiff=true ;;
(-c) fcopy=true ;;
+ (-s) subs="$2"; shift ;;
(*) usererror "$name: '$1': Invalid option" ;;
esac
shift