-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·75 lines (59 loc) · 1.45 KB
/
install
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
#!/bin/sh
# TODO: add -v for debugging
# TODO: only install the supplied argument if supplied
beverbose=1
# comment_line checks if a line is a comment 🐸
comment_line() {
case "$1" in
\#*) return 0 ;;
esac
return 1
}
# empty_line checks if a line is empty 😆
empty_line() {
test -z "$1" && return 0
return 1
}
# log prints information to stderr
log() {
local msg="$1"
printf '%s\n' "$msg" 1>&2
}
# error logs the error and exit immediately
error() {
printf '\033[1;31m'
log "$1"
printf '\033[0m'
exit 1
}
symlink() {
ln -nsf "$1" "$2"
}
main() {
cd "${0%/*}" || error "could not change directory to ${0%/*}"
counter=0
while read -r line; do
counter=$((counter + 1))
comment_line "$line" && continue
empty_line "$line" && continue
read -r src dst _ <<-EOL
$line
EOL
if ! test -e "$src"; then
error "line $counter: $src: no such file or directory"
fi
if test -z "$dst"; then
error "Error on line $counter: destination is missing"
fi
src="$PWD/$src"
dst="$HOME/$dst"
# make sure the parent directory exists
if ! test -d "${dst%/*}"; then
test $beverbose && log "CREATING A DIRECTORY ${dst%/*} FOR $src"
mkdir -p "${dst%/*}"
fi
test $beverbose && log "SYMLINK $src AT $dst"
symlink "$src" "$dst"
done <rules.txt
}
main "$@"