summaryrefslogtreecommitdiff
path: root/Makefile
blob: 7b72fe8fbb662f24772aa41f4903cc3371da8021 (plain)
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
# all (default)
#   Create all the necessary directories, javascript, css, etc.
#
# prod
#   Create static assets for production. Requires the following additional dependencies:
#   - uglifyjs
#   - zopfli
#   - pandoc
#
# chmod
#   For when the http process is run from a different user than the files are
#   chown'ed to. chmods all files and directories written to from vndb.pl.
#
# multi-start, multi-stop, multi-restart:
#   Start/stop/restart the Multi daemon. Provided for convenience, a proper initscript
#   probably makes more sense.
#
# NOTE: This Makefile has only been tested using a recent version of GNU make
#   in a relatively up-to-date Arch/Gentoo Linux environment, and may not work in
#   other environments. Patches to improve the portability are always welcome.


.PHONY: all prod chmod multi-stop multi-start multi-restart

ALL_KEEP=\
	static/ch static/cv static/sf static/st \
	data/log static/g www \
	data/conf.pl \
	www/robots.txt static/robots.txt

ALL_CLEAN=\
	static/g/plain.js \
	static/g/elm.js \
	data/icons/icons.css \
	sql/editfunc.sql \
	$(shell ls css/skins/*.sass | sed -e 's/css\/skins\/\(.\+\)\.sass/static\/g\/\1.css/g')

PROD=\
	static/g/api-nyan.html static/g/api-kana.html\
	static/g/plain.min.js static/g/plain.min.js.gz \
	static/g/elm.min.js static/g/elm.min.js.gz \
	static/g/icons.opt.png \
	$(shell ls css/skins/*.sass | sed -e 's/css\/skins\/\(.\+\)\.sass/static\/g\/\1.css.gz/g')

all: ${ALL_KEEP} ${ALL_CLEAN}
prod: all ${PROD}

clean:
	rm -f ${ALL_CLEAN} ${PROD}
	rm -f static/g/icons.png
	rm -f static/f/{vndb,elm,plain}{,.min}.js{,.gz} static/f/icons{,.opt}.png static/s/*/style{,.min}.css{,.gz} static/s/*/boxbg.png
	rm -f static/g/vndb{,.min}.js{,.gz}
	rm -rf elm/Gen/
	rm -rf elm/elm-stuff/build-artifacts
	$(MAKE) -C sql/c clean

cleaner: clean
	rm -rf elm/elm-stuff

sql/editfunc.sql: util/sqleditfunc.pl sql/schema.sql
	util/sqleditfunc.pl

static/ch static/cv static/sf static/st:
	mkdir -p $@;
	for i in $$(seq -w 0 1 99); do mkdir -p "$@/$$i"; done

data/log www static/g:
	mkdir -p $@

data/conf.pl:
	cp -n data/conf_example.pl data/conf.pl

%/robots.txt: | www
	echo 'User-agent: *' > $@
	echo 'Disallow: /' >> $@

%.gz: %
	zopfli $<

chmod: all
	chmod -R a-x+rwX static/{ch,cv,sf,st}



data/icons/icons.css: data/icons/*.png data/icons/*/*.png util/spritegen.pl | static/g
	util/spritegen.pl

static/g/icons.png: data/icons/icons.css

static/g/icons.opt.png: static/g/icons.png
	rm -f $@
	zopflipng -m --lossy_transparent $< $@

static/g/%.css: css/skins/%.sass css/v2.css data/icons/icons.css | static/g
	( echo '$$icons-version: "$(shell sha1sum static/g/icons.png | head -c8)";'; echo '@import "css/skins/$*"' ) | sassc --stdin -I. --style compressed >$@



# Order of JS files matters, so we read an '//order:x' comment from the files and sort by that.
# Files without that comment are assumed to have '//order:4'.
# (This trick will not work if we ever add JS files generated by this Makefile)
JS_FILES=$(shell find elm \! -path 'elm/elm-stuff/*' -name '*.js' -exec sh -c "echo \`grep -Eo '^// *order: *[0-9]+' \"{}\" || echo 4\` \"{}\"" \; | sed -E 's/\/\/ *order: *//' | sort | sed 's/..//')

static/g/plain.js: ${JS_FILES} | static/g
	echo '// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-only' > $@~
	echo '// @source: https://code.blicky.net/yorhel/vndb/src/branch/master/elm' >>$@~
	echo '// SPDX-License-Identifier: AGPL-3.0-only' >>$@~
	for fn in ${JS_FILES}; do \
		echo; \
		echo "(function(){'use strict'; /* $$fn */"; \
		cat $$fn; \
		echo "})();"; \
	done >>$@~
	echo '// @license-end' >>$@~
	mv $@~ $@

static/g/plain.min.js: static/g/plain.js
	uglifyjs $< --comments '/(@license|@source|SPDX-)/' --compress \
		'pure_getters,keep_fargs=false,unsafe_comps,unsafe'\
		| uglifyjs --mangle --comments all -o $@


ELM_FILES=elm/*.elm elm/*/*.elm
ELM_MODULES=$(shell grep -l '^main =' ${ELM_FILES} | sed 's/^elm\///')

# Patch the Javascript generated by Elm:
# - Add @license and @source comments
# - Redirect calls from Lib.Ffi.* to window.elmFfi_*
# - Patch the virtualdom diffing algorithm to always apply the 'selected' attribute
define fix-elm
	( echo '// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-only'; \
	  echo '// @license magnet:?xt=urn:btih:c80d50af7d3db9be66a4d0a86db0286e4fd33292&dn=bsd-3-clause.txt BSD-3-Clause'; \
	  echo '// @source: https://code.blicky.net/yorhel/vndb/src/branch/master/elm'; \
	  echo '// SPDX-License-Identifier: AGPL-3.0-only and BSD-3-Clause'; \
	  cat $@; \
	  echo; \
	  echo '// @license-end' \
	)   | sed 's/var \$$author\$$project\$$Lib\$$Ffi\$$/var __unused__/g' \
		| sed -E 's/\$$author\$$project\$$Lib\$$Ffi\$$([a-zA-Z0-9_]+)/window.elmFfi_\1(_Json_wrap,_Browser_call)/g' \
		| sed -E "s/([^ ]+) !== 'checked'/\\1 !== 'checked' \&\& \\1 !== 'selected'/g" >$@~
	mv $@~ $@
endef

elm/Gen/.generated: lib/VNWeb/*.pm lib/VNWeb/*/*.pm lib/VNDB/Types.pm lib/VNDB/ExtLinks.pm lib/VNDB/Config.pm data/conf.pl
	util/vndb.pl elmgen

static/g/elm.js: ${ELM_FILES} elm/Gen/.generated | static/g
	cd elm && ELM_HOME=elm-stuff elm make ${ELM_MODULES} --output ../$@
	${fix-elm}

static/g/elm.min.js: ${ELM_FILES} elm/Gen/.generated | static/g
	cd elm && ELM_HOME=elm-stuff elm make --optimize ${ELM_MODULES} --output ../$@
	${fix-elm}
	uglifyjs $@ --comments '/(@license|@source|SPDX-)/' --compress \
		'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters,keep_fargs=false,unsafe_comps,unsafe'\
		| uglifyjs --mangle --comments all -o $@~
	mv $@~ $@



static/g/api-%.html: data/api-%.md
	pandoc "$<" -st html5 --toc -o "$@"



# Multi

# may wait indefinitely, ^C and kill -9 in that case
define multi-stop
	if [ -s data/multi.pid ]; then\
	  kill `cat data/multi.pid`;\
	  while [ -s data/multi.pid ]; do\
	    if kill -0 `cat data/multi.pid`; then sleep 1;\
	    else rm -f data/multi.pid; fi\
	  done;\
	fi
endef

define multi-start
	util/multi.pl
endef

multi-stop:
	$(multi-stop)

multi-start:
	$(multi-start)

multi-restart:
	$(multi-stop)
	$(multi-start)