forked from peikk0/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·137 lines (116 loc) · 2.52 KB
/
install.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
# Install symlinks for a set of dotfiles
set -e
# Assume lite set by default, this is will be the most commonly used set
DEFAULT_SET=lite
READLINK=readlink
if type greadlink >/dev/null; then
READLINK=greadlink
fi
usage() {
echo "$0 [options] [<set>]" >&2
echo " -d <destdir> # destination directory (default: ${HOME})"
echo " -f # force overwriting existing files"
echo " -q # quiet"
echo " -h # help"
echo "Default set: ${DEFAULT_SET}"
}
usage_error() {
echo "Error: $@" >&2
usage
exit 1
}
help() {
usage
exit 0
}
error() {
echo "Error: $@" >&2
}
puts() { [ "${quiet}" -eq 1 ] && return
echo $@
}
load_set() {
set_file="${sets}/${set}"
if ! [ -f "${set_file}" ]; then
error "Set ${set} does not exist!"
exit 1
fi
sed -e '/^$/d' -e '/^#/d' "${set_file}"
}
install() {
file_dir="$(dirname "$1")"
if ! [ -d "${destdir}/${file_dir}" ]; then
makedirs "${destdir}/${file_dir}"
fi
symlink "$1"
}
makedirs() {
puts "MAKEDIRS $1"
mkdir -p "$1"
}
symlink() {
# avoid a catastrophic rm -rf $HOME
[ -n "$1" ] || return
[ -n "${destdir}" ] || return
if [ -e "${destdir}/$1" ]; then
if [ -h "${destdir}/$1" ] && \
[ $(${READLINK} "${destdir}/$1") = "${dotfiles}/$1" ]; then
puts "OK $1"
return
elif [ "${force}" -ne 1 ]; then
puts "SKIP $1 (use -f to force)"
return
fi
fi
puts "SYMLINK $1 -> ${destdir}/$1"
rm -rf "${destdir}/$1"
ln -snf "${dotfiles}/$1" "${destdir}/$1"
}
dotfiles="$(${READLINK} -f $(dirname $0))"
sets="${dotfiles}/sets"
set="${DEFAULT_SET}"
force=0
quiet=0
destdir="${HOME}"
while [ $# -ne 0 ]; do
case $1 in
-d)
shift
if [ $# -eq 0 ]; then
usage_error "Missing argument for -d"
fi
destdir="$1"
;;
-f)
force=1
;;
-q)
quiet=1
;;
-h)
help
;;
-*)
usage_error "Unknown option: $1"
;;
*)
set=$1; shift
if [ $# -ne 0 ]; then
usage_error "Unexpected extra arguments: $@"
fi
break
;;
esac
shift
done
puts "Set: ${set}"
puts "Destination directory: ${destdir}"
puts
files=$(load_set "${set}")
for file in $files; do
install "${file}"
done
puts
puts "Done."
exit 0