-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·311 lines (258 loc) · 5.57 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/sh
# install compare update
list_sed=''
list_copy=''
list_x11=''
list_x11_sed=''
SED() {
list_sed="${list_sed} $*"
}
COPY() {
list_copy="${list_copy} $*"
}
X11() {
list_x11="${list_x11} $*"
}
X11_SED() {
list_x11_sed="${list_x11_sed} $*"
}
CAT() {
local f
rm -f "${2}"
printf '! .%s\n\n' "${2}" >> "${2}"
for f in "${src}/${1}"/*.res
do
cat "${f}" >> "${2}"
printf '\n\n\n' >> "${2}"
done
}
# list the files to install
# SED substitutes @NAME@ type of entries
# COPY just copies the file
SED git/config
COPY git/global-ignore
COPY git/global-attributes
COPY bash_aliases
#COPY joverc
COPY mg
#COPY tcshrc
COPY zshrc
COPY lesskey
# X11 files to be copied to ${HOME}
X11 xbindkeysrc
X11 XCompose
X11 Xdefaults
X11 xinitrc
X11 xmobarrc
X11 xmonad/config.hs
X11 xmonad/build
X11_SED dunst/dunstrc
X11 xprofile
X11 Xresources
X11 xsession
# end of list
# ===========
ERROR() {
printf 'error: '
printf "${@}"
printf '\n'
exit 1
}
VERBOSE() {
[ X"${verbose}" = X"yes" ] && printf "$@"
}
USAGE() {
if [ -n "${1}" ]
then
printf 'error: '
printf "${@}"
printf '\n'
fi
cat <<EOF
usage: ${0##*/} <options>
--help -h this message
--verbose -v more messages
--prefix=<dir> -p <dir> set installation directory [${prefix}]
--non-interactive -n no interactive input
--bin -b also include bin/* to ${HOME}/bin
--copy -c copy files, default is to diff -u
--suppress -s suppress dotfiles
--x11 -x also install X11 configs
--debug -D show debug information
EOF
exit 1
}
# main program
verbose=no
debug=no
prefix="${HOME}"
interactive=yes
dotfiles=yes
bin=no
copy='diff -su'
x11=no
src=$(dirname "$0")
# parse options
while getopts :hvnbcp:sxD-: option
do
# convert long options
if [ X"${option}" = X"-" ]
then
option="${OPTARG%%=*}"
OPTARG="${OPTARG#${option}}"
OPTARG="${OPTARG#=}"
fi
case "${option}" in
(v|verbose)
verbose=yes
;;
(n|non-interactive)
interactive=no
;;
(b|bin)
bin=yes
;;
(c|copy)
copy='cp -p'
;;
(p|prefix)
prefix="${OPTARG}"
;;
(s|suppress)
dotfiles=no
;;
(x|x11)
x11=yes
;;
(--)
break
;;
(D|debug)
debug=yes
;;
(h|help)
USAGE
;;
('?')
USAGE 'invalid option: -%s' "${OPTARG}"
;;
(*)
USAGE 'invalid option: --%s' "${option}"
;;
esac
done
shift $((OPTIND - 1))
# verify arguments
[ ${#} -ne 0 ] && USAGE 'extraneous arguments: %s' "${*}"
# enable debugging
[ X"${debug}" = X"yes" ] && set -x
printf 'this will install the files to: %s\n' "${prefix}"
# set to an '#' if do not have the item
# to allow commenting out lines in scripts
have_home=
[ -z "${prefix}" ] && have_home='#'
tempfile=
cleanup() {
[ -z "${tempfile}" ] || rm -f "${tempfile}"
rm -f Xresources Xdefaults
}
trap cleanup INT EXIT
# create temporary files
CAT Xresources.d Xresources
CAT Xdefaults.d Xdefaults
interact() {
local junk
[ X"${interactive}" != X"yes" ] && return
if ! read -p 'Enter to "'"${copy}"'" or Ctrl-C to abort: ' junk
then
echo
exit 1
fi
}
# use sed to substitute some @VAR@ by local values
for f in ${list_sed}
do
tf="$(printf '%s' -- "${f}" | tr '/' '__')"
if ! tempfile="$(mktemp -q "/tmp/${tf}.XXXXXXXX")"
then
ERROR 'cannot create temp file for "%s"' "${f}"
fi
cfg='.config/'
[ X"${f}" = X"${f%/*}" ] && cfg='.'
d="${prefix}/${cfg}${f}"
printf '\033[1;31mSubstitute "%s" to "%s"\033[0m\n' "${f}" "${d}"
sed "s,@HOME@,${prefix}/,g;
s,@HAVE_HOME@,${have_home},g;
" "${src}/${f}" > "${tempfile}"
interact
${copy} "${tempfile}" "${d}"
rm -f "${tempfile}"
tempfile=
done
if [ X"${dotfiles}" = X"yes" ]
then
# files that are just copied
for f in ${list_copy}
do
cfg='.config/'
[ X"${f}" = X"${f%/*}" ] && cfg='.'
d="${prefix}/${cfg}${f}"
printf '\033[1;34mCopy "%s" to "%s"\033[0m\n' "${f}" "${d}"
interact
${copy} "${src}/${f}" "${d}"
done
# desktop files to .local
dst="${HOME}/.local/share/applications/"
mkdir -p "${dst}"
for f in "${src}"/*.desktop
do
bn="${f##*/}"
[ -e "/usr/local/share/applications/${bn}" ] && continue
[ -e "/usr/share/applications/${bn}" ] && continue
printf '\033[1;35mCopy "%s" to "%s"\033[0m\n' "${bn}" "${dst}"
interact
${copy} "${f}" "${dst}"
done
fi
# handle X11 files
if [ X"${x11}" = X"yes" ]
then
for f in ${list_x11}
do
d="${prefix}/.${f}"
printf '\033[1;32mCopy "%s" to "%s"\033[0m\n' "${f}" "${d}"
interact
${copy} "${src}/${f}" "${d}"
done
for f in ${list_x11_sed}
do
tf="$(printf '%s' -- "${f}" | tr '/' '__')"
if ! tempfile="$(mktemp -q "/tmp/${tf}.XXXXXXXX")"
then
ERROR 'cannot create temp file for "%s"' "${f}"
fi
cfg='.config/'
[ X"${f}" = X"${f%/*}" ] && cfg='.'
d="${prefix}/${cfg}${f}"
printf '\033[1;31mSubstitute "%s" to "%s"\033[0m\n' "${f}" "${d}"
sed "s,@HOME@,${prefix}/,g;
s,@HAVE_HOME@,${have_home},g;
" "${src}/${f}" > "${tempfile}"
interact
${copy} "${tempfile}" "${d}"
rm -f "${tempfile}"
tempfile=
done
fi
# handle bin files
if [ X"${bin}" = X"yes" ]
then
for f in "${src}/bin"/[a-zA-Z0-9]*
do
d="${prefix}/bin/${f##*/}"
[ -f "${d}" ] || continue
printf '\033[1;34mCopy "%s" to "%s"\033[0m\n' "${f}" "${d}"
interact
${copy} "${src}/${f}" "${d}"
done
fi