#!/usr/bin/make -f

# Uncomment the following to enable more makefile output
#export DH_VERBOSE = 1

#
# The merge directory holds intermediate stages in setting
# up an image. It is not needed to build DUE into a .deb,
# but it will interfere with Git seeing a clean directory.
#
MERGE_DIR = due-build-merge

# Path to install of Pandoc.
PANDOC_PRESENT = /usr/bin/pandoc

# Manual page is generated from manpage-due-1.md via Pandoc
MAN_PAGE = docs/due.1


# Discover any template builds before make targets
# Forces wildcard cache to be up to date
build:

# Build
%:
	dh $@

.PHONY: docs

docs: $(MAN_PAGE)
# Docs will not automatically rebuild.
# Run:
#      /usr/bin/make -f ./debian/rules docs
# To update documentation.
# If Pandoc is installed generate the man page from man.md
# Otherwise, use the last checked in version of it.
#  Odds are any users building this will be looking to build the
#  installer and not make changes to the man pages.
# Pandoc can pull in 50-150 MB of additional files, which may be
#  a bit of an ask.
ifneq ($(wildcard $(PANDOC_PRESENT)),)
	@echo ""
	@echo "#######################################################################"
	@echo "# Pandoc detected: updating documentation "
	@echo "# Removing existing $(MAN_PAGE) "
	rm $(MAN_PAGE)
	@echo "# Generating new man page from docs/manpage-due-1.md "
	pandoc --standalone --to man docs/manpage-due-1.md -o $(MAN_PAGE)
	@echo ""
	/bin/ls -lrt ./docs
	@echo ""
	@echo "#######################################################################"
	@echo ""
else
	@echo ""
	@echo "#######################################################################"
	@echo "# Pandoc is not installed. NOT regenerating due.1 man page            #"
	@echo "# If you want to update the man pages:                                #"
	@echo "#    apt-get install pandoc                                           #"
	@echo "# ...and retry this make -f debian/control docs                       #"
	@echo "#######################################################################"
	@echo ""
endif


override_dh_clean:
# remove intermediate merge directories, if they exist
ifneq ($(wildcard $(MERGE_DIR)),)
	@echo ""
	@echo "  Note: $(MERGE_DIR) work directory exists."
	@echo "   Consider removing it when building DUE as a Debian package."
	@echo ""
else
	@echo "Making clean."
endif

# Execute Debian clean
	dh_clean
