aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/stowify
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/dot-local/bin/stowify')
-rwxr-xr-xscripts/dot-local/bin/stowify74
1 files changed, 74 insertions, 0 deletions
diff --git a/scripts/dot-local/bin/stowify b/scripts/dot-local/bin/stowify
new file mode 100755
index 0000000..fb51025
--- /dev/null
+++ b/scripts/dot-local/bin/stowify
@@ -0,0 +1,74 @@
+#!/usr/bin/env bash
+
+set -Eeuo pipefail
+
+if [ $# -lt 2 ]; then
+ echo "Usage: $0 <source1> [source2 ...] <package_name>"
+ return 1
+fi
+
+pkg="${@:$#}"
+stowdir="$HOME/dotfiles"
+sources=("${@:1:$#-1}")
+
+mkdir -p "$stowdir/$pkg"
+
+_stowify_convert_path() {
+ local rel_path="$1"
+ local IFS='/'
+ local parts out_parts part
+
+ read -ra parts <<<"$rel_path"
+ for part in "${parts[@]}"; do
+ if [[ $part == .* ]]; then
+ out_parts+=("dot-${part#.}")
+ else
+ out_parts+=("$part")
+ fi
+ done
+
+ local out=""
+ for part in "${out_parts[@]}"; do
+ if [ -z "$out" ]; then
+ out="$part"
+ else
+ out="$out/$part"
+ fi
+ done
+ printf '%s\n' "$out"
+}
+
+for src in "${sources[@]}"; do
+ src="$(realpath -m "$src")" # expand to absolute path
+
+ if [ ! -e "$src" ]; then
+ echo "Error: $src does not exist"
+ continue
+ fi
+
+ if [[ $src == "$HOME"/* ]]; then
+ rel_path="${src#"$HOME"/}"
+ else
+ echo "Warning: $src is not under \$HOME — skipping"
+ continue
+ fi
+
+ conv_rel_path="$(_stowify_convert_path "$rel_path")" || continue
+
+ dest_path="$stowdir/$pkg/$conv_rel_path"
+ dest_dir="$(dirname "$dest_path")"
+
+ echo "$dest_dir"
+ echo "$dest_path"
+
+ mkdir -pv "$dest_dir" || {
+ echo "Failed to create $dest_dir"
+ continue
+ }
+ mv -v "$src" "$dest_path" || {
+ echo "Failed to move $src"
+ continue
+ }
+done
+
+stow -d "$stowdir" -v --dotfiles "$pkg"