#***********************************************************************************************************************
# DMTF - Distributed Management Task Force, Inc. - http://www.dmtf.org
# See the readme file in the DSP2023 ZIP archive for copyright information.
# This file is part of the DSP2023 ZIP archive and has been last changed in DSP2023 Version 1.1.0.
#
# makefile - main makefile for make-based MRP build environment.
#
# This makefile covers the build of HTML files, PDF 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 the DSP2023 readme file for a description on how to set up the make-based MRP build environment.
#
# Supported platforms:
#   Tested on Windows, should also work on Linux.
#
# Prerequisite commands and packages:
#   make (GNU make 3.79, from CygWin). Note that 3.81 uses the shell for the picture zip file.
#   rm (file removal command, from CygWin)
#   zip (GNU zip utility, from CygWin)
#   Python 2.6 or 2.7 (www.python.org, for running mrp2html.py)
#   Prince-XML 8.0 (PDF generator from HTML)
#
# Change history: See readme file of DSP2023.
#
# Last Updated: 2012-07-10
#***********************************************************************************************************************

#--- Customize these variables as needed

# List of directories containing .mrp.xml files that are to be processed for the build target.
# 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 to include the directories you want to be built.
build_directories := $(wildcard DSP*)

#--- Do not customize below this line

# Directories with profile files that are to be processed for the buildxmp target.
xmp_directories := $(wildcard XMP*)

# Provides customizable definitions of some file locations.
include resources/resources.properties
tocgen_file2 = resources/tocgen.js
dsp8054_file2 = resources/$(dsp8054_file)
dsp8029_file2 = resources/$(dsp8029_file)
cimxml_dir2 = resources/$(cimxml_dir)

# 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 $(xmp_directories) for .mrp.xml files, one
# directory level deep. It is valid to have more than one .mrp.xml file in a directory.
xmp_basenames := $(foreach dir,$(xmp_directories),$(basename $(basename $(wildcard $(dir)/*.mrp.xml))))
build_basenames := $(foreach dir,$(build_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 $(xmp_directories) for .vsd files, one
# directory level deep. It is valid to have more than one .vsd file in a directory.
xmp_visio_basenames := $(foreach dir,$(xmp_directories),$(basename $(wildcard $(dir)/*.vsd)))
build_visio_basenames := $(foreach dir,$(build_directories),$(basename $(wildcard $(dir)/*.vsd)))

# HTML profile files.
# The list is created automatically from the set of MRP XML files.
xmp_html_files := $(foreach bn,$(xmp_basenames),$(bn).html)
build_html_files := $(foreach bn,$(build_basenames),$(bn).html)

# PDF profile files.
# The list is created automatically from the set of MRP XML files.
xmp_pdf_files := $(foreach bn,$(xmp_basenames),$(bn).pdf)
build_pdf_files := $(foreach bn,$(build_basenames),$(bn).pdf)

# Picture files of the profiles.
# The list is created automatically from the set of Visio files.
xmp_pic_files := $(foreach bn,$(xmp_visio_basenames),$(wildcard $(bn)*.gif))
build_pic_files := $(foreach bn,$(build_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.
xmp_pic_zip_files := $(foreach bn,$(xmp_visio_basenames),$(bn).pic.zip)
build_pic_zip_files := $(foreach bn,$(build_visio_basenames),$(bn).pic.zip)

mrp2html_py_file := ../bin/mrp2html.py

.PHONY: clean build buildxmp

xmp_build_files := $(xmp_html_files) $(xmp_pdf_files) $(xmp_pic_zip_files)
build_files := $(build_html_files) $(build_pdf_files) $(build_pic_zip_files)

help:
	@echo 'MRP profiles makefile'
	@echo 'Valid targets are:'
	@echo '  build    - Build the HTML, PDF and picture zip files for MRP profiles in this subtree:'
	@echo '             $(build_directories)'
	@echo '  buildxmp - Build the HTML, PDF and picture zip files for the sample MRP profiles in this subtree:'
	@echo '             $(xmp_directories)'
	@echo '  {DSPnnnn}/{DSPnnnn}_{mnud}.pdf   - Build the PDF file for profile {DSPnnnn}.'
	@echo '  clean    - Remove any built files.'
	@echo help done.

build: $(build_files)
	@echo build done.

buildxmp: $(xmp_build_files)
	@echo buildxmp done.

clean:
	-rm -f $(xmp_build_files) $(build_files)
	@echo clean done.

_always:

%.html: %.mrp.xml $(dsp8029_file2) $(dsp8054_file2) $(mrp2html_py_file) $(tocgen_file2)
	python $(mrp2html_py_file) --xsltfile $(dsp8029_file2) --cssurl $(dsp8054_file2) --tocgenurl $(tocgen_file2) --cimxmlurl $(cimxml_dir2) $< $*.html

%.pdf: %.html
	"$(prince_file)" --javascript --media=print -o $*.pdf $<

# Note: Newer GNU make versions support .SECONDEXPANSION, which would allow to put the picture .gif files onto the target list.
# The latest GNU make available for Windows does not support that, so for portability we let zip update the picture archive if needed,
# and issue a warning for those cases that are not covered that way.
%.pic.zip: %.vsd _always
	@echo 'Warning: If .gif files for the following zip file have been removed, remove the zip file manually to rebuild it.'
	-zip -u -j $@ $**.gif
