-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.sh
executable file
·97 lines (79 loc) · 1.84 KB
/
manage.sh
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
#!/bin/sh
set -e
main() {
if [ $# != 2 ]; then
print_usage
exit 2
fi
case "$1" in
collect)
collect_dotfiles "$2"
;;
deploy)
deploy_dotfiles "$2"
;;
*)
print_usage
exit 2
;;
esac
}
print_usage() {
echo "Usage:
$0 <collect|deploy> CONFIG
CONFIG is a list of all files (not directories) that are meanth
to be stored as dotfile configuration. Listing a path means that
collect/deploy will manage this file.
Managing directories is not allowed, so that a sensitive files
are not included by a mistake. Each file must be explicitly
mentioned.
"
}
collect_dotfiles() {
dst=$(dirname "$(realpath "$1")")
while read -r line; do
# Expand any variable, so that i.e. $HOME can be used.
src=$(eval "echo $line")
if [ -z "$src" ]; then
continue
fi
if [ ! -r "$src" ]; then
logerr "$src does not exist or cannot be accessed"
elif [ -f "$src" ]; then
collect_file "$dst" "$src"
elif [ -d "$src" ]; then
logerr "$src is a directory. Collecting a directory is not allowed."
else
logerr "$src cannot be collected."
fi
done <"$1"
}
collect_file() {
dst_root=$1
src=$2
dst_path=${src#"$HOME/"}
mkdir -p "$dst_root/$(dirname "$dst_path")"
cp "$src" "$dst_root/$dst_path"
chmod +r "$dst_root/$dst_path"
}
logerr() {
echo >&2 "ERR" "$@"
}
deploy_dotfiles() {
src_root=$(dirname "$(realpath "$1")")
while read -r line; do
# Expand any variable, so that i.e. $HOME can be used.
dst=$(eval "echo $line")
if [ -z "$dst" ]; then
continue
fi
src=${dst#"$HOME"}
mkdir -p "$dst/$(dirname "$src")"
# Since collecting is easy, it is better to make a copy than to link and
# edit the dotfile as well by a mistake.
#
#ln -r "$src_root$src" "$dst"
cp "$src_root$src" "$dst"
done <"$1"
}
main "$@"