-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
104 lines (88 loc) · 2.24 KB
/
Copy pathinstall.sh
File metadata and controls
104 lines (88 loc) · 2.24 KB
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
#!/usr/bin/env bash
set -ue
DRY_RUN=0
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
TARGETS_FILE="$SCRIPT_DIR/.linktargets"
helpmsg() {
command echo "Usage: $0 [--help | -h] [--debug | -d] [--dry-run | -n]" 0>&2
command echo ""
command echo "Targets file: $TARGETS_FILE" 0>&2
command echo ""
}
run_cmd() {
if [[ "$DRY_RUN" -eq 1 ]]; then
command printf '[dry-run] %s\n' "$*"
else
"$@"
fi
}
link_target() {
local src="$1"
local dest="$2"
local backup_dir="$3"
if [[ -L "$dest" ]]; then
local current
current="$(readlink "$dest")"
if [[ "$current" == "$src" ]]; then
command echo "skip: $dest already links to $src"
return
fi
command echo "backup symlink: $dest -> $current"
run_cmd mv "$dest" "$backup_dir/"
elif [[ -e "$dest" ]]; then
command echo "backup existing: $dest"
run_cmd mv "$dest" "$backup_dir/"
fi
}
link_to_homedir() {
local dotdir="$SCRIPT_DIR"
local backup_root="$HOME/.dotbackup"
local backup_dir="$backup_root/$(date +%Y%m%d-%H%M%S)"
if [[ "$HOME" != "$dotdir" ]]; then
if [[ ! -f "$TARGETS_FILE" ]]; then
command echo "targets file not found: $TARGETS_FILE" 0>&2
exit 1
fi
command echo "backup old dotfiles..."
if [ ! -d "$backup_root" ]; then
command echo "$backup_root not found. Auto Make it"
run_cmd mkdir -p "$backup_root"
fi
run_cmd mkdir -p "$backup_dir"
local name
while IFS= read -r name || [[ -n "$name" ]]; do
[[ -z "$name" ]] && continue
[[ "$name" =~ ^[[:space:]]*# ]] && continue
local src="$dotdir/$name"
local dest="$HOME/$name"
if [[ ! -e "$src" && ! -L "$src" ]]; then
command echo "skip: source not found: $src"
continue
fi
run_cmd mkdir -p "$(dirname "$dest")"
link_target "$src" "$dest" "$backup_dir"
command echo "link: $dest -> $src"
run_cmd ln -snf "$src" "$dest"
done < "$TARGETS_FILE"
else
command echo "same install src dest"
fi
}
while [ $# -gt 0 ]; do
case ${1} in
--debug | -d)
set -uex
;;
--dry-run | -n)
DRY_RUN=1
;;
--help | -h)
helpmsg
exit 1
;;
*) ;;
esac
shift
done
link_to_homedir
command echo -e "\e[1;36m Install completed!!!! \e[m"