summaryrefslogtreecommitdiffstats
path: root/packaging/macos/jhb/usr/bin/run-parts
diff options
context:
space:
mode:
Diffstat (limited to 'packaging/macos/jhb/usr/bin/run-parts')
-rwxr-xr-xpackaging/macos/jhb/usr/bin/run-parts73
1 files changed, 73 insertions, 0 deletions
diff --git a/packaging/macos/jhb/usr/bin/run-parts b/packaging/macos/jhb/usr/bin/run-parts
new file mode 100755
index 0000000..ff0470c
--- /dev/null
+++ b/packaging/macos/jhb/usr/bin/run-parts
@@ -0,0 +1,73 @@
+#!/usr/bin/env bash
+
+### description ################################################################
+
+# A partial replacement for Debian's "run-parts" tool. Execute files in a
+# directory in their lexical order with the specialty of being able to
+# use symlinks to create an order without executing the files twice.
+
+### shellcheck #################################################################
+
+# Nothing here.
+
+### dependencies ###############################################################
+
+# Nothing here.
+
+### variables ##################################################################
+
+# Nothing here.
+
+### functions ##################################################################
+
+function main
+{
+ local mode=$1
+ local dir=$2
+
+ local file_pattern
+
+ if [ -d "$dir" ]; then
+ # reconstruct to make it proper (e.g. remove superfluous slashes)
+ dir=$(dirname "$dir")/$(basename "$dir")
+ else
+ # split into directory and file
+ file_pattern=$(basename "$dir")
+ dir=$(dirname "$dir")
+ fi
+
+ file_pattern=${file_pattern:-*} # default pattern is "*"
+
+ local linked_files
+ linked_files=$(find "$dir" -type l -exec readlink {} +)
+
+ for file in "$dir"/$file_pattern; do # requires 'shopt -s nullglob'
+ # Only process files that have no symlinks (in that same directory) pointing
+ # at them.
+ if [[ "$linked_files" != *$(basename "$file")* ]]; then
+ case "$mode" in
+ list)
+ echo "$file"
+ ;;
+ run)
+ $file
+ ;;
+ esac
+ fi
+ done
+}
+
+### main #######################################################################
+
+set -e
+
+shopt -s nullglob # in case no files are found
+
+case "$1" in
+ list)
+ main "list" "$2"
+ ;;
+ *)
+ main "run" "$1"
+ ;;
+esac