SHELL = /bin/sh

# © 2023–2024 Lady [@ Lady’s Computer].
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.

AWK := awk
ECHO := echo
GREP := grep
ICONV := iconv
SED := sed
TEST := test

GIT := git
GITFORCE :=
GITOPTS :=

# This Makefile requires rsync 3 or newer.
RSYNC := rsync
ifneq ($(wildcard .rsync-filter),)
RSYNCFILTER := .rsync-filter
endif
RSYNCOPTS := --checksum --compress --del --links --omit-dir-times --prune-empty-dirs --recursive --times --verbose

BUILDTARGET := .grass
DESTDIR := .

CLIENTCHARSET := $(if $(shell $(ICONV) -l | $(GREP) -i 'utf-8-mac' - || true),utf-8-mac,utf-8)
SERVER := computer
SERVERCHARSET := utf-8
SERVERPATH := $(notdir $(abspath .))

override comma := ,

ifneq ($(wildcard $(BUILDTARGET)),)
override buildtime := $(shell stat -f '%m' $(BUILDTARGET))
endif

override committime := $(shell $(GIT) $(GITOPTS) log -1 --format='%ct' | $(AWK) '{print $$NF}' || true)
ifneq ($(committime),)
override gitstatus := $(shell $(GIT) $(GITOPTS) status --porcelain)
endif

ensure-build:
ifeq ($(committime),)
	@$(ECHO) 'Error: Unable to get commit time of most recent commit!' >&2
	@false
endif
ifeq ($(buildtime),)
	@$(ECHO) 'Error: The website has not been built yet!' >&2
	@$(ECHO) 'Run `make´ before syncing.' >&2
	@false
endif
	@if $(TEST) '$(committime)' -gt '$(buildtime)'; then $(ECHO) 'Error: A commit was made after the last build!' >&2; $(ECHO) 'Run `make´ before syncing.' >&2; false; fi

ensure-branch-up-to-date:
ifeq ($(committime),)
	@$(ECHO) 'Error: Unable to get commit time of most recent commit!' >&2
	@false
endif
	$(GIT)$(if $(GITOPTS), $(GITOPTS),) fetch
	@if ! $(GIT) $(GITOPTS) merge-base --is-ancestor @{u} HEAD; then $(ECHO) 'Error: This branch is currently out‐of‐date!' >&2; $(ECHO) 'Pull in changes with `$(GIT)$(if $(GITOPTS), $(GITOPTS),) pull´ before syncing.' >&2; false; fi

ensure-clean:
ifneq ($(gitstatus),)
	@$(ECHO) 'Error: There are uncommitted changes!' >&2
	@$(ECHO) 'Commit changes and run `make´ before syncing.' >&2
	@false
endif

dry-sync: ensure-clean$(if $(GITFORCE),, ensure-branch-up-to-date) ensure-build
	cd $(DESTDIR) && $(RSYNC) --dry-run$(if $(RSYNCFILTER), --filter='. $(abspath $(RSYNCFILTER))',)$(if $(and $(CLIENTCHARSET),$(SERVERCHARSET),$(filter-out $(CLIENTCHARSET),$(SERVERCHARSET))), --iconv='$(CLIENTCHARSET)$(comma)$(SERVERCHARSET)',) $(RSYNCOPTS) . $(SERVER):$(SERVERPATH)

sync: ensure-clean$(if $(GITFORCE),, ensure-branch-up-to-date) ensure-build
	$(GIT)$(if $(GITOPTS), $(GITOPTS),) push$(if $(GITFORCE), --force,)
	cd $(DESTDIR) && $(RSYNC)$(if $(RSYNCFILTER), --filter='. $(abspath $(RSYNCFILTER))',)$(if $(and $(CLIENTCHARSET),$(SERVERCHARSET),$(filter-out $(CLIENTCHARSET),$(SERVERCHARSET))), --iconv='$(CLIENTCHARSET)$(comma)$(SERVERCHARSET)',) $(RSYNCOPTS) . $(SERVER):$(SERVERPATH)

.PHONY: dry-sync ensure-branch-up-to-date ensure-build ensure-clean sync;