# Copyright (C) 2023-2024 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

CURL ?= curl
WORDPRESS_BLOG_URL ?= https://blog.replicant.us/page

# At the time of writing, Guix 1.5.0 is the latest release and it has Guile
# 3.0.9 which is too old. We need 3.0.10 for the support of the date format
# used by WordPress in, date->time-utc otherwise we end up with the following
# error:
#     srfi/srfi-19.scm:669:45: In procedure date->time-utc:
#     In procedure struct-vtable: Wrong type argument in position 1 (expecting
#     struct): #f
#
# And if we have at the same time --with-input=guile=guile-next and and git as
# in the list of packages, we end up with the following build error:
#     The following derivations will be built:
#       /gnu/store/4hd9k1ylps11hcz852y97bv6hvy32c9s-graphviz-7.0.1.drv
#       /gnu/store/rbxql65g3rhi54d103hqgcmax60w9lam-vala-0.56.17.drv
#       /gnu/store/kw8l6g17z51x1lbhjdaxh5fsa156cb99-libsecret-0.21.4.drv
#       /gnu/store/9cl1zwzplakbwvpxwra5jlnyy0sip9m6-git-2.52.0.drv
#       /gnu/store/hysifylc5q45f67hfrlcs41aykmpm436-profile.drv
#
#     building /gnu/store/4hd9k1ylps11hcz852y97bv6hvy32c9s-graphviz-7.0.1.drv...
#     | 'move-guile-bindings' phasebuilder for
#         `/gnu/store/[...]-graphviz-7.0.1.drv' failed with exit code 1
#     build of /gnu/store/[...]-graphviz-7.0.1.drv failed
#     View build log at '/var/log/guix/drvs/4h/[...]-graphviz-7.0.1.drv.gz'.
#     cannot build derivation `/gnu/store/[...]-libsecret-0.21.4.drv':
#         1 dependencies couldn't be built
#     cannot build derivation `/gnu/store/[...]-git-2.52.0.drv':
#         1 dependencies couldn't be built
#     cannot build derivation `/gnu/store/[...]-profile.drv':
#         1 dependencies couldn't be built
#     guix shell: error: build of `/gnu/store/[...]-profile.drv' failed
#
# If we update to the commit 24163eea584663568b68e19f364256fc7396b61f ("gnu:
# guile: Update to 3.0.10.") to use Guile 3.0.10 as our main Guile, this also
# fails to build with the following error:
#
#    ▏builder for
#    `/gnu/store/w9yvw8972xns0j3j36lg4lbyqv5m2f25-guix-cli-core.drv' failed
#    with exit code 1
#
# So we update to the commit c3fefb065c53703e3dc6507c813182101c5c3a38 ("gnu:
# guile: Change ‘guile-3.0-latest’ to 3.0.11.") instead.
GUIX_COMMIT ?= c3fefb065c53703e3dc6507c813182101c5c3a38
GUIX_SHELL ?= guix \
	time-machine \
	--channels=channels.scm \
	--commit=$(GUIX_COMMIT) \
	-- \
	shell
GUIX_PYTHON_PACKAGES ?= python python-beautifulsoup4 python-html2text python-sh

CONVERT ?= \
	$(GUIX_SHELL) \
	-C \
	$(GUIX_PYTHON_PACKAGES) \
	-- \
	python3 wordpresshtml2commonmark.py

HAUNT ?= $(GUIX_SHELL) \
	-C \
	git haunt \
	-- \
	haunt

HAUNT_SERVE ?= $(GUIX_SHELL) \
	-N \
	-C \
	git haunt \
	-- \
	haunt serve

HAUNT_PORT ?= 1968

SENTINEL =

include build/get-wordpress-article-url.scm

.PHONY: \
	all \
	build \
	generate-wordpress-articles \
	haunt-blog.diff
	help \
	other-tests \
	posts \
	publish \
	serve \
	haunt-blog_revision.txt \
	update-makefile \
	update-makefile-tests \
	$(SENTINEL)

all: help

help:
	@printf "%s\n\t%s\n\t%s\n\t%s\n\t%s\n\n%s\n\t%s\n\t%s\n\t%s\n\t%s\n" \
	"General commands:" \
	"make serve       # run haunt serve -w -p $(HAUNT_PORT)" \
	"make publish     # Copy the html files to https://haunt.replicant.us" \
	"make check       # Run tests." \
	"make build       # Generate the HTML files with haunt." \
	"Commands that regenerate files:" \
	"make links.txt   # Create links.txt file with blog post links." \
	"                 # For security reasons This file needs to be manually" \
	"                 # reviewed manually before usage." \
	"make posts       # create blog posts markdown pages. To be done after reviewing links.txt." \

#####################################
# external/vendor_replicant_artwork #
#####################################

VENDOR_REPLICANT_ARTWORK_REV ?= fc213d2ca94cef9047d1e4a71c21c4c4c87f349d
VENDOR_REPLICANT_ARTWORK_PATH ?= external/vendor_replicant_artwork

$(VENDOR_REPLICANT_ARTWORK_PATH):
	git clone \
		https://git.replicant.us/replicant/vendor_replicant_artwork \
		$(VENDOR_REPLICANT_ARTWORK_PATH)

static/replicant_banner_white.png: vendor_replicant_artwork_revision.txt
	install -m 644 \
		$(VENDOR_REPLICANT_ARTWORK_PATH)/replicant_banner_white.png \
		$@

########################
# Source code releases #
########################

%.xz: %
	xz \
	-9e \
	--verbose \
	--keep \
	--force \
	$^

source/%.xz: %.xz
	install -D -m 644 $^ $@

HAUNT_BLOG_REV := $(shell git --no-pager log --oneline HEAD -1 --format='%H')

haunt-blog_revision.txt:
	if [ ! -f $@ ] || [ "`cat $@`" != "$(HAUNT_BLOG_REV)" ] ; then \
		echo "$(HAUNT_BLOG_REV)" > $@ ;                          \
	fi

haunt-blog.diff:
	git diff --output=$@

haunt-blog.tar: Makefile haunt-blog_revision.txt
	git \
	archive \
	--format=tar \
	--prefix=haunt-blog/ \
	-o $@ \
	HEAD

vendor_replicant_artwork_revision.txt: $(VENDOR_REPLICANT_ARTWORK_PATH) Makefile
	if [ ! -f $@ ] || \
		[ "`cat $@`" != "$(VENDOR_REPLICANT_ARTWORK_REV)" ] ;  \
	then \
		rm -f $@ ;                                             \
		git -C $(VENDOR_REPLICANT_ARTWORK_PATH) fetch origin ; \
		git -C $(VENDOR_REPLICANT_ARTWORK_PATH)                \
			checkout -f $(VENDOR_REPLICANT_ARTWORK_REV) ;  \
		git -C $(VENDOR_REPLICANT_ARTWORK_PATH) reset --hard ; \
		git -C $(VENDOR_REPLICANT_ARTWORK_PATH) clean -dfx ;   \
		echo $(VENDOR_REPLICANT_ARTWORK_REV) > $@ ;            \
	fi

vendor_replicant_artwork.tar: Makefile vendor_replicant_artwork_revision.txt
	git \
	-C external/vendor_replicant_artwork/ \
	archive \
	--format=tar \
	--prefix=haunt-blog/external/vendor_replicant_artwork/ \
	-o ../../$@ \
	HEAD

COMPLETE_AND_CORRESPONDING_SOURCE = \
	source/haunt-blog.diff.xz \
	source/haunt-blog.tar.xz \
	source/haunt-blog_revision.txt.xz \
	source/vendor_replicant_artwork.tar.xz \
	source/vendor_replicant_artwork_revision.txt.xz \
	$(SENTINEL)

###################################
# Regeneration of blog post files #
###################################

check-for-guile-support-in-make:
	@if [ "$(findstring guile,$(.FEATURES))" = "" ] ; then \
		echo "Make is missing Guile support." && \
		echo "Guix's make has Guile support." && exit 69 ; \
	fi

wordpress/pages:
	mkdir -p $@

wordpress/pages/replicant_blog_page_%.html: wordpress/pages
	$(CURL) $(WORDPRESS_BLOG_URL)/$(subst replicant_blog_page_,,$*)/ -o $@

WORDPRESS_PAGES = \
	wordpress/pages/replicant_blog_page_8.html \
	wordpress/pages/replicant_blog_page_7.html \
	wordpress/pages/replicant_blog_page_6.html \
	wordpress/pages/replicant_blog_page_5.html \
	wordpress/pages/replicant_blog_page_4.html \
	wordpress/pages/replicant_blog_page_3.html \
	wordpress/pages/replicant_blog_page_2.html \
	wordpress/pages/replicant_blog_page_1.html \
	wordpress/pages/replicant_blog_page_0.html \
	$(SENTINEL)

links.txt: $(WORDPRESS_PAGES)
	@echo -n '' > $@
	@for f in $(WORDPRESS_PAGES) ; do \
		echo "Processing $$f" > /dev/stderr ; \
		guix shell -C python python-beautifulsoup4 -- \
		python3 get_links.py $$f | sort -u >> $@ ; \
	done

wordpress/articles:
	mkdir -p $@

wordpress/articles/%.html: wordpress/articles check-for-guile-support-in-make
	$(CURL) "$(guile $(get-wordpress-article-url))" -o $@

posts/%.md: wordpress/articles/%.html
	mkdir -p posts
	$(CONVERT) $^ $@

# do not depend on links.txt as a human is supposed to review it manually
update-makefile:
	bash -c 'tmp=`mktemp` && \
		 guile \
		 -L . \
		 -c \
		 "(display (@@ (build update-makefile) new-makefile-text))" \
		 > $${tmp} && \
		 cp $${tmp} Makefile && rm -f $${tmp}'

# If there is a POSTS variable below it is auto-generated
# from links.txt by the build/update-makefile.scm script. You can use
# the update-makefile target to update the list below.
POSTS = \
	posts/2010_09_welcome-to-replicant.md \
	posts/2010_11_fdroid-a-free-software-alternative-to-google-market.md \
	posts/2011_03_new-replicant-test-images-available.md \
	posts/2011_04_replicant-2-2-sdk-available.md \
	posts/2011_04_replicant-lacks-tracking-antifeatures.md \
	posts/2011_05_replicant-2-2-preview-007.md \
	posts/2011_06_replicant-2-2-preview-008-is-out.md \
	posts/2011_08_nexus-s-support-in-replicant-being-worked-on.md \
	posts/2011_09_introducing-replicant-on-htc-dream-video.md \
	posts/2011_10_new-replicant-2-2-preview-images-0009-released.md \
	posts/2011_11_replicant-2-3-preview-images-for-nexus-one-released.md \
	posts/2011_11_replicant-on-nexus-s-preview.md \
	posts/2012_01_nexus-s-status-update-with-new-images.md \
	posts/2012_02_wiki-and-tracker-are-now-powered-by-redmine.md \
	posts/2012_03_galaxy-s-support-upcoming.md \
	posts/2012_04_new-set-of-replicant-2-3-images-including-galaxy-s-support.md \
	posts/2012_04_nexus-sgalaxy-s-privacy-issues.md \
	posts/2012_09_replicant-2-3-0004-images-release.md \
	posts/2012_09_replicant-2-3-0005-images-fixing-the-ussd-vulnerability.md \
	posts/2012_11_replicant-4-0-0001-images-release.md \
	posts/2013_01_replicant-4-0-sdk-release.md \
	posts/2013_02_replicant-4-0-0002-images-release.md \
	posts/2013_04_replicant-4-0-0003-images-release.md \
	posts/2013_07_announcing-the-free-software-foundation-fundraising-program-for-replicant.md \
	posts/2013_07_replicant-4-0-0004-images-release.md \
	posts/2013_08_opening-the-replicant-forums.md \
	posts/2013_10_replicant-4-0-0005-images-release.md \
	posts/2013_11_fairphone.md \
	posts/2014_01_replicant-4-2-kicks-out.md \
	posts/2014_03_unveiling-the-samsung-galaxy-back-door.md \
	posts/2014_06_replicant-4-2-on-the-goldelico-gta04.md \
	posts/2014_06_replicant-at-the-15th-libre-software-meeting-in-montpellier-france-this-july.md \
	posts/2014_07_replicant-4-2-0002-images-release.md \
	posts/2014_09_replicant-homepage-and-roadmap-for-the-future.md \
	posts/2014_12_replicant-4-2-0003-images-release.md \
	posts/2015_01_replicant-at-fosdem-2015.md \
	posts/2015_04_whats-happening-or-not-at-replicant.md \
	posts/2015_07_replicant-source-code-hosting-and-rmll-2015.md \
	posts/2015_07_rmll-feedback-optimus-black-advancement-and-cccamp.md \
	posts/2015_08_replicant-and-friends-at-chaos-communication-camp-2015.md \
	posts/2015_09_replicant-4-2-0004-images-release.md \
	posts/2015_09_upcoming-events-for-replicant-in-octobernovember-2015.md \
	posts/2015_12_shops-selling-devices-pre-installed-with-replicant.md \
	posts/2016_01_replicant-at-fosdem-2016.md \
	posts/2016_05_coliberator-2016-keynotes-and-workshops.md \
	posts/2016_07_media-from-2016-replicant-talks.md \
	posts/2016_07_replicant-at-pseshsf-2016.md \
	posts/2016_08_replicant-6-early-work-upstream-work-and-f-droid-issue.md \
	posts/2017_02_replicant-6-0-development-updates.md \
	posts/2017_04_there-wont-be-a-replicant-6-0-sdk-because-there-is-already-something-better.md \
	posts/2017_05_replicant-6-0-released.md \
	posts/2017_09_a-new-replicant-6-0-release.md \
	posts/2017_12_contributions-to-arcep-work-on-terminal-devices-and-public-consultation.md \
	posts/2017_12_third-replicant-6-0-release.md \
	posts/2018_01_les-terminaux-sont-ils-le-maillon-faible-de-louverture-dinternet.md \
	posts/2018_03_final-days-for-the-upstream-linux-allwinner-vpu-support-crowdfunding-campaign.md \
	posts/2019_01_meeting-point-at-fosdem.md \
	posts/2019_01_replicant-meeting-at-fosdem-2019.md \
	posts/2019_01_replicant-will-receive-a-device-from-necuno-solutions.md \
	posts/2019_06_replicant-contributors-meeting-the-2728-july-in-france.md \
	posts/2019_07_graphics-support-for-replicant-9.md \
	posts/2019_12_replicant-36c3.md \
	posts/2020_01_replicant-at-fosdem-2020.md \
	posts/2020_04_what-could-go-wrong-with-devices-that-have-non-replaceable-batteries.md \
	posts/2020_07_late-report-from-fosdem-2020.md \
	posts/2020_12_call-for-a-community-manager.md \
	posts/2021_05_presenting-replicants-community-manager.md \
	posts/2021_06_replicant-is-looking-for-volunteers-to-add-support-for-the-gsd4t-gps-chip.md \
	posts/2022_06_new-replicant-6-0-0004-release-and-replicant-11-status.md \
	posts/2023_02_replicant-at-fosdem-2023.md \
	posts/2023_12_replicant-37c3.md \
	posts/2024_03_replicant-status-and-report-of-the-37c3-and-fosdem-2024-conferences.md \
	$(SENTINEL)

posts: $(POSTS)

######################
# Haunt -> WordPress #
######################

generate-wordpress-articles: build
	$(GUIX_SHELL) -C $(GUIX_PYTHON_PACKAGES) \
	-- \
	python3 haunthtml2wordpress.py \
	./site/future-of-ngi-funding.html

####################
# General commands #
####################

build: static/replicant_banner_white.png $(COMPLETE_AND_CORRESPONDING_SOURCE)
	$(HAUNT) build

publish: build
	rsync -av --delete \
	site/ \
	root@git.replicant.us:/var/www/haunt.replicant.us/ \
	$(SENTINEL)

serve: build
	$(HAUNT_SERVE) -w -p $(HAUNT_PORT)

#########
# Tests #
#########

check: other-tests update-makefile-tests

other-tests: build
	tests/tests.sh

update-makefile-tests:
	guile -L . tests/update-makefile.scm
