Pretty print of site Makefile

#!/usr/bin/make -f

build: jekyll

drafts: jekylldrafts

# --- Common stuff used by main and draft targets ---

cleanup:
	#Cleanup common unneeded stuff
	-find -iname '*.bak' | xargs rm
	-find -iname 'Thumbs.db' | xargs rm
	-rm -rf ./_site/__draft

listings:
	#All things in one page
	./script/make-listing.sh
	#Hierarchical listing as MD pages
	# - Delete existing listings
	-find pages/listing/ -maxdepth 1 -mindepth 1 -type d | xargs rm -r
	# - Create new ones, in parallel
	echo -e "downloads\npics\nscript\nvolatile\nstatic" | xargs -I% --max-procs=0 ./script/make-dir-listing.sh pages/listing/% %/

.PHONY: pages/index.md
pages/index.md:
	echo "---" > $@
	echo "layout: default" >> $@
	echo "---" >> $@
	echo "" >> $@
	echo "<!-- no spellcheck -->" >> $@
	echo "" >> $@
	echo "# Pages index" >> $@
	echo "" >> $@
	echo "| Page |" >> $@
	echo "|------|" >> $@
	find pages/ -maxdepth 1 -iname '*.md' | grep -ivF 'index.md' | sort | sed 's/pages\/\(.*\)\.md/| [\1](\1.html) |/' >> $@

pages/makefile.md: Makefile
	echo "---" > $@
	echo "layout: default" >> $@
	echo "---" >> $@
	echo "" >> $@
	echo -ne '\nPretty print of site ' >> $@
	echo -e '[Makefile](/Makefile)\n' >> $@
	echo "" >> $@
	echo '```make' >> $@
	cat Makefile >> $@
	echo '```' >> $@

documents:
	-cd doc && make

#Make use of positive (black-on-white) diagrams
positivize:
	./script/positivize.sh pics/
	./script/positivize.sh pages/

#Make use of negative (white-on-black) diagrams
negativize:
	./script/negativize.sh pics/
	./script/negativize.sh pages/

# All the sites under the /sites dir
sites: kludge codenames it-works-on-xp wap_site

#A wap site. Yeah, really. Go to wap.heavydeck.net
#  - Generates the .wbmp files from existing .png files
wap_site:
	wap/make-site.sh wap/

# Kludge schematic editor
kludge:
	-rm -rf sites/kludge
	mkdir sites/kludge
	-git clone 'https://gitlab.com/heavydeck-projects/web/Kludge-schematic-editor.git' sites/kludge
	touch sites/kludge/kludge.html
	ln -s kludge.html sites/kludge/index.html
	-rm -rf sites/kludge/.git

# Codenames
codenames:
	-rm -rf sites/codenames
	mkdir sites/codenames
	-git clone 'https://gitlab.com/heavydeck-projects/web/Codenames-cs.git' sites/codenames
	touch sites/codenames/generator.html
	ln -s generator.html sites/codenames/index.html
	-rm -rf sites/codenames/.git
	#Version 2
	-rm -rf sites/codenames-v2
	mkdir sites/codenames-v2
	-git clone --single-branch --branch=V2 'https://gitlab.com/heavydeck-projects/web/Codenames-cs.git' sites/codenames-v2
	touch sites/codenames-v2/generator.html
	ln -s board.html sites/codenames-v2/index.html
	-rm -rf sites/codenames-v2/.git

it-works-on-xp:
	-rm -rf sites/it-works-on-xp
	mkdir sites/it-works-on-xp
	-git clone 'https://gitlab.com/heavydeck-projects/web/It-works-on-XP.git' sites/it-works-on-xp
	-mkdir sites/it-works-on-xp/blog
	-cd sites/it-works-on-xp ; make build >/dev/null 2>/dev/null
	-ln -s ../data/pics sites/it-works-on-xp/blog/pics
	-ln -s ../data/css sites/it-works-on-xp/blog/css
	-find sites/it-works-on-xp -iname '*.md' | xargs rm
	-touch sites/it-works-on-xp/blog/index.html
	-rm -rf sites/it-works-on-xp/.git

# --- Stuff pulled from other repos ---
externals: static/stl_viewer/stl_viewer.min.js

static/stl_viewer/stl_viewer.min.js:
	#Allow git to fail since that means the repo is already cloned
	-git clone https://github.com/omrips/viewstl.git _repos/stl_viewer/
	-mkdir -p static/stl_viewer/
	-rm static/stl_viewer/*.js
	cp -v _repos/stl_viewer/build/* static/stl_viewer/

# --- Patched files ---
#These files should break if the base template, in this case minima-2.5.1,
#change version. This is expected and it would only need updating the path and
#maybe updating the patch file as well.

_includes/head.html: _includes/head.diff
	patch -o $@ vendor/bundle/ruby/2.5.0/gems/minima-2.5.1/_includes/head.html $<

patches: _includes/head.html

# --- Miscelaneus targets ---

#Setup Jekyll for site creation
#  - This step may require Ruby headers & C/C++ compiler
init:
	-rm -r .bundle
	-rm -r vendor
	-rm -r .jekyll-cache
	-rm Gemfile
	-rm Gemfile.lock
	bundle init
	bundle config set --local path 'vendor/bundle'
	bundle install --path 'vendor/bundle'
	bundle add webrick
	bundle add eventmachine
	bundle add jekyll
	bundle add jekyll-feed
	bundle add minima
	-rm _config.yml
	cp _config.yml.original _config.yml

#Self-explanatory
spellcheck:
	./script/spellcheck.sh

#Generated pages under the pages/ dir
pages: pages/index.md pages/makefile.md listings

#Site statistics
stats.log:
	#stats for nerds
	touch stats.log
	#  - Repository commit ID
	echo -ne 'Commit ID: ' >> stats.log
	-git log --format="%H" -n 1 | head -1 >> stats.log
	-[ `git status --porcelain | wc -l` -ne 0 ] && echo -e '(With uncommitted changes)\n' >> stats.log || echo '' >> stats.log
	#  - Posts
	echo -ne ' ' >> stats.log
	find _posts/ -type f -iname '*.md' | wc -l | tr -d '\n\t' >> stats.log
	echo -e '\tPosts' >> stats.log
	#  - All file count
	echo -ne ' ' >> stats.log
	find _site/ -type f | wc -l | tr -d '\n\t' >> stats.log
	echo -e '\tFiles' >> stats.log
	#  - /pics/ file count
	echo -ne ' ' >> stats.log
	find _site/pics/ -type f | wc -l | tr -d '\n\t' >> stats.log
	echo -e '\tPictures' >> stats.log
	#  - /downloads/ file count
	echo -ne ' ' >> stats.log
	find _site/downloads/ -type f | wc -l | tr -d '\n\t' >> stats.log
	echo -e '\tDownloads' >> stats.log
	#  - Files from httrack mirrors
	echo -ne ' ' >> stats.log
	find _site/httrack-sites/ -type f | wc -l | tr -d '\n\t' >> stats.log
	echo -e '\tFiles from mirrors' >> stats.log
	#  - Site size
	echo -ne ' ' >> stats.log
	du -h -d0 _site/ | sed 's/M.*/M/' | tr -d '\n\t' >> stats.log
	echo -e '\tSite size' >> stats.log
	#  - Repo size
	echo -ne ' ' >> stats.log
	du -h -d0 .git/ | sed 's/M.*/M/' | tr -d '\n\t' >> stats.log
	echo -e '\tRepo size' >> stats.log

#Site backup (as a zipfile)
backup: cleanup pages sites documents
	-rm backup.zip
	zip -n .7z:.zip:.gz:.xz:.png:.jpg:.webm:.mp4 -r backup.zip *.md *.txt Makefile _config.yml _drafts/ _posts/ doc/ downloads/ httrack-sites/ live/ pages/ pics/ script/ sites/ static/

#Generate favicon.ico from source PNG files
#  - This target uses ImageMagick "convert"; It will usually be up-to-date
#    and not be executed since the generated favicon.ico will be added to
#    the repository.
pics/favicon/favicon.ico: pics/favicon/favicon-16.png pics/favicon/favicon-32.png pics/favicon/favicon-48.png pics/favicon/favicon-256.png
	convert $^ $@

#Generate a sample HLS playlist and TS files for /live/ch0/
sample-hls:
	#Generate a short video file (uses vaapi)
	gst-launch-1.0 -q audiotestsrc wave=6 ! avenc_aac bitrate=64000 ! queue ! muxer. videotestsrc ! "video/x-raw,width=1920,height=1080" ! vaapih264enc ! queue ! muxer. mpegtsmux name=muxer ! queue ! fdsink fd=1 | head --bytes=104857600 > sample-stream.ts
	#Make it into HLS
	-rm live/ch0/*.ts
	-rm live/ch0/*.m3u8
	gst-launch-1.0 -q filesrc location=sample-stream.ts ! tsdemux name=demux demux. ! queue ! "video/x-h264" ! h264parse ! muxer. demux. ! queue ! "audio/mpeg,mpegversion=4" ! aacparse ! muxer. mpegtsmux name=muxer ! queue ! hlssink playlist-length=30 target-duration=5
	#Move to destination and delete temporary file
	mv segment*.ts live/ch0/
	mv playlist.m3u8 live/ch0/
	rm sample-stream.ts

#Generate/test checksums
checksum:
	git ls-files | grep -v '_sha256sum$$' | xargs sha256sum | sort > _sha256sum
test:
	sha256sum --check --quiet _sha256sum

#Add a shitton of symlinks to gitignore
ignored-symlinks:
	cat .gitignore_base > .gitignore
	#All picture symlinks (except favicon)
	find ./pics/ -type l | sed 's/^\.\///' | grep -iv 'favicon' >> .gitignore
# --- Site build targets ---

#Main site build
#  - Site
#  - Cleaned-up httrack mirrors
#  - Stats for nerds
#  - Compression
jekyll: cleanup externals patches pages negativize sites documents stats.log pics/favicon/favicon.ico
	#Use release main.scss
	ln -f -s main.scss.release assets/main.scss
	#Build the site
	bundler exec jekyll b --trace
	#Cleanup some files that don't need to be public
	-rm _site/doc/Makefile
	#Cleanup HTTrack files on output site
	-find _site/httrack-sites/ -type d -iname hts-cache | xargs -n1 rm -r
	-find _site/httrack-sites/ -iname cookies.txt | xargs -n1 rm
	-find _site/httrack-sites/ -iname hts-log.txt | xargs -n1 rm
	#Build httrack site indices
	./_site/httrack-sites/make-index.sh
	#Inject stats.log into the misc placeholder
	#  - _site/misc/index.html
	#  - [begin_stats]
	sed -i -e '/begin_stats/r stats.log' _site/misc/index.html
	-rm stats.log

	#gzip compress most file types for nginx gzip_static
	-script/gzip-static.sh

	#Make sure __draft/index.html exists
	-mkdir _site/__draft
	-touch _site/__draft/index.html

	#remove css symlink
	rm assets/main.scss

	#find _site -print0 -type f | xargs -n1 --null chmod 644

#Main site build
#  - Site
#  - No httrack mirrors
#  - No stats
jekylldrafts: cleanup externals patches pages negativize sites documents pics/favicon/favicon.ico
	#Use draft main.scss
	ln -f -s main.scss.draft assets/main.scss
	#Make a temporary draft config to put the site under the /__draft url
	sed 's|baseurl: ""|baseurl: "/__draft"|g' _config.yml > _draft.yml
	#Build the site
	bundler exec jekyll b --incremental --trace --future --drafts --config=_draft.yml -d ./_site/__draft
	#Delete httrack alltogether
	-rm -rf _site/__draft/httrack-sites
	#Remove the config
	rm _draft.yml
	#remove draft css symlink
	rm assets/main.scss

#Fast drafts, just run Jekyll
fastdrafts: patches
	#Use draft main.scss
	ln -f -s main.scss.draft assets/main.scss
	#Make a temporary draft config to put the site under the /__draft url
	sed 's|baseurl: ""|baseurl: "/__draft"|g' _config.yml > _draft.yml
	#Build the site
	bundler exec jekyll b --incremental --trace --future --drafts --config=_draft.yml -d ./_site/__draft
	#Delete httrack alltogether
	-rm -rf _site/__draft/httrack-sites
	#Remove the config
	rm _draft.yml
	#remove draft css symlink
	rm assets/main.scss