collection_name = 45_platform

# Set default prefix if called by the root account, for system-wide install.
UID := $(shell id --user)
ifeq ($(UID),0)
    prefix = /usr/local
endif
# Set install path based on $(prefix), if provided.
ifneq ($(prefix),)
    datadir = $(prefix)/share
endif
# Fall back on $XDG_DATA_HOME, defaulting to ~/.local/share
ifeq ($(prefix),)
    ifeq ($(XDG_DATA_HOME),)
        XDG_DATA_HOME := $(HOME)/.local/share
    endif
    datadir = $(XDG_DATA_HOME)
endif
collections_path = $(DESTDIR)$(datadir)/play.it/collections

# Install the game scripts

.PHONY: install uninstall

install:
	install -D --mode=755 --target-directory=$(collections_path)/$(collection_name) games/*

uninstall:
	rm $(collections_path)/$(collection_name)/play-*.sh
	rmdir -p --ignore-fail-on-non-empty $(collections_path)/$(collection_name)

# Generate tarballs, for easier packaging

.PHONY: dist

## The generated tarball is signed with gpg by default,
## NO_SIGN should be set to a non-0 value to skip the signature.
NO_SIGN := 0

dist: DATE := $(shell date +%F)
dist: TARBALL := $(collection_name)_$(DATE).tar.gz
dist: TAR_OPTIONS := --sort=name --mtime=2023-08-25 --owner=root:0 --group=root:0 --use-compress-program='gzip --no-name'
dist: games/*.sh tests/check-license.sh COPYING licenses Makefile README.md
	mkdir --parents dist
	LC_ALL=C tar cf dist/$(TARBALL) $(TAR_OPTIONS) games/*.sh tests/check-license.sh COPYING licenses Makefile README.md
ifeq ($(NO_SIGN),0)
	rm --force dist/$(TARBALL).asc
	gpg --armor --detach-sign dist/$(TARBALL)
endif

# Run license check, to ensure no contributor is missing

.PHONY: check check-license
check: check-license
check-license:
	./tests/check-license.sh
