# We assume we've been checked out into a parallel directory to the
# upstream Fossil trunk sources.
UPSTREAM := ../../trunk

# Get upstream's current commit ID prefix to use as a unique version
# tag for our builds.  Do not confuse that with _this_ repo's commit
# ID.  Not only do these two repos have an independent commit history,
# no one wants to manually pop the extra indirection level when looking
# at an image tag.
GGREP  := $(shell command -v ggrep grep | head -1)
FSLVER := $(shell cut -c1-12 < $(UPSTREAM)/manifest.uuid)
FSLREL := $(shell cd $(UPSTREAM) ; fossil tag ls | $(GGREP) -P 'version-\d' | tail -1)
SRCTB  := src-$(FSLVER).tar.gz
USRCTB := $(UPSTREAM)/$(SRCTB)

# Image repo to use
PRIREG   := quay.io
SECREG   := docker.io
PRIORG   := tangent
SECORG   := tangentsoft
NAME     := fossil
PRIBNAME := $(PRIREG)/$(PRIORG)/$(NAME)
SECBNAME := $(SECREG)/$(SECORG)/$(NAME)
PRILTAG  := $(PRIBNAME):latest
SECLTAG  := $(SECBNAME):latest
PRIVTAG  := $(PRIBNAME):$(FSLVER)
SECVTAG  := $(SECBNAME):$(FSLVER)

# Which "farm" to build on.  If you don't name it, it will use the
# first one found, and if none such exist, it will build on the local
# host only.  See the farm-* targets for what we put behind this name.
FARM := fossil-build

# Create container image, with automatic push.  We have to tag it
# manually as a separate step due to a bug in --tag parsing when it
# contains a colon, at least as of Podman 5.3.1.
all: $(USRCTB)
	-podman untag $(PRILTAG) 2> /dev/null
	podman farm build \
		--farm $(FARM) \
		--build-arg UID=1000 \
		--build-arg FSLURL=$(SRCTB) \
		--build-arg FSLVER=$(FSLVER) \
		--tag $(PRIBNAME) \
		$(DBFLAGS) $(UPSTREAM)
	for t in $(PRIVTAG) $(SECVTAG) $(SECLTAG) ; do \
		podman tag  $(PRILTAG) $$t && \
		podman push $$t ; \
	done

# Create a build farm on an ARM64 Linux server with 2 remote "farmers",
# one a 64-bit Intel box on the LAN having a CNAME entry of "amd64" and
# the other an ARMv7 QEMU VM called arm32.
#
# We don't tie this into the build to make it run automatically because
# it's contingent on the local environment.  This exists to document the
# type of thing that must exist before the "all" target can succeed.
farm-linux:
	podman system connection add local-user unix://$$XDG_RUNTIME_DIR/podman/podman.sock
	podman system connection add --identity ~/.ssh/farm arm32 user@arm32
	podman system connection add --identity ~/.ssh/farm amd64 user@amd64
	podman farm create fossil-build
	podman farm update --add local-user,amd64 $(FARM)

# Similar, but on an Apple Silicon macOS host, where we orchestrate the
# build from Podman's background VM, which must have farmer keys.  For
# an Intel Mac, change all instances of "arm64" to "amd64".
farm-macos:
	podman machine ssh install -m 600 /dev/null .ssh/farm
	podman machine ssh install -m 600 /dev/null .ssh/farm.pub
	podman machine ssh dd of=.ssh/farm     < ~/.ssh/farm
	podman machine ssh dd of=.ssh/farm.pub < ~/.ssh/farm.pub
	podman system connection add --identity ~/.ssh/farm arm32 user@arm32
	podman system connection add --identity ~/.ssh/farm arm64 user@arm64
	podman farm create fossil-build
	podman farm update --add podman-machine-default,arm64 $(FARM)

login:
	podman login --username $(PRIORG) $(PRIREG)
	podman login --username $(SECORG) $(SECREG)

# Override defaults to make a release image instead of a trunk image
release:
	cd ../../release ; fossil update release
	$(MAKE) \
		FSLVER=$(FSLREL) \
		UPSTREAM=../../release

$(USRCTB):
	cd $(UPSTREAM) ; ./fossil tarball --name src $(FSLVER) $(SRCTB)
