1PREFIX?=/usr/local
2prefix=${PREFIX}
3SYSCONFDIR=${prefix}/etc
4INSTALL=./scripts/install-sh
5MKPATH=${INSTALL} -m 755 -d
6INSTALLBIN=${INSTALL} -m 555
7INSTALLFILE=${INSTALL} -m 444
8INSTALLMAN=${INSTALL} -m 444
9INSTALLDOC=${INSTALL} -m 444
10INSTALLCONF=${INSTALL} -m 644
11PANDOC=./scripts/pandoc-sh
12
13DOC_SRC=markdown_src/vimpager.md markdown_src/vimcat.md
14
15GEN_DOCS=man/vimpager.1 man/vimcat.1 html/vimpager.html html/vimcat.html markdown/vimpager.md markdown/vimcat.md
16
17ANSIESC=autoload/AnsiEsc.vim plugin/AnsiEscPlugin.vim plugin/cecutil.vim
18
19RUNTIME=autoload/vimpager.vim autoload/vimpager_utils.vim autoload/vimcat.vim plugin/vimpager.vim macros/less.vim syntax/perldoc.vim ${ANSIESC}
20
21SRC=vimcat ${RUNTIME}
22
23PROGRAMS=vimpager vimcat
24
25all: ${PROGRAMS:=-vertag-stamp} standalone/vimpager standalone/vimcat docs
26
27# set tag from git or ChangeLog
28%-vertag-stamp: %
29	@echo updating version tag in $<
30	@tag=`git tag 2>/dev/null | tail -1`; \
31	[ -z "$$tag" ] && tag=`sed -n '/^[0-9][0-9.]* [0-9-]*:$$/{s/ .*//;p;q;}' ChangeLog_$<.yml`; \
32	if [ -n "$$tag" ]; then \
33		sed -e 's/^version_tag=.*/version_tag='"$$tag"'/' $< > $<.work; \
34		mv -f $<.work $<; \
35	fi
36	@chmod +x $<
37	@touch $@
38
39# other recipes need the version, get it from git describe or ChangeLog
40%-version.txt: %
41	@echo building $@
42	@git describe >$<-version.txt 2>/dev/null \
43	|| sed -n '/^[0-9][0-9.]* [0-9-]*:$$/{s/ .*//;p;q;}' ChangeLog_$<.yml >$<-version.txt
44
45standalone/vimpager: vimpager vimpager-version.txt ${SRC:=.uu} inc/* Makefile
46	@echo building $@
47	@${MKPATH} ${@D}
48	@sed \
49	    -e '/^ *\. .*inc\/prologue.sh"$$/{' \
50	    -e     'r inc/prologue.sh' \
51	    -e     d \
52	    -e '}' \
53	    -e 's/^\( *\)# EXTRACT BUNDLED SCRIPTS HERE$$/\1extract_bundled_scripts/' \
54	    -e 's|^version=.*|version="'"`cat vimpager-version.txt`"' (standalone, shell=\$$(command -v \$$POSIX_SHELL))"|' \
55	    -e 's!^\( *\)runtime=.*$$!\1runtime='\''\$$tmp/runtime'\''!' \
56	    -e 's!^\( *\)vimcat=.*$$!\1vimcat='\''\$$runtime/bin/vimcat'\''!' \
57	    -e 's!^\( *\)system_vimpagerrc=.*$$!\1system_vimpagerrc='\'\''!' \
58	    -e '/^# INCLUDE BUNDLED SCRIPTS HERE$$/{ q; }' \
59	    vimpager > $@
60	@cat inc/do_uudecode.sh >> $@
61	@cat inc/bundled_scripts.sh >> $@
62	@cat ${SRC:=.uu} >> $@
63	@sed -n '/^# END OF BUNDLED SCRIPTS$$/,$$p' vimpager >> $@
64	@chmod +x $@
65
66standalone/vimcat: vimcat autoload/vimcat.vim vimcat-version.txt inc/prologue.sh Makefile
67	@echo building $@
68	@${MKPATH} ${@D}
69	@nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"; \
70	sed -e '1a\'"$$nl"': if 0' \
71	    -e '/^# FIND REAL PARENT DIRECTORY$$/,/^# END OF FIND REAL PARENT DIRECTORY$$/d' \
72	    -e 's/^\( *\)# INSERT VIMCAT_DEBUG PREPARATION HERE$$/\1if [ "$${VIMCAT_DEBUG:-0}" -eq 0 ]; then silent="silent! "; else silent=; fi/' \
73	    -e 's|^version=.*|version="'"`cat vimcat-version.txt`"' (standalone, shell=\$$(command -v \$$POSIX_SHELL))"|' \
74	    -e '/^runtime=.*/d' \
75	    -e '/^ *--cmd "set rtp^=\$$runtime" \\$$/d' \
76	    -e '/call vimcat#Init/i\'"$$nl"'--cmd "$$silent source $$0" \\' \
77	    -e 's/vimcat#\([^ ]*\)(/\1(/g' \
78	    -e '/^ *\. .*inc\/prologue.sh"$$/{' \
79	    -e     'r inc/prologue.sh' \
80	    -e     d \
81	    -e '}' \
82	    vimcat > $@
83	@cp $@ $@.work
84	@awk '/^[ 	]*(if|for|while)/ { print $$1 }' $@ \
85	  | sed '1!G;h;$$!d' \
86	  | sed -e 's/^/: end/' >> $@.work
87	@mv -f $@.work $@
88	@echo ': endif' >> $@
89	@sed -e 's/vimcat#\([^ ]*\)(/\1(/g' autoload/vimcat.vim >> $@
90	@chmod +x $@
91
92vimcat.uu: vimcat vimcat-version.txt
93	@echo uuencoding vimcat
94	@echo 'vimcat_script() {' > $@
95	@printf "\t(cat <<'EOF') | do_uudecode > bin/vimcat\n" >> $@
96	@sed \
97	    -e 's|^version=.*|version="'"`cat vimcat-version.txt`"' (bundled, shell=\$$(command -v \$$POSIX_SHELL))"|' \
98	    -e '/^ *\. .*inc\/prologue.sh"$$/{' \
99	    -e     'r inc/prologue.sh' \
100	    -e     d \
101	    -e '}' \
102	    vimcat > $@.work
103	@uuencode $@.work vimcat >> $@
104	@echo EOF >> $@
105	@echo '}' >> $@
106	@rm $@.work
107
108%.uu: %
109	@echo uuencoding $<
110	@echo '$<() {' | sed 's|[/.]|_|g' > $@
111	@printf "\t(cat <<'EOF') | do_uudecode > $<\n" >> $@
112	@uuencode $< $< >> $@
113	@echo EOF >> $@
114	@echo '}' >> $@
115
116uninstall:
117	rm -f "${prefix}/bin/vimpager"
118	rm -f "${prefix}/bin/vimcat"
119	rm -f "${prefix}/share/man/man1/vimpager.1"
120	rm -f "${prefix}/share/man/man1/vimcat.1"
121	rm -rf "${prefix}/share/doc/vimpager"
122	rm -rf "${prefix}/share/vimpager"
123	@if [ '${PREFIX}' = '/usr' ] && diff /etc/vimpagerrc vimpagerrc >/dev/null 2>&1; then \
124	    echo rm -f /etc/vimpagerrc; \
125	    rm -rf /etc/vimpagerrc; \
126	elif diff "${SYSCONFDIR}/vimpagerrc" vimpagerrc >/dev/null 2>&1; then \
127	    echo rm -f "${SYSCONFDIR}/vimpagerrc"; \
128	    rm -f "${SYSCONFDIR}/vimpagerrc"; \
129	fi
130
131install: docs vimpager.configured vimcat.configured
132	@chmod +x ./install-sh 2>/dev/null || true
133	@${MKPATH} "${DESTDIR}${prefix}/bin"
134	${INSTALLBIN} vimpager.configured "${DESTDIR}${prefix}/bin/vimpager"
135	${INSTALLBIN} vimcat.configured "${DESTDIR}${prefix}/bin/vimcat"
136	@if [ -d man ]; then \
137	    ${MKPATH} "${DESTDIR}${prefix}/share/man/man1"; \
138	    echo ${INSTALLMAN} man/vimpager.1 "${DESTDIR}${prefix}/share/man/man1/vimpager.1"; \
139	    ${INSTALLMAN} man/vimpager.1 "${DESTDIR}${prefix}/share/man/man1/vimpager.1"; \
140	    echo ${INSTALLMAN} man/vimcat.1 "${DESTDIR}${prefix}/share/man/man1/vimcat.1"; \
141	    ${INSTALLMAN} man/vimcat.1 "${DESTDIR}${prefix}/share/man/man1/vimcat.1"; \
142	fi
143	@${MKPATH} "${DESTDIR}${prefix}/share/doc/vimpager"
144	${INSTALLDOC} markdown_src/vimpager.md "${DESTDIR}${prefix}/share/doc/vimpager/vimpager.md"
145	${INSTALLDOC} markdown_src/vimcat.md "${DESTDIR}${prefix}/share/doc/vimpager/vimcat.md"
146	${INSTALLDOC} TODO.yml "${DESTDIR}${prefix}/share/doc/vimpager/TODO.yml"
147	${INSTALLDOC} DOC_AUTHORS.yml "${DESTDIR}${prefix}/share/doc/vimpager/DOC_AUTHORS.yml"
148	${INSTALLDOC} ChangeLog_vimpager.yml "${DESTDIR}${prefix}/share/doc/vimpager/ChangeLog_vimpager.yml"
149	${INSTALLDOC} ChangeLog_vimcat.yml "${DESTDIR}${prefix}/share/doc/vimpager/ChangeLog_vimcat.yml"
150	${INSTALLDOC} uganda.txt "${DESTDIR}${prefix}/share/doc/vimpager/uganda.txt"
151	${INSTALLDOC} debian/copyright "${DESTDIR}${prefix}/share/doc/vimpager/copyright"
152	@if [ -d html ]; then \
153	    ${MKPATH} "${DESTDIR}${prefix}/share/doc/vimpager/html"; \
154	    echo ${INSTALLDOC} html/vimpager.html "${DESTDIR}${prefix}/share/doc/vimpager/html/vimpager.html"; \
155	    ${INSTALLDOC} html/vimpager.html "${DESTDIR}${prefix}/share/doc/vimpager/html/vimpager.html"; \
156	    echo ${INSTALLDOC} html/vimcat.html "${DESTDIR}${prefix}/share/doc/vimpager/html/vimcat.html"; \
157	    ${INSTALLDOC} html/vimcat.html "${DESTDIR}${prefix}/share/doc/vimpager/html/vimcat.html"; \
158	fi
159	${MKPATH} "${DESTDIR}${prefix}/share/vimpager"
160	@for rt_file in ${RUNTIME}; do \
161	    if [ ! -d "`dirname "${DESTDIR}${prefix}/share/vimpager/$$rt_file"`" ]; then \
162		echo ${MKPATH} "`dirname "${DESTDIR}${prefix}/share/vimpager/$$rt_file"`"; \
163		${MKPATH} "`dirname "${DESTDIR}${prefix}/share/vimpager/$$rt_file"`"; \
164	    fi; \
165	    echo ${INSTALLFILE} "$$rt_file" "${DESTDIR}${prefix}/share/vimpager/$$rt_file"; \
166	    ${INSTALLFILE} "$$rt_file" "${DESTDIR}${prefix}/share/vimpager/$$rt_file"; \
167	done
168	@SYSCONFDIR='${DESTDIR}${SYSCONFDIR}'; \
169	if [ '${PREFIX}' = /usr ]; then \
170	    SYSCONFDIR='${DESTDIR}/etc'; \
171	fi; \
172	${MKPATH} "$${SYSCONFDIR}" 2>/dev/null || true; \
173	echo ${INSTALLCONF} vimpagerrc "$${SYSCONFDIR}/vimpagerrc"; \
174	${INSTALLCONF} vimpagerrc "$${SYSCONFDIR}/vimpagerrc"
175
176%.configured: % %-version.txt
177	@echo configuring $<
178	@POSIX_SHELL="`scripts/find_shell`"; \
179	if [ '${PREFIX}' = /usr ]; then \
180	    vimpagerrc=/etc/vimpagerrc; \
181	else \
182	    vimpagerrc='${SYSCONFDIR}/vimpagerrc'; \
183	fi; \
184	sed -e '1{ s|.*|#!'"$$POSIX_SHELL"'|; }' \
185	    -e 's|\$$POSIX_SHELL|'"$$POSIX_SHELL|" \
186	    -e '/^ *\. .*inc\/prologue.sh"$$/d' \
187	    -e 's|^version=.*|version="'"`cat $<-version.txt`"' (configured, shell='"$$POSIX_SHELL"')"|' \
188	    -e '/^# FIND REAL PARENT DIRECTORY$$/,/^# END OF FIND REAL PARENT DIRECTORY$$/d' \
189	    -e 's!^\( *\)runtime=.*!\1runtime='\''${PREFIX}/share/vimpager'\''!' \
190	    -e 's!^\( *\)vimcat=.*!\1vimcat='\''${PREFIX}/bin/vimcat'\''!' \
191	    -e 's!^\( *\)system_vimpagerrc=.*!\1system_vimpagerrc='\'"$$vimpagerrc"\''!' \
192	    $< > $@
193	@chmod +x $@
194
195check-root:
196	@if [ "`id | cut -d= -f2 | cut -d'(' -f1`" -ne 0 ]; then \
197	    echo 'ERROR: You must be root, try sudo.' >&2; \
198	    echo >&2; \
199	    exit 1; \
200	fi
201
202build-deb-deps-prereqs: check-root
203	@-apt-get -qq update
204	@apt-get -yqq install debhelper devscripts equivs gdebi-core
205
206build-deb-deps-build: build-deb-deps-prereqs clean
207	@mk-build-deps
208	@echo y | LANG=C gdebi vimpager-build-deps*.deb
209
210build-deb-deps-clean:
211	@rm -f vimpager-build-deps*.deb
212
213build-deb-deps: build-deb-deps-build build-deb-deps-clean
214
215build-deb-build: build-deb-deps
216	@orig_tar_ball=../vimpager_"`sed -ne '/^vimpager (/{ s/^vimpager (\([^)-]*\).*/\1/p; q; }' debian/changelog)`".orig.tar; \
217	    rm -f "$$orig_tar_ball".gz; \
218	    tar cf "$$orig_tar_ball" * .travis.yml .mailmap .gitignore; \
219	    gzip "$$orig_tar_ball"
220	@dpkg-buildpackage -us -uc
221
222build-deb-clean:
223	@dpkg --purge vimpager-build-deps
224	@-[ "$${CLEAN_BUILD_DEPS:-1}" -ne 0 ] && apt-get -yqq autoremove
225	@debian/rules clean
226
227build-deb: build-deb-build build-deb-clean
228
229install-deb: build-deb
230	@echo y | LANG=C gdebi `ls -1t ../vimpager*deb | head -1`
231
232docs: ${GEN_DOCS} docs.tar.gz Makefile
233	@rm -f docs-warn-stamp doctoc-warn-stamp
234
235docs.tar.gz: ${GEN_DOCS} ${DOC_SRC}
236	@rm -f $@
237	@if [ "`ls -1 $? 2>/dev/null | wc -l`" -eq "`echo $? | wc -w`" ]; then \
238	    echo tar cf docs.tar $?; \
239	    tar cf docs.tar $?; \
240	    echo gzip -9 docs.tar; \
241	    gzip -9 docs.tar; \
242	fi
243
244# Build markdown with TOCs and gitter.im badge
245markdown/%.md: markdown_src/%.md
246	@if command -v doctoc >/dev/null; then \
247	    echo 'generating $@'; \
248	    ${MKPATH} `dirname '$@'` 2>/dev/null || true; \
249	    cp $< $@.work; \
250	    doctoc --title '### Vimpager User Manual' $@.work >/dev/null; \
251	    cat markdown_src/gitter-im-badge.md $@.work > $@; \
252	    rm -f $@.work; \
253	else \
254	    if [ ! -r doctoc-warn-stamp ]; then \
255		echo >&2; \
256		echo "WARNING: doctoc is not available, markdown with Tables Of Contents will not be generated. If you want to generate them, install doctoc with: npm install -g doctoc" >&2; \
257		echo >&2; \
258		touch doctoc-warn-stamp; \
259	    fi; \
260	fi
261
262man/%.1: markdown_src/%.md
263	@if command -v pandoc >/dev/null; then \
264	    echo 'generating $@'; \
265	    ${MKPATH} `dirname '$@'` 2>/dev/null || true; \
266	    ${PANDOC} -s -f markdown_github $< -o $@; \
267	else \
268	    if [ ! -r docs-warn-stamp ]; then \
269		echo >&2; \
270		echo "WARNING: pandoc is not available, man pages and html will not be generated. If you want to install the man pages and html, install pandoc and re-run make." >&2; \
271		echo >&2; \
272		touch docs-warn-stamp; \
273	    fi; \
274	fi
275
276.SECONDARY: vimpager.md.work vimcat.md.work
277
278# transform markdown links to html links
279%.md.work: markdown_src/%.md
280	@sed -e 's|\(\[[^]]*\]\)(markdown/\([^.]*\)\.md)|\1(\2.html)|g' < $< > $@
281
282html/%.html: %.md.work
283	@if command -v pandoc >/dev/null; then \
284	    echo 'generating $@'; \
285	    ${MKPATH} `dirname '$@'` 2>/dev/null || true; \
286	    ${PANDOC} -s --toc -f markdown_github $< -o $@; \
287	    rm -f $<; \
288	else \
289	    if [ ! -r docs-warn-stamp ]; then \
290		echo >&2; \
291		echo "WARNING: pandoc is not available, man pages and html will not be generated. If you want to install the man pages and html, install pandoc and re-run make." >&2; \
292		echo >&2; \
293		touch docs-warn-stamp; \
294	    fi; \
295	fi
296
297realclean distclean clean:
298	rm -rf *.work */*.work *-stamp *-version.txt *.deb *.tar.gz *.configured *.uu */*.uu man html standalone */with_meta_*
299
300test: standalone/vimpager standalone/vimcat
301	@if command -v bats >/dev/null; then \
302	    bats test; \
303	else \
304	    echo "WARNING: bats is not available, tests will not be run. If you want to run tests, install bats from https://github.com/sstephenson/bats.git or your distribution and rerun \`make test\`." >&2; \
305	    echo >&2; \
306	fi
307
308.PHONY: all install uninstall docs realclean distclean clean test check-root build-deb-deps-prereqs build-deb-deps-build build-deb-deps-clean build-deb-deps build-deb-clean build-deb install-deb
309# vim: sw=4
310