#***********************************************************************************************************************
# DMTF - Distributed Management Task Force, Inc. - http://www.dmtf.org
# See dsp2023_readme.txt for copyright information.
#
# makefile - part of the DMTF DSP2023 zip archive.
#
# MRP profiles makefile.
# This makefile covers the build of HTML files and picture zip files for MRP XML profiles.
# It is used for the sample profiles in DSP2023, and can also be used for your own MRP profiles.
# See dsp2023_readme.txt for a description on how to set up the build environment for MRP profiles.
#
# Supported platforms:
#   Tested on Windows.
#
# Prerequisite commands and other files:
#   make (GNU make, from CygWin)
#   cat (file view command, from CygWin, used by embed.awk)
#   rm (file removal command, from CygWin)
#   zip (GNU zip utility, from CygWin)
#   gawk (GNU awk utility, from CygWin)
#   base64 (Base64 encoder/decoder utility, from CygWin, used by embed-file.awk)
#   xalan-j 2.7.1 (Xalan-J XSLT processor, from Apache Xalan-J project)
#   URI resolver 1.2 (XML Catalog support for Xalan-J, from Apache XML-Commons)
#   Prince-XML 8.0 (PDF generator from HTML)
#   xalan.bat (Wrapper script for Xalan-J, from DSP2023)
#   prince-xml.bat (Wrapper script for Prince-XML, from DSP2023)
#   resources/tocgen.js (Javascript for dynamically generating TOC in HTML file, from DSP2023)
#   resources/embed-file.awk (AWK script for processing EMBED-FILE directives, from DSP2023)
#   resources/embed-css-file.awk (AWK script for embedding a CSS file, from DSP2023)
#***********************************************************************************************************************

# Directories with profile files that are to be processed by this makefile.
# The makefile will process all .mrp.xml profile files it finds there, and the picture files of all .vsd (Visio) files
# it finds there.
# Customize this variable as needed.
mrp_directories := \
    $(wildcard xmp*) \

other_directories := \
    $(wildcard dsp*) \

# dsp8029 XSLT stylesheet file to be used for the HTML file generation.
dsp8029_file := resources/dsp8029_1.0.0.xsl

# dsp8054 CSS stylesheet file to check dependencies against.
# The dsp8054 file that is used by the build process is specified in the global parameter "css-url" in dsp8029.
dsp8054_file := resources/dsp8054_1.0.0.css

# Other files on which the HTML generation depends
other_html_build_files := resources/tocgen.js resources/embed-css-file.awk resources/embed-file.awk

# Boolean controlling whether the HTML file generated by the dsp8029 XSLT stylesheet is standalone (i.e. has the CSS
# stylesheet and any pictures embedded in the HTML instead of referencing them).
# Valid values are true and false.
standalone := true

#--- Do not customize below this line

# Base names (that is, relative path and base filename without file extension) of the MRP XML files.
# The list is created automatically by searching the directories listed in $(mrp_directories) for .mrp.xml files, one
# directory level deep. It is valid to have more than one .mrp.xml file in a directory.
mrp_basenames := $(foreach dir,$(mrp_directories),$(basename $(basename $(wildcard $(dir)/*.mrp.xml))))
other_basenames := $(foreach dir,$(other_directories),$(basename $(basename $(wildcard $(dir)/*.mrp.xml))))

# Base names (that is, relative path and base filename without file extension) of the Visio files.
# The list is created automatically by searching the directories listed in $(mrp_directories) for .vsd files, one
# directory level deep. It is valid to have more than one .vsd file in a directory.
mrp_visio_basenames := $(foreach dir,$(mrp_directories),$(basename $(wildcard $(dir)/*.vsd)))
other_visio_basenames := $(foreach dir,$(other_directories),$(basename $(wildcard $(dir)/*.vsd)))

# HTML profile files.
# The list is created automatically from the set of MRP XML files.
mrp_html_files := $(foreach bn,$(mrp_basenames),$(bn).html)
other_html_files := $(foreach bn,$(other_basenames),$(bn).html)

# PDF profile files.
# The list is created automatically from the set of MRP XML files.
mrp_pdf_files := $(foreach bn,$(mrp_basenames),$(bn).pdf)
other_pdf_files := $(foreach bn,$(other_basenames),$(bn).pdf)

# Picture files of the profiles.
# The list is created automatically from the set of Visio files.
mrp_pic_files := $(foreach bn,$(mrp_visio_basenames),$(wildcard $(bn)*.gif))
other_pic_files := $(foreach bn,$(other_visio_basenames),$(wildcard $(bn)*.gif))

# Picture ZIP files, with all picture files of the profiles.
# The list is created automatically from the set of Visio files.
mrp_pic_zip_files := $(foreach bn,$(mrp_visio_basenames),$(bn).pic.zip)
other_pic_zip_files := $(foreach bn,$(other_visio_basenames),$(bn).pic.zip)


.PHONY: clean build

mrp_build_files := $(mrp_html_files) $(mrp_pdf_files) $(mrp_pic_zip_files)
other_build_files := $(other_html_files) $(other_pdf_files) $(other_pic_zip_files)

help:
	@echo 'MRP profiles makefile'
	@echo 'Valid targets are:'
	@echo '  build   - Build the profile HTML, PDF and picture zip files for non-sample MRP profiles in this subtree.'
	@echo '  buildmrp - Build the profile HTML, PDF and picture zip files for the sample MRP profiles in this subtree.'
	@echo '  clean   - Remove any built files.'
	@echo help done.

build: $(other_build_files)
	@echo build done.

buildmrp: $(mrp_build_files)
	@echo buildmrp done.

clean:
	-rm -f $(mrp_build_files) $(other_build_files)
	@echo clean done.

%.html: %.mrp.xml $(dsp8029_file) $(dsp8054_file) $(other_html_build_files)
	xalan.bat -in $< -xsl $(dsp8029_file) -out $*.html.1.tmp -param standalone $(standalone) -param css-url ../$(dsp8054_file)
	cmd /c "cd $(dir $<) & gawk -v embed_css_file_awk=../resources/embed-css-file.awk -f ../resources/embed-file.awk <$(notdir $*).html.1.tmp 2>&1 >$(notdir $@)"
	rm $*.html.*.tmp

%.pdf: %.html
	-prince-xml --javascript --media=print $< -o $*.pdf

%.pic.zip: %.vsd
	@echo 'Note: The picture files (.gif) are not on the dependencies list, so use make clean if picture files have been created or modified.'
	-zip -q $@ $*_*.gif

#%.svg: %.vdx
#       xalan.bat -in $< -xsl resources/vdxtosvg/visio.xsl -out $*.svg -param pageNumber 1 -param userScale 100
