aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/stowify
diff options
context:
space:
mode:
authorMohammad Reza Karimi <m.r.karimi.j@gmail.com>2026-01-04 21:28:36 -0500
committerMohammad Reza Karimi <m.r.karimi.j@gmail.com>2026-01-04 21:28:36 -0500
commit06d1242317f7159ccf3014f0a1480e7c56236ebb (patch)
tree89863b3287ae2c7cc88cd532220f18c38e8d8657 /scripts/dot-local/bin/stowify
parent5a41da5881a11ba3fdc3890c342aa3b7eb53e0cf (diff)
add all sorts of things
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"