-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup
executable file
·188 lines (151 loc) · 3 KB
/
setup
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/sh
DOT_FILES="
vimrc
tmux.conf
"
main()
{
[ $# -gt 1 ] && quit Surplus argument \""$2"\"
[ $# -eq 0 ] && arg='<default>' || arg=$1
case $arg in
'<default>')
undo_all
setup_all
;;
rm)
undo_all
;;
help | -h | --help)
show_help
;;
*)
quit Unknown argument \""$1"\"
;;
esac
}
setup_all()
{
log Installing setup ...
setup_dot_files
setup_shrc
setup_git_config
log
}
undo_all()
{
log Removing setup ...
undo_dot_files
undo_shrc
undo_git_config
log
}
setup_dot_files()
{
printf "%s" "$DOT_FILES" | grep [[:graph:]] | while read filename
do
target="$PWD/_$filename"
link=~/."$filename"
if [ -f "$link" ]
then
err Link "$link" already exists
elif [ ! -f "$target" ]
then
err File "$target" does not exist
elif ln -s "$target" "$link"
then
log Created "$link -> $target"
fi
done
}
undo_dot_files()
{
printf "%s" "$DOT_FILES" | grep [[:graph:]] | while read filename
do
link=~/."$filename"
if [ ! -f "$link" ]
then
err Link "$link" does not exist
elif rm "$link"
then
log Deleted "$link"
fi
done
}
# Comment to mark lines inserted by this script.
SETUP_MARK='# Added by setup script'
setup_shrc()
{
shrc="$PWD/shrc"
cmd="[ -f \"$shrc\" ] && . \"$shrc\" $SETUP_MARK"
for file in ~/.bashrc ~/.bash_profile ~/.zshrc
do
if [ -f "$file" ]
then
if grep -q "$SETUP_MARK" "$file"
then
err File "$file" is already updated to load shrc
elif echo "$cmd" >> "$file"
then
log Updated "$file" to load shrc
fi
else
log Ignored missing file "$file". \
To setup "$file", create it manually and run setup again.
fi
done
unset shrc
unset cmd
unset file
}
undo_shrc()
{
for file in ~/.bashrc ~/.bash_profile ~/.zshrc
do
if [ -f "$file" ]
then
if grep -q "$SETUP_MARK" "$file"
then
sed "/$SETUP_MARK/d" "$file" > "$file.tmp" &&
mv "$file.tmp" "$file" &&
log File "$file" updated to not load shrc
else
err File "$file" does not load shrc
fi
else
err Ignored missing file "$file"
fi
done
unset file
}
setup_git_config()
{
sh -x ./gitconfig.sh
}
undo_git_config()
{
awk '/^git/ {print $1 " " $2 " " $3 " --unset " $4}' gitconfig.sh | sh -x
}
err()
{
log "$0: $*"
}
log()
{
printf "%s\n" "$*" >&2
}
quit()
{
err "$*"
exit 1
}
show_help()
{
printf "Usage: $0 [rm | help]
Setup files in home directory.
Actions:
<default> Reinstall setup if no action is specified.
rm Remove setup.
help Show this help and exit.
"
}
main "$@"