experiments with writing emacs packages, and with pijul
TOP := $(dir $(lastword $(MAKEFILE_LIST)))

## User options ######################################################
#
# You can override these settings in "config.mk" or on the command
# line.
#
# You might also want to set LOAD_PATH.  If you do, then it must
# contain "-L .".
#
# If you don't do so, then the default is set in the "Load-Path"
# section below.  The default assumes that all dependencies are
# installed either at "../<DEPENDENCY>", or when using package.el
# at "ELPA_DIR/<DEPENDENCY>-<HIGHEST-VERSION>".

PREFIX   ?= /usr/local
sharedir ?= $(PREFIX)/share
lispdir  ?= $(sharedir)/emacs/site-lisp/majul
infodir  ?= $(sharedir)/info
docdir   ?= $(sharedir)/doc/majul

CP       ?= install -p -m 644
MKDIR    ?= install -p -m 755 -d
RMDIR    ?= rm -rf
TAR      ?= tar
SED      ?= sed

EMACS    ?= emacs
BATCH     = $(EMACS) -Q --batch $(LOAD_PATH)

LISP_EXTRA_TARGETS ?= check-declare

INSTALL_INFO     ?= $(shell command -v ginstall-info || printf install-info)
MAKEINFO         ?= makeinfo
MANUAL_HTML_ARGS ?= --css-ref /assets/page.css

GITSTATS      ?= pijulstats
GITSTATS_DIR  ?= $(TOP)docs/stats
GITSTATS_ARGS ?= -c style=https://majul.vc/assets/stats.css -c max_authors=999

BUILD_MAGIT_LIBGIT ?= false

## Files #############################################################

PKG       = majul
PACKAGES  = majul majul-section pijul-commit

TEXIPAGES = $(addsuffix .texi,$(filter-out pijul-commit,$(PACKAGES)))
INFOPAGES = $(addsuffix .info,$(filter-out pijul-commit,$(PACKAGES)))
HTMLFILES = $(addsuffix .html,$(filter-out pijul-commit,$(PACKAGES)))
HTMLDIRS  = $(filter-out pijul-commit,$(PACKAGES))
PDFFILES  = $(addsuffix .pdf,$(filter-out pijul-commit,$(PACKAGES)))
EPUBFILES = $(addsuffix .epub,$(filter-out pijul-commit,$(PACKAGES)))

ELS  = pijul-commit.el
ELS += majul-section.el
ELS += majul-base.el
ifeq "$(BUILD_MAGIT_LIBGIT)" "true"
ELS += majul-libpijul.el
endif
ELS += majul-pijul.el
ELS += majul-mode.el
ELS += majul-margin.el
ELS += majul-process.el
ELS += majul-transient.el
ELS += majul-autorevert.el
ELS += majul-core.el
ELS += majul-diff.el
ELS += majul-log.el
ELS += majul-wip.el
ELS += majul-reflog.el
ELS += majul-apply.el
ELS += majul-repos.el
ELS += majul.el
ELS += majul-status.el
ELS += majul-refs.el
ELS += majul-files.el
ELS += majul-reset.el
ELS += majul-branch.el
ELS += majul-merge.el
ELS += majul-tag.el
ELS += majul-worktree.el
ELS += majul-notes.el
ELS += majul-obsolete.el
ELS += majul-sequence.el
ELS += majul-commit.el
ELS += majul-remote.el
ELS += majul-clone.el
ELS += majul-fetch.el
ELS += majul-pull.el
ELS += majul-push.el
ELS += majul-patch.el
ELS += majul-bisect.el
ELS += majul-stash.el
ELS += majul-blame.el
ELS += majul-sparse-checkout.el
ELS += majul-submodule.el
ELS += majul-subtree.el
ELS += majul-ediff.el
ELS += majul-pijulignore.el
ELS += majul-bundle.el
ELS += majul-extras.el
ELS += pijul-rebase.el
ELS += majul-bookmark.el
ELCS = $(ELS:.el=.elc)
ELMS = majul.el $(filter-out $(addsuffix .el,$(PACKAGES)),$(ELS))
ELGS = majul-autoloads.el majul-version.el

## Versions ##########################################################

VERSION ?= $(shell \
  test -e $(TOP).pijul && \
  pijul describe --tags --abbrev=0 --always | cut -c2-)
TIMESTAMP = 20211004

COMPAT_VERSION        = 28.1.0.4
DASH_VERSION          = 2.19.1
GIT_COMMIT_VERSION    = $(VERSION)
LIBGIT_VERSION        = 0
MAGIT_VERSION         = $(VERSION)
MAGIT_LIBGIT_VERSION  = $(VERSION)
MAGIT_SECTION_VERSION = $(VERSION)
TRANSIENT_VERSION     = 0.3.6
WITH_EDITOR_VERSION   = 3.0.5

COMPAT_SNAPSHOT              = 28.1.0.4
DASH_MELPA_SNAPSHOT          = 20210826
GIT_COMMIT_MELPA_SNAPSHOT    = $(TIMESTAMP)
LIBGIT_MELPA_SNAPSHOT        = 0
MAGIT_MELPA_SNAPSHOT         = $(TIMESTAMP)
MAGIT_LIBGIT_MELPA_SNAPSHOT  = $(TIMESTAMP)
MAGIT_SECTION_MELPA_SNAPSHOT = $(TIMESTAMP)
TRANSIENT_MELPA_SNAPSHOT     = 20210920
WITH_EDITOR_MELPA_SNAPSHOT   = 20211001

EMACS_VERSION        = 25.1
LIBGIT_EMACS_VERSION = 26.1

EMACSOLD := $(shell $(BATCH) --eval \
  "(and (version< emacs-version \"$(EMACS_VERSION)\") (princ \"true\"))")
ifeq "$(EMACSOLD)" "true"
  $(error At least version $(EMACS_VERSION) of Emacs is required)
endif

## Load-Path #########################################################

# Remember to also update majul-emacs-Q-command!

ifndef LOAD_PATH

USER_EMACS_DIR = $(HOME)/.emacs.d
ifeq "$(wildcard $(USER_EMACS_DIR))" ""
  XDG_CONFIG_DIR = $(or $(XDG_CONFIG_HOME),$(HOME)/.config)
  ifneq "$(wildcard $(XDG_CONFIG_DIR)/emacs)" ""
    USER_EMACS_DIR = $(XDG_CONFIG_DIR)/emacs
  endif
endif

ELPA_DIR ?= $(USER_EMACS_DIR)/elpa

COMPAT_DIR ?= $(shell \
  find -L $(ELPA_DIR) -maxdepth 1 -regex '.*/compat-[.0-9]*' 2> /dev/null | \
  sort | tail -n 1)
ifeq "$(COMPAT_DIR)" ""
  COMPAT_DIR = $(TOP)../compat
endif

DASH_DIR ?= $(shell \
  find -L $(ELPA_DIR) -maxdepth 1 -regex '.*/dash-[.0-9]*' 2> /dev/null | \
  sort | tail -n 1)
ifeq "$(DASH_DIR)" ""
  DASH_DIR = $(TOP)../dash
endif

LIBGIT_DIR ?= $(shell \
  find -L $(ELPA_DIR) -maxdepth 1 -regex '.*/libpijul-[.0-9]*' 2> /dev/null | \
  sort | tail -n 1)
ifeq "$(LIBGIT_DIR)" ""
  LIBGIT_DIR = $(TOP)../libpijul
endif

TRANSIENT_DIR ?= $(shell \
  find -L $(ELPA_DIR) -maxdepth 1 -regex '.*/transient-[.0-9]*' 2> /dev/null | \
  sort | tail -n 1)
ifeq "$(TRANSIENT_DIR)" ""
  TRANSIENT_DIR = $(TOP)../transient/lisp
endif

WITH_EDITOR_DIR ?= $(shell \
  find -L $(ELPA_DIR) -maxdepth 1 -regex '.*/with-editor-[.0-9]*' 2> /dev/null | \
  sort | tail -n 1)
ifeq "$(WITH_EDITOR_DIR)" ""
  WITH_EDITOR_DIR = $(TOP)../with-editor/lisp
endif

MAGIT_SECTION_DIR ?= $(shell \
  find -L $(ELPA_DIR) -maxdepth 1 -regex '.*/majul-section-[.0-9]*' 2> /dev/null | \
  sort | tail -n 1)

SYSTYPE := $(shell $(EMACS) -Q --batch --eval "(princ system-type)")
ifeq ($(SYSTYPE), windows-nt)
  CYGPATH := $(shell cygpath --version 2>/dev/null)
endif

LOAD_PATH = -L $(TOP)lisp

# When making changes here, then don't forget to adjust "Makefile",
# ".pijulhub/workflows/test.yml", ".pijulhub/ISSUE_TEMPLATE/bug_report.md",
# `majul-emacs-Q-command' and the "Installing from the Pijul Repository"
# info node accordingly.  Also don't forget to "rgrep \b<pkg>\b".

ifdef CYGPATH
  LOAD_PATH += -L $(shell cygpath --mixed $(COMPAT_DIR))
  LOAD_PATH += -L $(shell cygpath --mixed $(DASH_DIR))
  LOAD_PATH += -L $(shell cygpath --mixed $(LIBGIT_DIR))
  LOAD_PATH += -L $(shell cygpath --mixed $(TRANSIENT_DIR))
  LOAD_PATH += -L $(shell cygpath --mixed $(WITH_EDITOR_DIR))
  ifneq "$(MAGIT_SECTION_DIR)" ""
    LOAD_PATH += -L $(shell cygpath --mixed $(MAGIT_SECTION_DIR))
  endif
else
  LOAD_PATH += -L $(COMPAT_DIR)
  LOAD_PATH += -L $(DASH_DIR)
  LOAD_PATH += -L $(LIBGIT_DIR)
  LOAD_PATH += -L $(TRANSIENT_DIR)
  LOAD_PATH += -L $(WITH_EDITOR_DIR)
  ifneq "$(MAGIT_SECTION_DIR)" ""
    LOAD_PATH += -L $(MAGIT_SECTION_DIR)
  endif
endif

endif # ifndef LOAD_PATH

ifndef ORG_LOAD_PATH
ORG_LOAD_PATH = -L ../../org/lisp
endif

## Publish ###########################################################

DOMAIN      ?= majul.vc
CFRONT_DIST ?= E2LUHBKU1FBV02

PUBLISH_TARGETS ?= html html-dir pdf

DOCBOOK_XSL ?= /usr/share/xml/docbook/stylesheet/docbook-xsl/epub/docbook.xsl

EPUBTRASH = epub.xml META-INF OEBPS