-
Notifications
You must be signed in to change notification settings - Fork 6
/
install.sh
executable file
·72 lines (65 loc) · 1.44 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
#!/bin/bash
script_dir() {
echo $(cd $(dirname $0) && git rev-parse --show-toplevel)
}
run_hook() {
if [ -e "$1" ]; then
(. $1)
fi
}
link_file() {
if [ ! -e "$1" ]; then
return
fi
if [ -d "$1" ]; then
if [ -e "$1/.linkdir" ]; then
if [ -e "$2" ]; then
rm -fr "$2"
fi
ln -sf "$1" "$2"
return
fi
if [ ! -d "$2" ]; then
mkdir -p $2
fi
local list=$4
if [ -z "$list" ]; then
list=$(ls -1 $1)
fi
run_hook "$1/.hook-pre"
for f in $list; do
if ! grep -Fxq "$f" "$1/.exclude" 2>/dev/null; then
link_file "$1/$f" "$2/$3$f"
fi
done
run_hook "$1/.hook-post"
else
if [ -e "$2" ]; then
rm -f $2
fi
ln -sf $1 $2
fi
}
undo() {
echo "Deleting dotfiles.."
FILES=$(find $HOME -maxdepth 4 -type l)
for FILE in $FILES; do
LINK=$(readlink $FILE)
if [[ ${LINK} = ${BASE_DIR}* ]]; then
rm -f $FILE
fi
done
}
UNAME=$(uname)
HOSTNAME=$(hostname -s)
BASE_DIR=$(script_dir)
case $1 in
-u|-uninstall|uninstall)
undo
;;
*)
link_file "$BASE_DIR" "$HOME" "." "$(ls -1 $BASE_DIR | grep -v '^host-\|^uname-')"
link_file "$BASE_DIR/host-${HOSTNAME}" "$HOME" "."
link_file "$BASE_DIR/uname-${UNAME}" "$HOME" "."
;;
esac