aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/stowify
blob: fb5102542b66ebdda50f8ab26259ce350ad4b148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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"