VERSION := 0.1.4
NAME := ghostty-tex
DIST := dist
PKG := $(NAME)-$(VERSION)
SOURCE_PKG := $(PKG)-source
FILES := ghostty-tex.el ghostty-tex-pkg.el ghostty-tex-early.el \
	kitty-graphics.el README.md LICENSE KITTY-GRAPHICS-LICENSE
PACKAGE_TEST := env \
	GHOSTTY_TEX_PACKAGE="$(abspath $(DIST)/$(PKG).tar)" \
	GHOSTTY_TEX_VERSION="$(VERSION)" \
	emacs -Q --batch -l test/package-install-test.el \
	-f ert-run-tests-batch-and-exit
# macOS bsdtar otherwise stores AppleDouble xattrs that package.el treats
# as `._` members and then dies on.
ifeq ($(shell uname),Darwin)
TAR := COPYFILE_DISABLE=1 tar --no-xattrs --no-mac-metadata
else
TAR := tar --format=ustar
endif

.PHONY: dist clean compile test package-test check

compile:
	emacs -Q --batch \
	  --eval "(add-to-list 'load-path default-directory)" \
	  --eval "(setq byte-compile-error-on-warn nil)" \
	  -f batch-byte-compile kitty-graphics.el ghostty-tex.el ghostty-tex-early.el

test:
	emacs -Q --batch -L . -L test \
	  -l test/kitty-graphics-test.el \
	  -f ert-run-tests-batch-and-exit

# Build the package first, then install it into a disposable package-user-dir.
package-test: dist
	$(PACKAGE_TEST)

# Keep this sequence explicit: dist starts with clean, while compile should
# leave byte-code behind for the local drop-in deployment.
check: dist
	$(MAKE) compile
	$(MAKE) test
	$(PACKAGE_TEST)

dist: clean
	mkdir -p $(DIST)/$(PKG)
	cp $(FILES) $(DIST)/$(PKG)/
	$(TAR) -C $(DIST) -cf $(DIST)/$(PKG).tar $(PKG)
	$(TAR) -C $(DIST) -czf $(DIST)/$(PKG).tar.gz $(PKG)
	mkdir -p $(DIST)/$(SOURCE_PKG)
	cp $(FILES) Makefile .gitignore $(DIST)/$(SOURCE_PKG)/
	cp -R test $(DIST)/$(SOURCE_PKG)/
	$(TAR) -C $(DIST) -czf $(DIST)/$(SOURCE_PKG).tar.gz $(SOURCE_PKG)
	@echo "package.el:  $(DIST)/$(PKG).tar"
	@echo "drop-in:     $(DIST)/$(PKG).tar.gz"
	@echo "source:      $(DIST)/$(SOURCE_PKG).tar.gz"

clean:
	rm -rf $(DIST) *.elc
