#!/usr/bin/env bash set -Eeuo pipefail if [ $# -lt 2 ]; then echo "Usage: $0 [source2 ...] " 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"