#
# Copyright (c) 2014 Rick Salevsky <rsalevsky@suse.de>
# Copyright (c) 2014, 2015, 2016 Karl Eichwalder <ke@suse.de>
# Copyright (c) 2015, 2016, 2017, 2018, 2019 Stefan Knorr <sknorr@suse.de>
#


# AsciiDoc conditional text (e.g. for "to be released" information during beta).
LIFECYCLE_VALID := beta maintained unmaintained
ifndef LIFECYCLE
  LIFECYCLE := maintained
endif
ifneq "$(LIFECYCLE)" "$(filter $(LIFECYCLE),$(LIFECYCLE_VALID))"
  override LIFECYCLE := maintained
endif

daps_command := daps

xsltproc_command := xsltproc \
  --stringparam generate.toc "/article toc" \
  --stringparam generate.section.toc.level 0 \
  --stringparam section.autolabel 1 \
  --stringparam section.label.includes.component.label 2 \
  --stringparam variablelist.as.blocks 1 \
  --stringparam toc.section.depth 2 \
  --stringparam toc.max.depth 3 \
  --stringparam show.comments 0 \
  --xinclude \
  --nonet

mainfile  := adoc/release-notes.adoc
dc_file   := DC-release-notes
src_files := $(wildcard adoc/*.adoc adoc/*.xml)

daps_xslt_rn_dir := /usr/share/daps/daps-xslt/relnotes

profile_params := --adocattr lifecycle=$(LIFECYCLE)
text_params    :=

# files to be created:
profile_result       := build/.profiled/noprofile/release-notes.xml
text_result          := build/release-notes/release-notes.txt
yast_html_result_dir := build/release-notes/yast-html
yast_html_result     := $(yast_html_result_dir)/release-notes.html


.PHONY: clean html pdf single-html text validate yast-html 

all: validate single-html yast-html pdf text html

#----------------

clean:
	rm -rf build/

#----------------

html: $(dc_file) $(src_files)
	$(daps_command) -d $< -vv $(profile_params) html --param="toc.section.depth=2"

#----------------

pdf: $(dc_file) $(src_files)
	$(daps_command) -d $< -vv $(profile_params) pdf

#----------------

single-html: $(dc_file) $(src_files)
	$(daps_command) -d $< -vv $(profile_params) html --single --param="toc.section.depth=2"

#----------------

# We need the text in ASCII to avoid issues when this is shown in text-only
# YaST.
text: $(text_result)
	iconv -f UTF-8 -t ASCII//TRANSLIT -o /dev/stdout $< > $<.tmp
	mv $<.tmp $<
$(text_result): $(dc_file) $(src_files)
	$(daps_command) -d $< -vv $(profile_params) text $(text_params)

#----------------

validate: $(dc_file) $(src_files)
	$(daps_command) -d $< -vv $(profile_params) --adocattr attribute-missing=warn validate

#----------------

yast-html: $(yast_html_result)

$(yast_html_result): $(profile_result)
	mkdir -p $(yast_html_result_dir)
	$(xsltproc_command) $(daps_xslt_rn_dir)/yast.xsl $< > $@ 2>/dev/null
	@echo "Created $@ for YaST HTML target."

$(profile_result): $(dc_file) $(src_files)
	$(daps_command) -vv -d $< $(profile_params) profile




