-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmklink
executable file
·108 lines (83 loc) · 2.31 KB
/
mklink
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash
set -eu -o pipefail
backup_and_link() {
local source=${1}
local target=$2
local __dry_run="${DRYRUN+echo dryrun-would-exec:}"
if [[ -L ${target} || -f ${target} || -d ${target} ]]; then
# If already a symlink to desired source, we're done.
[[ -L ${target} && $(readlink "${target}") == "${source}" ]] && return
datestamp=${datestamp:-$(date -u +%Y%m%dT%H%M%SZ)} # cached
local backup="${target}.bak.${datestamp}"
echo "Old ${target} backed up as ${backup}" >&2
${__dry_run} mv "${target}" "${backup}"
fi
${__dry_run} ln -s "${source}" "${target}"
}
link() {
local source target
source="${HERE}/$1"
target="${HOME}/$2"
backup_and_link "${source}" "${target}"
}
link:dotfiles() {
local source target
for dotfile in $(find ${HERE} -mindepth 1 -maxdepth 1 -type f -name 'dot.*'); do
source="${dotfile}"
target="$(basename "${dotfile}")"
target="${HOME}/${target##dot}"
backup_and_link "${source}" "${target}"
done
}
link:dot_config() {
local source target
mkdir -p "${HOME}/.config"
for dir in $(find ${HERE}/dot.config -mindepth 1 -maxdepth 1 -type d); do
source="${dir}"
target="${HOME}/.config/$(basename "${dir}")"
backup_and_link "${source}" "${target}"
done
}
link:dot_local_bin() {
local source target
mkdir -p "${HOME}/.local/bin"
for f in $(find ${HERE}/dot.local/bin -mindepth 1 -maxdepth 1 -type f); do
source="${f}"
target="${HOME}/.local/bin/$(basename "${f}")"
backup_and_link "${source}" "${target}"
done
}
link:dot_local_share() {
local source target
mkdir -p "${HOME}/.local/share"
for dir in $(find ${HERE}/dot.local/share -mindepth 1 -maxdepth 1 -type d); do
source="${dir}"
target="${HOME}/.local/share/$(basename "${dir}")"
backup_and_link "${source}" "${target}"
done
}
link:all() {
link:dotfiles
link:dot_config
link:dot_local_bin
link:dot_local_share
link dot.config/vim .vim
}
is_phony_target() {
for name in "${PHONY[@]}"; do
[[ ${name} == "$1" ]] && return 0
done
return 1
}
usage() {
IFS=\|
echo "usage: $0 <${PHONY[*]}>" >&2
exit 64 # EX_USAGE
}
HERE="$(cd "$(dirname "$0")"; pwd -P)" # ugly but portable
PHONY=(dotfiles dot_config dot_local_bin dot_local_share)
PHONY+=(all)
if (($# != 1)) || ! is_phony_target "$1"; then
usage
fi
link:$1