#!/usr/bin/env texlua

--[[
	Build script for the LaTeX-ZUGFeRD project
	Copyright (C) 2024 Marei Peischl <marei@peitex.de>

	This file is part of the build system of the zugferd package.

	It may be distributed and/or modified under the conditions of the
	LaTeX Project Public License (LPPL), either version 1.3c of this
	license or (at your option) any later version. The latest version
	of this license is in the file

	https://www.latex-project.org/lppl.txt
]]
 
module="zugferd"

-- allow to tag the build.lua file
if options["target"] == "tag" then
	excludefiles={"*~"}
end

packageversion = "0.10"
packagedate      = "2025-09-22"

sourcefiles={"*.dtx","*.ins", "*.sty"}
demofiles={"*.tex"}

-- demofiles should be typeset for testing but not for the ctan upload
if options["target"] == "ctan" then
	typesetcmds="\\def\\DoNotTypesetImplementation{don't typeset implementation for CTAN}"
else
	typesetdemofiles={"*.tex"}
end

typesetexe="lualatex"
typesetopts=""

supportdir="support"
checksuppfiles={"validate_zugferd.sh","Mustang-CLI*.jar","zugferd-test.ltx"}
checkconfigs = {"build"}
-- checkconfigs = {"build", "config-pdf"} -- pdf config is only checked on release
recordstatus=true

ctanreadme="README_CTAN.md"

function get_dev_tag (oldtag)
	if string.match(oldtag, "%-dev$") then
		return (oldtag), true
	end
	newtag=string.gsub(oldtag,"v?(%d+).%d+%-?%w*%s?","%1.")..string.format("%02d",math.floor(string.gsub(oldtag,"v?%d+.(%d+)%-?%w*%s?","%1") + 1))
	return newtag.."-dev",false
end

--[[
  # Tagging configuration
]]

tagfiles = {"*.dtx","*.sty", "*.md", "*.tex","*.lua"}
function update_tag(file, content, tagname, tagdate)
	local versionpattern = "%d+.%d+%-?%w*"
	local datepattern = "%d%d%d%d%-%d%d%-%d%d"
	local tag_only_changes = false
	local old_tagpattern = ""
	local tagname = tagname or packageversion
	if tagname == "dev" or string.match(tagname, "%-dev$") then
		tagname, tag_only_changes = get_dev_tag(packageversion)
	else
    -- when a new tag is set all \changes{<old-dev-tag>} should be replaced as well
		old_tagpattern = string.gsub(packageversion, "%.", "%%.")
		old_tagpattern = string.gsub(old_tagpattern, "%-", "%%-")
	end
	-- Copyright (C) 2018–2025 by uploadconfig["author"]>
	-- maybe change to -- instead of –
	content = string.gsub(content, "(Copyright %(C%) 20%d%d)%-*%d%d%d%d by " ..
							uploadconfig["author"], "%1--" .. os.date("%Y") ..
							" by " .. uploadconfig["author"])
	-- tagging \changes
	if file ~= "build.lua" then
		content = string.gsub(content, "\\changes{v?0*%.0*}{" .. datepattern,
							"\\changes{v" .. tagname .. "}{" .. tagdate)
		content = string.gsub(content, "\\changes{version}{date",
							"\\changes{v" .. tagname .. "}{" .. tagdate)
	end
	-- don't tag fileversion to not change all dates
	if tag_only_changes then return content end
	if string.match(file, "%.md$") or string.match(file, "%.tex$") then
		content = string.gsub(content,
			"([Vv]ersion%s)"..versionpattern.."%s%("..datepattern.."%)",
			"%1"..tagname.." ("..tagdate..")")
	elseif file == "build.lua" then
		content = string.gsub(content,"(packageversion%s*=%s*\")"..versionpattern, "%1"..tagname)
		content = string.gsub(content,"(packagedate%s*=%s*\")"..datepattern, "%1"..tagdate)
	else
		content = string.gsub(content,
			"(\\Provides%a-{[^\n]-}%[)"..datepattern.."%s-v"..versionpattern,
			"%1"..tagdate.." v"..tagname)
		content = string.gsub(content,
			"(\\ProvidesExpl%a-{[^\n]-}){[^\n]-}{[^\n]-}",
			"%1{".. tagdate.."}{"..tagname .. "}")
		content = string.gsub(content,
			"(\\usepackage{"..module.."}%[)"..datepattern.."%]",
			"%1"..tagdate.."]")
		content = string.gsub(content,
							"\\changes{v" .. tagname .. "(%-dev)?}{" ..
								datepattern,
							"\\changes{v" .. tagname .. "}{" .. tagdate)
		content = string.gsub(content,"(%%<%*[^\n]+\n%s+%[)"..
								datepattern .. "%s-v" .. versionpattern,
							"%1" .. tagdate .. " " .. tagname)
	end
	content = string.gsub (content,
	                  "(Copyright %(C%) 202%d%–)%d%d%d%d",
	                  "%1"..os.date("%Y"))
	return content
end

github_base_url="https://github.com/TeXhackse/LaTeX-ZUGFeRD"
gitlab_base_url="https://gitlab.com/islandoftex/texmf/"..module

ctanpkg=module
uploadconfig = {
    author = "Marei Peischl",
    uploader = "Marei Peischl",
    description="This package provides interfaces to allow creating ZUGFeRD or Faktur-X invoices with LaTeX including the XML file. It can be used to modify personal invoicing templates to fulfil the requirements for digital invoicing without further modification of the invoicing processes.",
    pkg = ctanpkg,
    version = packageversion .. " " .. packagedate,
    license = "lppl1.3c",
    summary = "ZUGFeRD and Faktur-X invoicing using LaTeX",
    ctanPath = "/macros/latex/contrib/" .. ctanpkg,
    repository = {gitlab_base_url..".git",github_base_url..".git"},
    bugtracker = {gitlab_base_url.."/issues",github_base_url.."/issues"},
    support = {gitlab_base_url.."/issues",github_base_url.."/issues"},
    announcement_file = "ctan.ann",
    update = true,
    topic   = "invoice",
    note_file = "ctan.note"
}

function runtest_tasks ( name , run )
		if run == checkruns and options["target"] == "save" and name ~= "lostchars" then
			return "./validate_zugferd.sh " .. name
		else
			return ""
		end
end
