aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: f07d9d62ed8388cc4c97d556203e914ccf0cdb76 (plain) (blame)
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
#!gmake -f
SUBSTS_FILE=SUBSTS.local

# Files that need changes to work locally or that contain sensitive
# information
LOCAL_FILES = \
    .devscripts \
    .gitconfig \
    .imapfilter/config.lua \
    .msmtprc \
    .mutt/accounts.rc \
    .mutt/gpg.rc \
    .mutt/score.rc \
    .mutt/ssl.rc \
    .muttrc \
    .netrc \
    .offlineimaprc \
    .screenrc \
    .xinitrc \
    .xmonad/xmonad.hs \
    .zsh/func/prompt_wunjo_setup \
    .zshrc \
    bin/gpg-wrapper \
    bin/run-mailcheck

# GPG encrypted files
GPG_FILES = \
    .mutt/alias.rc \
    .ssh/id_ecdsa \
    .ssh/id_rsa \
    .ssh/id_rsa.lambda

# Files that are system independent.
# IMPORTANT: directories must have trailing slash
GLOBAL_FILES = \
    .Xdefaults \
    .Xsession \
    .asoundrc \
    .beetsconfig \
    .calendar/ \
    .config/nitrogen/ \
    .config/zathura/ \
    .cmus/ \
    .dput.cf \
    .dzen/ \
    .emacs \
    .emacs.d \
    .fonts.conf \
    .gitconfig \
    .imapfilter/ \
    .inputrc \
    .local/share/wallpapers/ \
    .mailcap \
    .mailcheckrc \
    .mutt/ \
    .mutt/alias.rc \
    .muttrc \
    .notmuch-config \
    .offlineimap.py \
    .pythonrc.py \
    .quiltrc-dpkg \
    .reportbugrc \
    .sbuildrc \
    .screenlayout/ \
    .screenrc-mail \
    .signature \
    .ssh/ \
    .tmux/ \
    .tmux.conf \
    .tmux-mail.conf \
    .tmux-powerlinerc \
    .urxvt/ \
    .vim/ \
    .vimperatorrc \
    .vimrc \
    .vm \
    .xinitrc \
    .xkb/ \
    .xmobarrc \
    .xmonad/ \
    .zsh/ \
    .zlogout \
    bin/ \

get-val = $(shell awk '{if (match($$0, /$1/)) { print $$2 } }' $(SUBSTS_FILE))
get-sed-args = $(foreach var,$($(1)),-e 's|$(var)|$(call get-val,$(var))|g')

CURRENT_BRANCH = $(shell git branch --no-color | colrm 1 2)

SHA256        = $(call get-val,SHA256)
GPG_DISABLED  = $(call get-val,GPG_DISABLED)
GPG_BINARY    = $(call get-val,GPG_BINARY)
EMACS_DISABLED= $(call get-val,EMACS_DISABLED)

VARS_.devscripts        = MSMTP_PATH
VARS_.gitconfig         = MSMTP_PATH
VARS_.imapfilter/config.lua = LOCAL_PASS PM_EMAIL IMAPFILTER_GMAIL_SERVER GMAIL_PASS IMAP_FOLDER_SEP
VARS_.msmtprc           = GMAIL_PASS QUEENSU_PASS LOCALHOST MCGSOCS_PASS MCGILL_PASS RYANAKCA_PASS SSL_CERTS
VARS_.mutt/accounts.rc  = LOCAL_PASS GMAIL_PASS QUEENSU_PASS MSMTP_PATH MCGILL_PASS
VARS_.mutt/gpg.rc       = GPG_BINARY PGPEWRAP_BINARY
VARS_.mutt/score.rc     = MUTT_10_SCORE MUTT_20_SCORE
VARS_.mutt/ssl.rc       = SSL_CERTS
VARS_.muttrc            = MSMTP_PATH
VARS_.netrc             = LOCAL_PASS GMAIL_PASS
VARS_.offlineimaprc     = LOCAL_PASS GMAIL_PASS QUEENSU_PASS MCGSOCS_PASS MCGILL_PASS SSL_CERTS
VARS_.screenrc          = ZSH_PATH SCREEN_HOST_COLOUR
VARS_.xinitrc           = SCREENLAYOUT REDSHIFT_MODE GSD_PATH PULSE_JACKD
VARS_.xmonad/xmonad.hs  = XMONAD_DZEN_W XMONAD_DZEN_X XMONAD_DZEN_Y
VARS_.zsh/func/prompt_wunjo_setup = ZSH_HOST_COLOUR
VARS_.zshrc             = LOCALE SUBSTS_RM SUBSTS_LS MSMTP_PATH KEYCHAIN
VARS_bin/gpg-wrapper    = GPG_BINARY
VARS_bin/run-mailcheck  = ZSH_PATH GREP_PATH

all: clean build

# This target relies on GLOBAL_FILES being before LOCAL_FILES so that the
# build/LOCAL_FILES targets overwrite what was copied in GLOBAL_FILES.
BUILD = $(patsubst %,build/%,$(GLOBAL_FILES) $(LOCAL_FILES) $(GPG_FILES))

build: $(BUILD) emacsen fonts build/bin/msmtp/msmtp-default

# We must force these with a phony target, otherwise, make will see that they're
# already there (for example, from installing the rest of .mutt or .zsh) and
# will skip them---which means they don't get their substitutions
build/.imapfilter/config.lua: FORCE
build/.mutt/accounts.rc: FORCE
build/.xmonad/xmonad.hs: FORCE
build/.zsh/func/prompt_wunjo_setup: FORCE
FORCE:

.mutt/alias.rc: gpg/.mutt/alias.rc.gpg
.ssh/id_%: gpg/.ssh/id_%.gpg
# $(patsubst gpg/,,$(wildcard gpg/.* gpg/*))
$(GPG_FILES):
	touch $@ && chmod 600 $@
	[ "$(GPG_DISABLED)" = "True" ] || $(GPG_BINARY) --decrypt gpg/$@.gpg > $@

emacsen:
	[ "$(EMACS_DISABLED)" = "True" ] || $(MAKE) -C $@
	[ "$(EMACS_DISABLED)" = "True" ] || $(MAKE) -C $@ install

fonts:
	$(MAKE) -C .fonts install

build/bin/msmtp/msmtp-default: build/.msmtprc
	-mkdir -p $(dir $@)
	awk '/account/ { FNAME =  "$(dir $@)/msmtp-"$$2; print "#!/bin/sh" > FNAME ; print "$(call get-val,MSMTP_PATH) -a " $$2 " \"$$@\"" >> FNAME }' $<
	chmod 755 build/bin/msmtp/*

build/%: % $(SUBSTS_FILE)
	[ -d $(dir $@) ] || mkdir -p $(dir $@)
	( [ -d $< ] && rsync -a $</* $@/ ) || rsync -a $< $@
	@# sed will only be called if $* is in LOCAL_FILES.
	@# Thought of using ifeq/ifneq, but the gmake manual reads:
	@# "make evaluates conditionals when it reads a makefile. Consequently,
	@# you cannot use automatic variables in the tests of conditionals
	@# because they are not defined until recipes are run (see Automatic
	@# Variables)."
	[ "$(filter $*,$(LOCAL_FILES))" != "$*" ] || sed $(call get-sed-args,VARS_$*) $< > $@

install: build
	rsync -a build/ ~
	chmod 600 ~/.msmtprc ~/.netrc ~/.ssh/id_*
	chmod 700 ~/.ssh
	fc-cache ~/.fonts

sha256sums: .git/refs/heads/$(CURRENT_BRANCH)
	$(SHA256) `git ls-files | grep -v $@` > $@
	[ $(SHA256) = 'sha256' ] || awk '{ \
	    sum = $$1; \
	    $$1 = ""; \
	    # We want (filename), not ( filename). \
	    gsub(/^ /,""); \
	    print "SHA256 (" $$0 ") = " sum; \
	    }' $@ > $@.temp
	mv $@.temp $@

sha256sums.asc: sha256sums
	rm -f $@
	$(GPG_BINARY) --armor --detach-sign $<

merge: SUBSTS $(SUBSTS_FILE)
	# sdiff has exit status 1 if files are different. Ignore
	- sdiff -o SUBSTS.merged $^
	@echo ""
	@echo "Please review SUBSTS.merged, then run"
	@echo "mv SUBSTS.merged $(SUBSTS_FILE)"

verify:
	# BSD sha256 sum command doesn't have a -c option.
	# BSD and coreutils sha256 commands have different outputs, however, the
	# file is always in field two. The sum's location varies.
	awk --posix 'BEGIN {\
		mismatch_count = 0; \
		match_count = 0; \
	    } { \
		# The file associated with the sum we`re checking \
		# Appears to be field 2 in BSD and coreutils sum \
		file = $$2; \
		# We want `filename`, not `(filename)` \
		sub(/^\(/, "", file); \
		sub(/\)$$/, "", file); \
		# Generate the sum to compare with \
		cmd = "$(SHA256) " file; \
		cmd | getline gensum; \
		close(cmd); \
		split(gensum, gensum_fields); \
		gensum = ""; \
		for (field in gensum_fields) { \
		    if (gensum_fields[field] ~ /[[:xdigit:]]{64}/) { \
			gensum = gensum_fields[field]; \
		    } \
		} \
		for (i = 1; i <= NF; i++) { \
		    if ($$i ~ /[[:xdigit:]]{64}/) { \
			filesum = $$i; \
		    } \
		} \
		if (filesum == gensum) { \
		    print "Match: " file; \
		    match_count += 1; \
		} else { \
		    print "Mismatch: " file; \
		    mismatch_count += 1; \
		} \
	    } END { \
		print match_count, "matches, ", mismatch_count, "mismatches." \
	}' sha256sums
	$(GPG_BINARY) --verify sha256sums.asc

udh: udh-master

# As a backup for when master changes
udh-%:
	# -L to follow symlinks
	rsync -avzL $(@:udh-%=%).debian.org:/var/lib/misc/ssh_known_hosts .ssh/known_hosts.d/debian

clean:
	rm -fr build
	$(MAKE) -C emacsen clean

clobber: clean
	rm -f $(GPG_FILES)
	$(MAKE) -C emacsen clobber

.PHONY: build install clean verify merge udh emacsen