# ---------------------------------------
# Author: David Marsh <rdmarsh@gmail.com>
# ---------------------------------------
#
# This Makefile is used to generate static blog files for rdm.sh
#   for usage: make help
#   more info: make about
#
#   on a mac you'll need the following:
#   brew install imagemagick graphviz yuicompressor pandoc
#
# Copyright (C) 2021--2026 David Marsh rdmarsh@gmail.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

# FILE EXTENSIONS
# ---------------------------------------
# change only if needed

MD   := md
HTML := html
CSS  := css
PNG  := png
JPG  := jpg
JPEG := jpeg
WEBP := webp
DOT  := dot

# SOURCE DIR and FILENAMES
# ---------------------------------------
# change only if needed

srcdir  := _src
imgdir  := _img
bakdir  := ../rdmsh_backup

# WEBSITE SETTINGS
# ---------------------------------------
# change as needed

# site information
site_url   := rdm.sh
site_name  := $(site_url)
site_owner := david marsh
site_desc  := doing things the hard way

# site assets
site_css     := _.css
site_htm     := _.htm
site_ico     := favicon.png
site_img     := _$(site_name).webp
site_img_alt := A head shot of David, with a greying beard, looking at the camera

# date information
year_start   := 2002
#year_current := $(shell date +%Y)
#date_today   := $(shell date +%Y-%m-%d)
DATE := $(shell date +%Y-%m-%d)
year_current := $(word 1,$(subst -, ,$(DATE)))
date_today   := $(DATE)

# license and copyright information
site_license   := [CC-BY-SA-4.0](http://creativecommons.org/licenses/by-sa/4.0/)
site_copyright := &copy;$(year_start)--$(year_current) [$(site_owner)](https://$(site_url)/about.$(HTML)) $(site_license)

# page configuration
rec_qty     := 9 # number of recent posts to show on the front page
page_author := $(site_owner)
page_lang   := en

# site style configuration
# uncomment the style you want to use
#code_style := breezedark #suits dark
#code_style := espresso   #suits neither
#code_style := haddock    #suits light
#code_style := kate       #suits light
code_style  := monochrome #suits both
#code_style := pygments   #suits light
#code_style := tango      #suits light
#code_style := zenburn    #suits dark

# SOCIAL LINKS
# ---------------------------------------
# change as needed; escape bare @ and " symbols due to make file

social_mastodon := <a rel=\"me\" href=\"https://mastodon.au/@davidmarsh\">\@davidmarsh</a>
mastodon_site   := <a href=\"https://joinmastodon.org/\">mastodon</a>

# NEW PAGE DEFAULTS
# ---------------------------------------
# change as needed
# usage: make new title=foobar

title    := title
subtitle := subtitle
slug     := $(shell echo $(title) | tr '[:upper:]' '[:lower:]' | tr " " "_")

# NAV and FOOTER, USE MD and/or HTML
# ---------------------------------------
# change as needed; single quotes only

#20251229 use dirs to build site nav, new code here replacing hardcoded navs

site_nav_exclude := out drafts
site_nav_first := archive lists rpg howto
site_nav_last  := other

NAV_DIRS := $(wildcard $(srcdir)/*/)
NAV_DIRS := $(patsubst $(srcdir)/%/,%,$(NAV_DIRS))
NAV_DIRS := $(filter-out .% _% $(site_nav_exclude),$(NAV_DIRS))
NAV_DIRS := $(sort $(NAV_DIRS))

NAV_DIRS_FIRST_ORDERED := $(filter $(NAV_DIRS),$(site_nav_first))
NAV_DIRS_LAST_ORDERED  := $(filter $(NAV_DIRS),$(site_nav_last))
NAV_DIRS_REST          := $(filter-out $(site_nav_first) $(site_nav_last),$(NAV_DIRS))
NAV_DIRS_REST          := $(sort $(NAV_DIRS_REST))

NAV_DIRS := $(NAV_DIRS_FIRST_ORDERED) $(NAV_DIRS_REST) $(NAV_DIRS_LAST_ORDERED)

NAV_LINKS := $(foreach d,$(NAV_DIRS),[${d}](${d}.$(HTML)))

site_nav := [home](index.$(HTML))
site_nav += $(foreach l,$(NAV_LINKS), | $(l))
site_nav += | [about](about.$(HTML))

#site_nav := [home](/index.$(HTML))
#site_nav += | [archive](/archive.$(HTML))
#site_nav += | [lists](/lists.$(HTML))
#site_nav += | [rpg](/rpg.$(HTML))
#site_nav += | [howto](/howto.$(HTML))
#site_nav += | [other](/other.$(HTML))
#site_nav += | [about](/about.$(HTML))

#20251229 here ends new code

site_footer := $(site_copyright)
site_footer += <br />
site_footer += $(social_mastodon) on $(mastodon_site)

# SPECIAL PAGES
# ---------------------------------------
# change as needed

HTTP_STATUS_PRE := http_status_

#ARCHIVE_TITLE    := the archive
#ARCHIVE_SUBTITLE := here there be dragons and broken links

LIST_PRE      := list_of_
#listsdir eventually to be removed later
listsdir := $(srcdir)/lists
#LIST_TITLE    := lists and collections
#LIST_SUBTITLE := keeping track of things

RPG_PRE      := rpg_
#RPG_TITLE    := rpg content
#RPG_SUBTITLE := posts about rpg related stuff

HOWTO_PRE      := howto_
#HOWTO_TITLE    := how tos
#HOWTO_SUBTITLE := handy how-tos and tips

DRAFT_PRE      := draft_
#DRAFT_TITLE    := drafts
#DRAFT_SUBTITLE := how did you get here?

#OTHER_TITLE    := other pages
#OTHER_SUBTITLE := stuff and things

# COMPILED HTMLS
# ---------------------------------------
# change only if needed

# gather HTML files for different categories
HTML_FILES = $(wildcard *.$(HTML))

LISTS       = $(sort $(wildcard $(LIST_PRE)*.$(HTML)))
RPGS        = $(sort $(wildcard $(RPG_PRE)*.$(HTML)))
#TAGS       = $(sort $(wildcard $(TAG_PRE)*.$(HTML)))
HOWTO       = $(sort $(wildcard $(HOWTO_PRE)*.$(HTML)))
DRAFTS      = $(sort $(wildcard $(DRAFT_PRE)*.$(HTML)))



HTTP_STATUS = $(sort $(wildcard $(HTTP_STATUS_PRE)*.$(HTML)))

# find archived HTML files
#ARCHIVE     = $(shell find * -type f -maxdepth 0 -regex "^[0-9]{4}-[0-9]{2}-[0-9]{2}\..*\.$(HTML)$$" | sort -rn)
ARCHIVE     = $(shell find * -type f -maxdepth 0 -regex "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\..*\.$(HTML)$$" | sort -rn)
RECENTPOSTS = $(wordlist 1 ,$(rec_qty),$(ARCHIVE))

FILTER  = index.$(HTML) archive.$(HTML) lists.$(HTML) rpg.$(HTML) howto.$(HTML) other.$(HTML) drafts.$(HTML) about.$(HTML)
#OTHERS = $(sort $(filter-out $(FILTER) $(ARCHIVE) $(RPGS) $(TAGS) $(LISTS) $(HOWTO) $(DRAFTS), $(wildcard *.$(HTML))))
OTHERS  = $(sort $(filter-out $(FILTER) $(ARCHIVE) $(RPGS) $(LISTS) $(HOWTO) $(DRAFTS) $(HTTP_STATUS), $(HTML_FILES)))

# COMPILE NAV and FOOTER AS HTML
# ---------------------------------------
# do not change

# nav and footer html
nav_html    := <nav>$(site_nav)</nav>
footer_html := <hr /><footer>$(site_footer)<br />[powered by rdm.sh](https://rdm.sh/powered_by.html)</footer>

# include content for the page, we shell to pandoc to convert MD to HTML
include_before = $(shell echo "$(nav_html)" | $(PD) --ascii)
include_after  = $(shell echo "$(footer_html)" | $(PD) --ascii)

# EXECUTABLE AND PROGRAM FLAGS
# ---------------------------------------
# change only if needed

# editor to use for new posts
EDIT ?= vim
#open to last line
EDITFLAGS += +

# pandoc options for html
# note: footnote targets need one blank line between them and href targets
PD ?= pandoc
PDFLAGS += --verbose
PDFLAGS += --from=gfm+tex_math_dollars+footnotes+smart+implicit_figures
PDFLAGS += --to=html
PDFLAGS += --resource-path=$(srcdir)
PDFLAGS += --extract-media=$(imgdir)
PDFLAGS += --wrap=none
PDFLAGS += --standalone
PDFLAGS += --strip-comments
PDFLAGS += --section-divs
PDFLAGS += --email-obfuscation=references
#note: uncomment for toc on every page
#PDFLAGS += --toc
PDFLAGS += --variable=toc-title:"Table of Contents"
#note: makes larger files if you have lots of code, disabled with 'no-highlight'
#PDFLAGS += --highlight-style=$(code_style)
#PDFLAGS += --no-highlight
PDFLAGS += --syntax-highlighting=none
PDFLAGS += --ascii #use only ascii in output

PDFLAGS += --metadata=lang:'$(page_lang)'
#PDFLAGS += --metadata=abstract-title:

PDFLAGS += --css /$(site_css)
PDFLAGS += --template=$(site_htm)
PDFLAGS += --variable=include-before:'$(include_before)'
PDFLAGS += --variable=include-after:'$(include_after)'
PDFLAGS += --variable=author-meta:'$(page_author)'

#these are used specifically for our template
PDFLAGS += --variable=site_name:'$(site_name)'
PDFLAGS += --variable=site_url:'https://$(site_url)'
PDFLAGS += --variable=img_url:'https://$(site_url)/$(imgdir)/'

PDFLAGS += --variable=site_img:'$(site_img)'
PDFLAGS += --variable=site_img_alt:'$(site_img_alt)'

# yui css compressor
YUI ?= yuicompressor
YUFLAGS += --type css

# convert binary
MACOS_VER := $(shell sw_vers -productVersion | cut -d. -f1)
ifeq ($(shell expr $(MACOS_VER) \> 13), 1)
    CON ?= /opt/homebrew/bin/magick
else
    CON ?= /usr/local/bin/magick
endif


# flags for favicon
FAVFLAGS += -strip -define icon:auto-resize=64,48,32,16

# flags for webp images
WEBPFLAGS += -strip -resize 800x\>
JPGFLAGS += -strip -quality 50
PNGFLAGS += -strip -define webp:lossless=true
DOTFLAGS += -bordercolor white -border 10 -define webp:lossless=true

# python options for local test server
SERVER = 127.0.0.1
PORT ?= 8000
PY ?= python3
PYFLAGS += -m http.server
PYFLAGS += --bind $(SERVER) $(PORT)

# rsync options for push
RSYNC ?= rsync
RSFLAGS += -avxzc
RSFLAGS += --del
RSFLAGS += --exclude='.*.swp'
#RSFLAGS += --exclude='$(srcdir)/'
RSFLAGS += --delete-excluded

# tell os to open browser (mac only afaik)
BROWSER := open

# BUILD SOURCE AND TARGTS
# ---------------------------------------
# do not change

#20260101:subdirs
#MDSOURCES  := $(wildcard $(srcdir)/*.$(MD))
#go into subdirs
#MDSOURCES := $(wildcard $(srcdir)/**/*.md)
#but we're not ready for that yet, so temp comment above for this
#MDSOURCES := $(filter-out %/index.md,$(wildcard $(srcdir)/**/*.md))

#MDSOURCES := $(wildcard $(srcdir)/**/*.md)
#MDSOURCES := $(filter-out %/index.md,$(MDSOURCES))


#MDSOURCES := $(wildcard $(srcdir)/*.$(MD))
#MDSOURCES += $(wildcard $(srcdir)/**/*.md)
#MDSOURCES := $(sort $(MDSOURCES))
#MDSOURCES := $(filter-out %/index.md,$(MDSOURCES))

#MDSOURCES := $(wildcard $(srcdir)/*.$(MD))
#MDSOURCES += $(wildcard $(srcdir)/**/*.md)
#MDSOURCES := $(filter-out %/index.md,$(MDSOURCES))
#MDSOURCES := $(sort $(MDSOURCES))

# ---------------------------------------
# SOURCE DISCOVERY
# ---------------------------------------

# Top-level pages (flat)
TOP_MD   := $(wildcard $(srcdir)/*.$(MD))
TOP_HTML := $(patsubst $(srcdir)/%.$(MD),%.$(HTML),$(TOP_MD))

# Section landing pages
SECTION_INDEX_MD := $(wildcard $(srcdir)/*/index.$(MD))
SECTION_INDEX_HTML := $(patsubst $(srcdir)/%/index.$(MD),%.$(HTML),$(SECTION_INDEX_MD))

# Per-page section content
SECTION_PAGE_MD := $(wildcard $(srcdir)/*/*/index.$(MD))
SECTION_PAGE_HTML := $(patsubst $(srcdir)/%/index.$(MD),%/index.$(HTML),$(SECTION_PAGE_MD))

# Generate targets for each type
#HTMTARGETS := $(patsubst $(srcdir)/%.$(MD),%.$(HTML),$(MDSOURCES))
#HTMTARGETS := $(notdir $(MDSOURCES))
#HTMTARGETS := $(HTMTARGETS:.$(MD)=.$(HTML))
#HTMTARGETS := $(patsubst $(srcdir)/%.$(MD),%.$(HTML),$(MDSOURCES))


# All generated HTML pages
HTMTARGETS := $(sort \
	$(TOP_HTML) \
	$(SECTION_INDEX_HTML) \
	$(SECTION_PAGE_HTML) \
)


#20260101:subdirs



PNGSOURCES := $(wildcard $(srcdir)/*.$(PNG))
JPGSOURCES := $(wildcard $(srcdir)/*.$(JPG))
JPGSOURCES += $(wildcard $(srcdir)/*.$(JPEG))
DOTSOURCES := $(wildcard $(srcdir)/*.$(DOT))

PNGTARGETS := $(PNGSOURCES:.$(PNG)=.$(WEBP))
JPGTARGETS := $(JPGSOURCES:.$(JPG)=.$(WEBP))
JPGTARGETS += $(JPGSOURCES:.$(JPEG)=.$(WEBP))
DOTTARGETS += $(DOTSOURCES:.$(DOT)=.$(WEBP))

# TARGETS
# ---------------------------------------
# do not change

.PHONY: all
all: images tidy robots siteimg favicon ## Build everything
	+$(MAKE) pages
	+$(MAKE) index archive lists rpg howto other drafts
	+$(MAKE) sitemap

.PHONY: typos pages
pages: $(HTMTARGETS) ## Build markdown pages

.PHONY: images
images: $(PNGTARGETS) $(JPGTARGETS) $(DOTTARGETS) ## Build images

.PHONY: push
push: tidy ## Push site to prod via rsync
	$(RSYNC) $(RSFLAGS) . $(site_url):$(site_url)

.PHONY: test
test: ## Start local test server
	$(PY) $(PYFLAGS)

.PHONY: open
open: ## Open main page in browser
	$(BROWSER) http://$(SERVER):$(PORT)/

.PHONY: typos
typos: ## Check for common typos
	! grep "wepb" $(MDSOURCES)

.PHONY: back
back: tidy | $(bakdir) ## TAR and backup (eg ../backup/site.YYYY-MM-DD.tar.gz)
	tar -cvf $(bakdir)/$(site_url).$(date_today).tar.gz .

$(bakdir):
	mkdir -p $@

.PHONY: clean
clean: tidy ## Remove all generated files
	$(RM) $(site_css)
	$(RM) $(site_htm)
	$(RM) $(imgdir)/*
	$(RM) $(srcdir)/*.$(WEBP)
	$(RM) favicon.ico
	$(RM) robots.txt
	$(RM) sitemap.txt
	$(RM) sitemap.xml
	$(RM) feed.json
	$(RM) *.$(HTML)

.PHONY: tidy
tidy: nomac ## Tidy extra files
	$(RM) .*.swo
	$(RM) .*.swp
	$(RM) $(srcdir)/.*.swo
	$(RM) $(srcdir)/.*.swp

.PHONY: nomac
nomac: ## Tidy mac files
	$(RM) .DS_Store
	$(RM) $(srcdir)/.DS_Store
	$(RM) $(imgdir)/.DS_Store
	$(RM) $(bakdir)/.DS_Store

########################################
###
### index
###
########################################

.PHONY: index
index: index.$(HTML) ## Build index files

INDEX_TEXT = '---\
			 \ntitle: $(site_name)\
			 \nsubtitle: $(site_desc)\
			 \n---\
			 \n\
			 \n$(shell echo $(RECENTPOSTS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/_/, " ", $$2) ; print "\\n* <span class='date'>" $$1 "</span> -- [" $$2 "](" file ")"}')\
			 \n<br /><small>*[everything...](archive.$(HTML))*</small>\
			 '

#			 \n\
#			 \n$(TABLE_TITLE)\
#			 \n-------------\
#			 \n$(shell echo $(TABLES) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(TABLE_PRE)/, "", $$1) ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#			 \n\
#			 \n$(LIST_TITLE)\
#			 \n-------------\
#			 \n$(shell echo $(LISTS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(LIST_PRE)/, "", $$1) ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#			 \n\
#			 \n$(RPG_TITLE)\
#			 \n------------\
#			 \n$(shell echo $(RPGS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(RPG_PRE)/, "", $$1) ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#			 \n\
#			 \n$(REVIEW_TITLE)\
#			 \n---------------\
#			 \n$(shell echo $(REVIEWS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(REVIEW_PRE)/, "", $$1) ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#			 \n\

#index.$(HTML): $(RECENTPOSTS) $(LISTS) $(RPGS) $(OTHERS) $(MAKEFILE_LIST)
index.$(HTML): $(RECENTPOSTS) $(MAKEFILE_LIST)
	echo $(INDEX_TEXT) | $(PD) $(PDFLAGS) $(OUTPUT_OPTION)

########################################
###
### archive
###
########################################

.PHONY: archive
archive: archive.$(HTML) ## Build archive page

#ACHIVE_TEXT = '---\
#			  \ntitle: $(ARCHIVE_TITLE)\
#			  \nsubtitle: $(ARCHIVE_SUBTITLE)\
#			  \n---\
#			  \n\
#			  \n$(shell echo $(ARCHIVE) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/_/, " ", $$2) ; print "\\n* <span class='date'>" $$1 "</span> -- [" $$2 "](" file ")"}')\
#			  '

ARCHIVE_ITEMS := $(shell \
	echo $(ARCHIVE) | tr " " "\n" | \
    awk 'BEGIN{FS="."} {file=$$0 ; gsub(/_/," ",$$2); print "\\n* <span class='date'>" $$1 "</span> -- [" $$2 "](" file ")"}')

#archive.$(HTML): $(ARCHIVE) $(MAKEFILE_LIST)
#		echo $(ACHIVE_TEXT) | $(PD) $(PDFLAGS) $(OUTPUT_OPTION)

archive.$(HTML): _src/archive/index.md $(ARCHIVE) $(MAKEFILE_LIST)
	( cat _src/archive/index.md ; echo ; echo "$(ARCHIVE_ITEMS)" ) | \
	$(PD) $(PDFLAGS) $(OUTPUT_OPTION)

########################################
###
### lists
###
########################################
	
.PHONY: lists
lists: lists.$(HTML) ## Build list page

#LISTS_TEXT = '---\
#			 \ntitle: $(LIST_TITLE)\
#			 \nsubtitle: $(LIST_SUBTITLE)\
#			 \n---\
#			 \n\
#			 \n$(shell echo $(LISTS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(LIST_PRE)/, "", $$1) ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#			 '

#LIST_ITEMS := $(shell \
#	echo $(LISTS) | tr " " "\n" | \
#	awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(LIST_PRE)/,"",$$1); gsub(/_/," ",$$1); print "\\n* [" $$1 "](" file ")"}' \
#)

LIST_ITEMS := $(shell \
	echo $(LISTS) | tr " " "\n" | \
	awk 'BEGIN{FS="/"} {file=$$0; name=$$NF; sub(/\..*/,"",name); sub(/^$(LIST_PRE)/,"",name); gsub(/_/," ",name); print "\\n* [" name "](" file ")"}')

#lists.$(HTML): $(LISTS) $(MAKEFILE_LIST)
#	echo $(LISTS_TEXT) | $(PD) $(PDFLAGS) $(OUTPUT_OPTION)

lists.$(HTML): _src/lists/index.md $(LISTS) $(MAKEFILE_LIST)
	( cat _src/lists/index.md ; echo ; echo "$(LIST_ITEMS)" ) | \
	$(PD) $(PDFLAGS) $(OUTPUT_OPTION)

########################################
###
### rpg
###
########################################

.PHONY: rpg
rpg: rpg.$(HTML) ## Build rpg page

#RPG_TEXT = '---\
#		\ntitle: $(RPG_TITLE)\
#		\nsubtitle: $(RPG_SUBTITLE)\
#		\n---\
#		\n\
#		\n$(shell echo $(RPGS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(RPG_PRE)/, "", $$1) ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#		' 

RPG_ITEMS := $(shell \
	echo $(RPGS) | tr " " "\n" | \
	awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(RPG_PRE)/,"",$$1); gsub(/_/," ",$$1); print "\\n* [" $$1 "](" file ")"}')

#rpg.$(HTML): $(RPGS) $(MAKEFILE_LIST)
#	echo $(RPG_TEXT) | $(PD) $(PDFLAGS) $(OUTPUT_OPTION)

rpg.$(HTML): _src/rpg/index.md $(RPGS) $(MAKEFILE_LIST)
	( cat _src/rpg/index.md ; echo ; echo "$(RPG_ITEMS)" ) | \
	$(PD) $(PDFLAGS) $(OUTPUT_OPTION)

########################################
###
### howto
###
########################################

.PHONY: howto
howto: howto.$(HTML) ## Build howto page

#HOWTO_TEXT = '---\
#			  \ntitle: $(HOWTO_TITLE)\
#			  \nsubtitle: $(HOWTO_SUBTITLE)\
#			  \n---\
#			  \n\
#			  \n$(shell echo $(HOWTO) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(HOWTO_PRE)/, "", $$1) ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#			  ' 

HOWTO_ITEMS := $(shell \
	echo $(HOWTO) | tr " " "\n" | \
	awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(HOWTO_PRE)/,"",$$1); gsub(/_/," ",$$1); print "\\n* [" $$1 "](" file ")"}')

#howto.$(HTML): $(HOWTO) $(MAKEFILE_LIST)
#	echo $(HOWTO_TEXT) | $(PD) $(PDFLAGS) $(OUTPUT_OPTION)

howto.$(HTML): _src/howto/index.md $(HOWTO) $(MAKEFILE_LIST)
	( cat _src/howto/index.md ; echo ; echo "$(HOWTO_ITEMS)" ) | \
	$(PD) $(PDFLAGS) $(OUTPUT_OPTION)

########################################
###
### drafts
###
########################################

.PHONY: drafts
drafts: drafts.$(HTML) ## Build drafts page

#DRAFTS_TEXT = '---\
#			  \ntitle: $(DRAFT_TITLE)\
#			  \nsubtitle: $(DRAFT_SUBTITLE)\
#			  \n---\
#			  \n\
#			  \n$(shell echo $(DRAFTS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(DRAFT_PRE)/, "", $$1) ; gsub(/_/, " ", $$2) ; print "\\n* <span class='date'>" $$1 "</span> -- [" $$2 "](" file ")"}')\
#		  ' 

DRAFTS_ITEMS := $(shell \
	echo $(DRAFTS) | tr " " "\n" | \
    awk 'BEGIN{FS="."} {file=$$0 ; gsub(/$(DRAFT_PRE)/,"",$$1); gsub(/_/," ",$$2); print "\\n* <span class='date'>" $$1 "</span> -- [" $$2 "](" file ")"}')

#drafts.$(HTML): $(DRAFTS) $(MAKEFILE_LIST)
#	echo $(DRAFTS_TEXT) | $(PD) $(PDFLAGS) $(OUTPUT_OPTION)

drafts.$(HTML): _src/drafts/index.md $(DRAFTS) $(MAKEFILE_LIST)
	( cat _src/drafts/index.md ; echo ; echo "$(DRAFTS_ITEMS)" ) | \
	$(PD) $(PDFLAGS) $(OUTPUT_OPTION)

########################################
###
### other
###
########################################
	
.PHONY: other
other: other.$(HTML) ## Build other page

#OTHER_TEXT = '---\
#			 \ntitle: $(OTHER_TITLE)\
#			 \nsubtitle: $(OTHER_SUBTITLE)\
#			 \n---\
#			 \n\
#			 \n$(shell echo $(OTHERS) | tr " " "\n" | awk 'BEGIN{FS="."} {file=$$0 ; gsub(/_/, " ", $$1) ; print "\\n* [" $$1 "](" file ") "}')\
#			 '

OTHER_ITEMS := $(shell \
	echo $(OTHERS) | tr " " "\n" | \
	awk 'BEGIN{FS="."} {file=$$0 ; gsub(/_/," ",$$1); print "\\n* [" $$1 "](" file ")"}')

#other.$(HTML): $(OTHERS) $(MAKEFILE_LIST)
#	echo $(OTHER_TEXT) | $(PD) $(PDFLAGS) $(OUTPUT_OPTION)

other.$(HTML): _src/other/index.md $(OTHERS) $(MAKEFILE_LIST)
	( cat _src/other/index.md ; echo ; echo "$(OTHER_ITEMS)" ) | \
	$(PD) $(PDFLAGS) $(OUTPUT_OPTION)

########################################
###
### meta files
###
########################################

$(site_css): $(srcdir)/_$(site_url).$(CSS) $(MAKEFILE_LIST)
	$(YUI) $(YUFLAGS) $< $(OUTPUT_OPTION)

$(site_htm): $(srcdir)/_$(site_url).htm $(MAKEFILE_LIST)
	cp -f $< $@

.PHONY: favicon
favicon: favicon.ico ## Build favicon file

favicon.ico: $(srcdir)/$(site_ico) $(MAKEFILE_LIST)
	$(CON) $< $(FAVFLAGS) $@

.PHONY: siteimg
siteimg: $(imgdir)/$(site_img) ## Build default site image file

$(imgdir)/$(site_img): $(srcdir)/$(site_img)
	cp -f $< $@

.PHONY: sitemap
sitemap: sitemap.txt sitemap.xml ## Build sitemap files

sitemap.txt: $(ARCHIVE) $(MAKEFILE_LIST)
	ls *.$(HTML) | grep -vE 'test.$(HTML)|drafts.$(HTML)|$(DRAFT_PRE)|$(HTTP_STATUS_PRE)' | sort | sed 's,^,https://$(site_url)/,' > $@

sitemap.xml: $(ARCHIVE) $(MAKEFILE_LIST)
	echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' > $@
	ls *.$(HTML) | grep -vE 'test.$(HTML)|drafts.$(HTML)|$(DRAFT_PRE)|$(HTTP_STATUS_PRE)' | sort | sed -e 's,^,<url><loc>https://$(site_url)/,' -e 's,$$,</loc></url>,' >> $@
	echo '</urlset>' >> $@

.PHONY: robots
robots: robots.txt ## Build robots file

#		\ndisallow: /test.$(HTML)\

ROBOTS_TEXT = 'User-Agent: *\
		\nallow: /\
		\nallow: /_mirror/\
		\ndisallow: /drafts.$(HTML)\
		\ndisallow: /$(DRAFT_PRE)*.$(HTML)\
		\ndisallow: /$(HTTP_STATUS_PRE)*.$(HTML)\
		\nDisallow: /?p=*\
		\n\
		\nSitemap: https://$(site_url)/sitemap.xml\
		\n\
		\nCrawl-delay: 30\
		'

robots.txt: $(MAKEFILE_LIST)
	echo $(ROBOTS_TEXT) > $@

.PHONY: rss
rss: feed.json ## Build rss feeds

feed.json: $(MAKEFILE_LIST)
	echo '{\
		\n  "version": "https://jsonfeed.org/version/1.1",\
		\n  "title": "$(site_name)",\
		\n  "home_page_url": "https://$(site_url)/",\
		\n  "feed_url": "https://$(site_url)/feed.json",\
		\n  "description": "$(site_desc)",\
		\n  "icon": "https://$(site_url)/$(imgdir)/$(site_img)",\
		\n  "favicon": "https://$(site_url)/favicon.ico",\
		\n    {\
		\n      "id": "2",\
		\n todo dave you are up to here \
		\n    ]\
		\n  ]\
		\n}\
		' > $@

$(srcdir)/%.$(WEBP): $(srcdir)/%.$(JPG) #$(MAKEFILE_LIST)
	$(CON) $< $(JPGFLAGS) $(WEBPFLAGS) $@

$(srcdir)/%.$(WEBP): $(srcdir)/%.$(JPEG) #$(MAKEFILE_LIST)
	$(CON) $< $(JPGFLAGS) $(WEBPFLAGS) $@

$(srcdir)/%.$(WEBP): $(srcdir)/%.$(PNG) #$(MAKEFILE_LIST)
	$(CON) $< $(PNGFLAGS) $(WEBPFLAGS) $@

$(srcdir)/%.$(WEBP): $(srcdir)/%.$(DOT) #$(MAKEFILE_LIST)
	$(CON) $< $(DOTFLAGS) $(WEBPFLAGS) $@

########################################
###
### new post
###
########################################

.PHONY: new
new: $(srcdir)/$(DRAFT_PRE)$(date_today).$(slug).$(MD) ## Create a draft post and open for editing (eg make new title="foo bar")
	$(EDIT) $(EDITFLAGS) $<

NEW_POST = '---\
		   \ntitle: $(title)\
		   \nsubtitle: $(subtitle)\
		   \nauthor: $(page_author)\
		   \ndate: $(date_today)\
		   \nkeywords: [keyword1, keyword2]\
		   \nimg: image.wepb\
		   \nalt: alt image description\
		   \nabstract: an abstract all on one line, limit to 200 chars\
		   \n---\
		   \n\
		   \ndont forget to remove "$(DRAFT_PRE)" and rename as appropriate to publish:\
		   \n\
		   \n    $(srcdir)/$(DRAFT_PRE)$(date_today).$(slug).$(MD)\
		   \n\
		   \n\
		   '

$(srcdir)/$(DRAFT_PRE)$(date_today).$(slug).$(MD):
	echo $(NEW_POST) > $@

#helper rule to make dirs if needed
%/:
	mkdir -p $@

#dave
#%.$(HTML): $(srcdir)/%.$(MD) $(site_css) $(site_htm) $(MAKEFILE_LIST)
#%.$(HTML): $(srcdir)/**/%.$(MD) $(site_css) $(site_htm) $(MAKEFILE_LIST)
#	$(eval $@_TOC := $(shell if grep -Eq "^toc:\s*\w+$$" $< ; then echo '--toc' ; fi))
#	$(PD) $(PDFLAGS) $($@_TOC) $< $(OUTPUT_OPTION) #there is a quote here to fix vim colouring'
#dave


# ---------------------------------------
# BUILD RULES
# ---------------------------------------

# Top-level pages
%.$(HTML): $(srcdir)/%.$(MD) $(site_css) $(site_htm) $(MAKEFILE_LIST)
	$(eval $@_TOC := $(shell if grep -Eq "^toc:\s*\w+$$" $< ; then echo '--toc' ; fi))
	$(PD) $(PDFLAGS) $($@_TOC) $< $(OUTPUT_OPTION) #there is a quote here to fix vim colouring'

# Section index pages
%.$(HTML): $(srcdir)/%/index.$(MD) $(site_css) $(site_htm) $(MAKEFILE_LIST)
	$(eval $@_TOC := $(shell if grep -Eq "^toc:\s*\w+$$" $< ; then echo '--toc' ; fi))
	$(PD) $(PDFLAGS) $($@_TOC) $< $(OUTPUT_OPTION) #there is a quote here to fix vim colouring'

# Per-page section content
%/index.$(HTML): $(srcdir)/%/index.$(MD) $(site_css) $(site_htm) $(MAKEFILE_LIST)
	@mkdir -p $(@D)
	$(eval $@_TOC := $(shell if grep -Eq "^toc:\s*\w+$$" $< ; then echo '--toc' ; fi))
	$(PD) $(PDFLAGS) $($@_TOC) $< $(OUTPUT_OPTION) #there is a quote here to fix vim colouring'


.PHONY: about
about: ## About this Makefile
	@echo
	@echo 'This Makefile is used to generate static blog files for $(site_url)'
	@echo
	@echo 'Edit this Makefile to change paramaters eg, site_url, site_name, etc'
	@echo
	@echo 'Put your Markdown files in $(srcdir):'
	@echo '  - Do not use spaces in filenames, use underscores'
	@echo '  - Post filename format with YYYY-MM-DD.x.$(MD)'
	@echo '  - List filename format $(LIST_PRE)x.$(MD)'
	@echo '  - RPG filename format $(RPG_PRE)x.$(MD)'
	@echo '  - How to filename format $(HOWTO_PRE)x.$(MD)'
#	@echo '  - Table filename format $(TABLE_PRE)x.$(MD)'
#	@echo '  - Review filename format $(REVIEW_PRE)x.$(MD)'
#	@echo '  - Links page filename is links.md'
	@echo '  - Other page filename format is anything else'
	@echo '  - About page filename is about.md'
	@echo
	@echo 'Run "make help" to for how to run'
	@echo

.PHONY: copy
copy: ## Copyright notice
	@echo
	@echo 'Copyright (C) 2021--2026 David Marsh'
	@echo 'rdmarsh@gmail.com https://rdm.sh/powered_by.html'
	@echo
	@echo 'This program is free software: you can redistribute it and/or modify'
	@echo 'it under the terms of the GNU General Public License as published by'
	@echo 'the Free Software Foundation, either version 3 of the License, or'
	@echo '(at your option) any later version.'
	@echo	
	@echo 'This program comes with ABSOLUTELY NO WARRANTY.'
	@echo 'This is free software, and you are welcome to redistribute it'
	@echo 'under certain conditions. View the file "LICENSE" for details.'
	@echo

.PHONY: help
help: ## Show this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage: make [flags] [option]\n"} /^[$$()% \.0-9a-zA-Z_-]+:.*?##/ { printf "  \033[36mmake %-12s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
	@echo
	@echo 'Useful make flags:'
	@echo '  make -n   : dry run'
	@echo '  make -j   : run simultaneous jobs'
	@echo '  make -B   : force make target'
	@echo
	@echo 'You can override Makefile vars like so:'
	@echo '  make site_css=pandoc.css'
	@echo

#testing stuff, delete later
print-nav:
	@printf "%s\n" $(NAV_DIRS_FIRST_ORDERED)
	@echo
	@printf "%s\n" $(NAV_DIRS)

print-md:
	@printf "%s\n" $(MDSOURCES)

print-vars:
	@echo "HTML=$(HTML)"
	@echo "MD=$(MD)"
	@echo "srcdir=$(srcdir)"

print-html:
	@printf "%s\n" $(HTMTARGETS)
