aboutsummaryrefslogtreecommitdiffstats
path: root/link.sh
blob: 4f79d0371e96e7ea324c1e5d47b25454433896cc (plain) (blame)
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
#!/bin/sh
set -o errexit
set -o nounset

DOTFILES=${DOTFILES:-$HOME/.dotfiles}
DOTFILES=$(cd "$DOTFILES" && pwd)

# Directories where we want the link the contents.
CONTAINERS='.config'

main() {
  cd "$DOTFILES"
  walk ''
}


walk() {
  for path in "$1".[!.]* "$1"..?* "$1"*; do
    [ -e "$path" ] || [ -L "$path" ] || continue   # unmatched glob
    case $path in
      .git|.gitignore|link.sh|README*) continue ;;
    esac
    if [ -d "$path" ] && is_container "$path"; then
      walk "$path/"
    else
      link "$path"
    fi
  done
}

is_container() {
  for c in $CONTAINERS; do
    case $c in
      "$1"|"$1"/*) return 0 ;;
    esac
  done
  return 1
}

link() {
  src=$DOTFILES/$1
  dest=$HOME/$1

  mkdir -p "$(dirname "$dest")"

  if [ -L "$dest" ]; then
    rm "$dest"
  elif [ -e "$dest" ]; then
    mv "$dest" "$dest.bak.$(date +%Y%m%d%H%M%S)"
  fi

  ln -s "$src" "$dest"
  printf 'linked %s\n' "$1"
}

main