-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
374 lines (312 loc) · 10.1 KB
/
Makefile
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# ========================================================================
# Copyright (c) 2014-2018 T. R. Burghart.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# ========================================================================
#
# Installs shell configuration files.
#
# Requires GNU Make functionality and a Unix-y environment.
#
# Builds a proper profile/rc structure and removes leftover gunk.
#
# The result looks like:
# /etc/bashrc -> /etc/shrc *
# /etc/profile
# /etc/shrc
# /etc/zprofile -> /etc/profile
# /etc/zshrc -> /etc/shrc
# /usr/local/etc/sh.aliases
# /usr/local/etc/sh.rc
# /usr/local/etc/sh.terminal
# ~/.profile
# ~/.shrc
# ~/.bash_profile -> ~/.profile
# ~/.bashrc -> ~/.shrc
# ~/.kshrc -> ~/.shrc
# ~/.zprofile -> ~/.profile
# ~/.zshrc -> ~/.shrc
#
# * /etc/bashrc gets special handling
# It's not a recognized startup file for bash, but its presence has been
# co-opted by enough default system configurations that I replace it so
# that if it does get sourced at least I'll control what it contains.
#
# Used if present:
# /etc/profile.d/*.sh
# /usr/local/etc/sh.*
#
# !!! Removed if present:
#
# /etc/kshrc
# /etc/zshenv
# /etc/profile.d/zzz_local.sh
# /usr/local/etc/sh.local
# /usr/local/etc/bash.term.prompt
# /usr/local/etc/zsh.term.prompt
# ~/.zshenv
#
home_deprecated := $(wildcard $(HOME)/.zshenv)
root_deprecated := $(wildcard \
/etc/kshrc \
/etc/zshenv \
/etc/profile.d/zzz_local.sh \
/usr/local/etc/sh.local \
/usr/local/etc/bash.term.prompt \
/usr/local/etc/zsh.term.prompt)
SHELL := /bin/bash
export SHELL
prjdir := $(CURDIR)
uname := $(or $(wildcard /bin/uname), \
$(wildcard /usr/bin/uname), \
$(error Can't find system uname program))
stype := $(or $(shell $(uname) -s 2>/dev/null \
| /usr/bin/tr '[A-Z]' '[a-z]' 2>/dev/null), \
$(error Can't determine system type))
uid := $(or $(shell /usr/bin/id -u 2>/dev/null), \
$(error Can't figure out your User ID))
gid := $(or $(shell /usr/bin/id -g 2>/dev/null), \
$(error Can't figure out your Group ID))
unm := $(or $(shell /usr/bin/id -un 2>/dev/null), \
$(error Can't figure out your User Name))
gnm := $(or $(shell /usr/bin/id -gn 2>/dev/null), \
$(error Can't figure out your Group Name))
INSTALL := $(or $(wildcard /usr/gnu/bin/install), \
$(wildcard /usr/ucb/install), \
$(wildcard /usr/bin/install), \
$(error Can't find compatible install program))
# whether to install files to /usr/local/bin
lclinst := false
ifeq ($(stype),sunos)
sys_usr := root
sys_grp := root
else
sys_usr := 0
sys_grp := 0
endif
ifeq ($(stype),darwin)
adm_grp := admin
else
adm_grp := $(sys_grp)
endif
ifeq ($(stype),darwin)
ifneq ($(uid),0)
lclinst := true
endif
else
ifeq ($(uid),0)
lclinst := true
endif
endif
# try to get the actual login name, for the test below
usrname := $(or $(LOGNAME),$(USER),$(shell /usr/bin/id -un 2>/dev/null), \
$(error Can't figure out your login name))
# only install to root's home directory if there's no alternate superuser
# on my systems, by convention that's 'toor'
ifeq ($(uid)/$(usrname),0/root)
no_home := $(shell /usr/bin/id -u toor 2>/dev/null)
endif
ifeq ($(shell test -d /etc/profile.d || echo no),)
ifneq ($(shell egrep -qw pathmunge /etc/profile.d/* 2>/dev/null || echo no),no)
$(warning Files in /etc/profile.d use pathmunge)
endif
prof_d := true
else
prof_d := false
endif
dl_cmd := $(shell type -p curl 2>/dev/null || true)
ifneq ($(wildcard $(dl_cmd)),)
dl_cmd += -SsLRo
else
dl_cmd := $(shell type -p wget 2>/dev/null || true)
ifneq ($(wildcard $(dl_cmd)),)
dl_cmd += -qNO
else
undefine dl_cmd
$(warning Need curl or wget to download files)
endif
endif
# counting on install unlinking the destination file before copying
INSTADM := $(INSTALL) -o $(uid) -g $(adm_grp)
INSTSYS := $(INSTALL) -o $(sys_usr) -g $(sys_grp)
INSTUSR := $(INSTALL) -o $(uid) -g $(gid)
LNADM := /bin/ln -fs
LNSYS := /bin/ln -fs
LNUSR := /bin/ln -fs
ifneq ($(uid),0)
INSTSYS := @echo $(INSTSYS)
LNSYS := @echo $(LNSYS)
endif
ifneq ($(adm_grp),$(adm_grp))
INSTAF := $(INSTADM) -m 0664
INSTAL := $(LNADM)
INSTAX := $(INSTADM) -m 0775
else
INSTAF := $(INSTADM) -m 0644
INSTAL := $(LNADM)
INSTAX := $(INSTADM) -m 0755
endif
INSTSF := $(INSTSYS) -m 0644
INSTSL := $(LNSYS)
INSTSX := $(INSTSYS) -m 0755
INSTUF := $(INSTUSR) -m 0644
INSTUL := $(LNUSR)
INSTUX := $(INSTUSR) -m 0755
# files to be installed from downloads defined elsewhere
dl_tgts := /usr/local/bin/rmate /usr/local/bin/rebar /usr/local/bin/rebar3
# these will be wildcard patterns, sort to remove duplicates
sh_etc := $(sort profile ksh.kshrc bashrc kshrc shrc zshenv zprofile zshrc)
sh_loc := $(sort sh.* env.local)
sh_usr := $(sort $(filter-out ksh.kshrc, $(sh_etc)) \
bash_profile *login *logout)
h_srcs := $(wildcard $(foreach pat, $(sh_usr), \
$(prjdir)/home/$(stype)/$(pat) \
$(prjdir)/home/$(pat) ))
r_srcs := $(wildcard $(foreach pat, $(sh_etc), \
$(prjdir)/root/$(stype)/etc/$(pat) \
$(prjdir)/root/etc/$(pat) ))
r_srcs += $(wildcard $(foreach pat, $(sh_loc), \
$(prjdir)/root/$(stype)/usr/local/etc/$(pat) \
$(prjdir)/root/usr/local/etc/$(pat) ))
ifeq ($(prof_d),true)
r_srcs += $(wildcard $(foreach pat, $(sh_loc), \
$(prjdir)/root/$(stype)/etc/profile.d/$(pat) \
$(prjdir)/root/etc/profile.d/$(pat) ))
endif
# pattern substitutions are nested to avoid system-specific
# paths winding up as targets
h_tgts := $(patsubst $(prjdir)/home/%, $(HOME)/.%, \
$(patsubst $(prjdir)/home/$(stype)/%, $(HOME)/.%, $(h_srcs)))
r_tgts := $(patsubst $(prjdir)/root/%, /%, \
$(patsubst $(prjdir)/root/$(stype)/%, /%, $(r_srcs)))
# these will get symlinked if source files don't exist
# the definitive list is in the Makefile header
h_tgts += $(foreach fn, bash_profile zprofile bashrc kshrc zshrc, $(HOME)/.$(fn))
r_tgts += $(foreach fn, zprofile bashrc zshrc, /etc/$(fn))
# sort to remove duplicates
h_tgts := $(sort $(h_tgts))
r_tgts := $(sort $(r_tgts))
.PHONY: default home root touch vars
ifeq ($(no_home),)
default :: home
endif
default :: root
touch ::
@find $(prjdir)/root $(prjdir)/home -type f -exec touch {} \;
# for debugging
vars ::
@$(MAKE) -Rrnp -C $(prjdir) \
| egrep '^[[:alnum:]_]+[[:space:]]*:?=' | sort
ifneq ($(home_deprecated),)
home ::
@echo '***' Clean up manually with command:
@echo /bin/rm $(home_deprecated)
endif
ifneq ($(root_deprecated),)
root ::
@echo '***' Clean up manually with command:
@echo /bin/rm $(root_deprecated)
endif
home :: $(h_tgts)
@echo '***' Home directory layout:
@/bin/ls -lh $(sort $(wildcard \
$(HOME)/.*shrc $(HOME)/.*profile \
$(HOME)/.*login $(HOME)/.*logout \
$(h_tgts) $(home_deprecated) ))
ifeq ($(lclinst),true)
home :: $(dl_tgts)
endif
ifneq ($(uid),0)
root ::
@echo '***' Root commands must be executed manually.
endif
root :: $(r_tgts)
@echo '***' Root directory layout:
@/bin/ls -lh $(sort $(wildcard $(r_tgts) $(root_deprecated)))
# for each target directory, single-colon rules for the the
# system-specific source first, then the generic source
# gmake will use the first match only with single-colon rules
/etc/% : $(prjdir)/root/$(stype)/etc/%
$(INSTSF) $< $@
ifeq ($(prof_d),true)
/etc/% : $(prjdir)/root/etc/%.profile.d
$(INSTSF) $< $@
endif
/etc/% : $(prjdir)/root/etc/%
$(INSTSF) $< $@
/usr/local/etc/% : $(prjdir)/root/$(stype)/usr/local/etc/%
@test -d $(@D) || /bin/mkdir -p $(@D)
$(INSTSF) $< $@
/usr/local/etc/% : $(prjdir)/root/usr/local/etc/%
@test -d $(@D) || /bin/mkdir -p $(@D)
$(INSTSF) $< $@
$(HOME)/.% : $(prjdir)/home/$(stype)/%
$(INSTUF) $< $@
$(HOME)/.% : $(prjdir)/home/%
$(INSTUF) $< $@
# create symlinks for files that don't have sources
# NO wildcards - too much chance to screw up!
# the definitive list is in the Makefile header
/etc/bashrc : /etc/shrc
$(INSTSL) $(<F) $@
/etc/zprofile : /etc/profile
$(INSTSL) $(<F) $@
/etc/zshrc : /etc/shrc
$(INSTSL) $(<F) $@
$(HOME)/.bash_profile : $(HOME)/.profile
$(INSTUL) $(<F) $@
$(HOME)/.zprofile : $(HOME)/.profile
$(INSTUL) $(<F) $@
$(HOME)/.bashrc : $(HOME)/.shrc
$(INSTUL) $(<F) $@
$(HOME)/.kshrc : $(HOME)/.shrc
$(INSTUL) $(<F) $@
$(HOME)/.zshrc : $(HOME)/.shrc
$(INSTUL) $(<F) $@
# downloaded files are always installed from a temporary copy
# so their timestamps can be checked
ifeq ($(dl_cmd),)
$(warning Skipping download/check of $(dl_tgts))
else
escript := $(or $(shell type -P escript 2>/dev/null || true), \
$(lastword $(sort $(wildcard \
$(if $(LOCAL_OTP_DIR),$(LOCAL_OTP_DIR),/opt/erlang)/otp-??-ga/bin/escript))))
/usr/local/bin/% : /tmp/ledl/%
@test -d $(@D) || /bin/mkdir -p $(@D)
$(INSTAX) -p $< $@
/tmp/ledl/rmate :
@test -d $(@D) || /bin/mkdir -p $(@D)
@echo Downloading $@ ...
@$(prjdir)/bin/github.download textmate rmate bin/rmate $@
# if no escript, sets the hard-coded timestamp from rebar v2.6.4
/tmp/ledl/rebar :
@test -d $(@D) || /bin/mkdir -p $(@D)
@echo Downloading $@ ...
@$(dl_cmd) $@ https://github.com/rebar/rebar/wiki/rebar
@echo '#!/bin/bash -e' >[email protected]
@echo 'if [[ -z "$$2" ]]' >>[email protected]
@echo 'then' >>[email protected]
@echo ' echo Warning: Missing escript, using hard-coded Rebar file time >&2' >>[email protected]
@echo ' touch -t 201608311451.36 "$$1"' >>[email protected]
@echo ' exit $$?' >>[email protected]
@echo 'fi' >>[email protected]
@echo 'ts="$$("$$2" "$$1" -V | tr -d _ | \' >>[email protected]
@echo " awk '{print substr(\$$4,1,12) \".\" substr(\$$4,13,14)}')\"" >>[email protected]
@echo 'touch -t "$$ts" "$$1"' >>[email protected]
@chmod +x [email protected]
[email protected] "$@" "$(escript)"
/tmp/ledl/rebar3 :
@test -d $(@D) || /bin/mkdir -p $(@D)
@echo Downloading $@ ...
@$(dl_cmd) $@ https://s3.amazonaws.com/rebar3/rebar3
endif