# The GHOUL makefile ===========================================================

# Some snippets...
# find /var/www/html -type d -exec chmod 0755 {} \;
# find /var/www/html -type f -exec chmod 0644 {} \;

# Without walls or fences, who needs Windows or Gates?
# MAN! I love Linux!

# Settings =====================================================================
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
#MAKEFLAGS += -j 1
#.NOTPARALLEL:
.RECIPEPREFIX := >

RM := rm -f
RMDIR := rm -rf
MKDIR := mkdir -p
ERRIGNORE := 2>/dev/null
SEP := /

# Hide calls; delete "@" from 'HIDE =  @' to show calls.
HIDE := @

# Directories ==================================================================
PROJDIR := $(realpath $(CURDIR))
SOURCEDIR := $(PROJDIR)/source
SITEDIR := $(PROJDIR)/site
# SET PUBLISHDIR TO WHAT YOU NEED IT TO BE !!! =================================
PUBLISHDIR := /var/www/html/template
# BEFORE YOU UNCOMMENT THE PUBLISHING LINES BELOW !!! ==========================
DOCINFODIR := $(SOURCEDIR)/docinfo
SENTINELDIR := $(SOURCEDIR)/sentinels

# Store some file-lists in some variables... ===================================
PARTIALORGS := $(SOURCEDIR)/partials/*

DOCINFODIRS := $(shell find $(SOURCEDIR) -type d -wholename '*/docinfo' | grep -v -e /source/docinfo)
DOCINFOORGS := $(DOCINFODIR)/docinfo*
DOCINFO_STL := $(SENTINELDIR)/docinfo

# All assets except the zipfiles in the /source/assets/ directory (i.e. TheGHOUL.zip and GHOULDOCS.zip)
ASSETSORGS := $(shell find $(SOURCEDIR) -type f -wholename '*/assets/*' | grep -v -e template -e /_icons/ -e 'source/assets/.*zip')
ASSETSDEST := $(subst $(SOURCEDIR),$(SITEDIR),$(ASSETSORGS))
ASSET_STL := $(SENTINELDIR)/assets

CSSORGS := $(shell find $(SOURCEDIR) -type f -name '*.css' | grep -v template)
CSSDEST := $(subst $(SOURCEDIR),$(SITEDIR),$(CSSORGS))
CSS_STL := $(SENTINELDIR)/css

IMAGEORGS := $(shell find $(SOURCEDIR) -type f -wholename '*/images/*' | grep -v template)
IMAGEDEST := $(subst $(SOURCEDIR),$(SITEDIR),$(IMAGEORGS))
IMAGE_STL := $(SENTINELDIR)/images

# .adoc sources, minus files from non-page containing directories.
ADOC := $(shell find $(SOURCEDIR) -type f -name '*.adoc' | grep -v -e /partials/ -e /assets/ -e /docinfo/ -e /css/ -e template)
HTML := $(subst $(SOURCEDIR),$(SITEDIR),$(ADOC:.adoc=.html))

# Uncomment the next two lines out if you're publishing the website.
#PUBORGS = $(shell find $(SITEDIR) -type f -name '*')
#PUBDEST = $(subst $(SITEDIR),$(PUBLISHDIR),$(PUBORGS))

# Here comes the action... =====================================================
.PHONY: all copy process publish zips debug

# Default rule.
all: publish

# The individual rules =========================================================
copy: $(DOCINFO_STL) $(ASSETSDEST) $(CSSDEST) $(IMAGEDEST)

#docinfo: $(DOCINFO)
# Bring *shared* docinfo files up to date throughout the source directory. These are distributed to all docinfo directories. Private docinfo files are ignored so pages can have their own private docinfo files if needed. A sentinel file is touched to 'store' the last-updated time.
$(DOCINFO_STL): $(DOCINFOORGS)
> $(HIDE)for dir in $(DOCINFODIRS); \
    do \
      cp $(DOCINFOORGS) $$dir; \
    done
> $(HIDE)touch $@

#check this
$(IMAGE_STL): $(IMAGEORGS)
> $(HIDE)for thing in $(IMAGEORGS); \
    do \
      cp $$thing ${thing/$(SOURCEDIR)/$(SITEDIR)}; \
    done
> $(HIDE)touch $@

$(SITEDIR)/%: $(SOURCEDIR)/%
> $(HIDE)$(MKDIR) $(dir $@)
> $(HIDE)cp $< $@

process: $(HTML) copy

$(SITEDIR)/%.html: $(SOURCEDIR)/%.adoc $(DOCINFO_STL) $(PARTIALORGS)
> $(HIDE)$(MKDIR) $(dir S@)
> $(HIDE)asciidoctor -D $(dir $@) $<

publish: $(PUBDEST) process

$(PUBLISHDIR)/%: $(SITEDIR)/%
> $(HIDE)$(MKDIR) $(dir $@)
> $(HIDE)cp $< $@

# Just in case...
debug:
> @echo $(IMAGEORGS)
