#!/bin/sh
# Converts the documentation of a SUSE product of the 16.0 family into a
# collection for mcp-docs: the articles that documentation.suse.com
# publishes for it (tools/suse-docs/PRODUCT), from SUSE/doc-modular at a
# pinned revision of its maintenance/SLE16.0 branch. PRODUCT is sles-16.0
# (SUSE Linux Enterprise Server, the default), sle-ha-16.0 (SUSE Linux
# Enterprise High Availability) or sles-sap-16.0 (SUSE Linux Enterprise
# Server for SAP applications).
#
#   [PRODUCT=...] tools/suse-docs/convert [OUT [ROOT]]
#
# OUT (default build/PRODUCT) receives the documents and PRODUCT.yaml,
# the collection file; ROOT (default OUT's absolute path) is where the
# collection file says the documents are, for example
# /usr/share/mcp-docs/docs/suse/PRODUCT when they are installed there.
#
# The sources are cloned into build/doc-modular, or, with SRC set, read
# from that tree as it is (an unpacked tarball, as in a package build,
# which has no network; tools/suse-docs/source-tarball makes it). REV
# names the revision of SRC (default: the pinned one), cited in the
# collection's index and its source links. In a checkout of mcp-docs the
# converter is built from it; installed (package mcp-docs-convert, below
# /usr/share/mcp-docs-convert/suse-docs), it is the installed one.
set -eu

rev=a9f708401da072b024e66994c8f15d91250b836e
branch=maintenance/SLE16.0
here=$(cd "$(dirname "$0")" && pwd)
name=${PRODUCT:-sles-16.0}
case $name in
sles-16.0)
	product='SUSE Linux Enterprise Server'
	site=sles/16.0
	about='installation, administration, security, virtualization, containers'
	;;
sle-ha-16.0)
	product='SUSE Linux Enterprise High Availability'
	site=sle-ha/16.0
	about='cluster installation, fencing and SBD, resources and constraints, maintenance, crmsh and Hawk'
	;;
sles-sap-16.0)
	product='SUSE Linux Enterprise Server for SAP applications'
	site=sles-sap/16.0
	about='installing and configuring a system for SAP applications, saptune, Ansible for SAP, and the administration of SLES 16.0 as it applies to them'
	;;
*)
	echo "convert: unknown PRODUCT $name (sles-16.0, sle-ha-16.0, sles-sap-16.0)" >&2
	exit 2
	;;
esac
version=16.0
title="$product $version"
out=${1:-build/$name}

if [ -n "${SRC:-}" ]; then
	src=$SRC
	rev=${REV:-$rev}
else
	src=build/doc-modular
	if [ ! -d "$src/.git" ]; then
		mkdir -p build
		git clone -q --filter=blob:none --no-checkout https://github.com/SUSE/doc-modular "$src"
	fi
	git -C "$src" cat-file -e "$rev^{commit}" 2>/dev/null || git -C "$src" fetch -q origin "$branch"
	git -C "$src" checkout -q --detach "$rev"
fi

convert=mcp-docs-convert
repo=$(cd "$here/../.." && pwd)
if [ -d "$repo/cmd/mcp-docs-convert" ]; then
	(cd "$repo" && go build -o bin/mcp-docs-convert ./cmd/mcp-docs-convert)
	convert=$repo/bin/mcp-docs-convert
fi

rm -rf "$out"
mkdir -p "$out"
"$convert" --strict --src "$src" --dcs "$here/$name" --out "$out" \
	--name "$name" --title "$title" --product "$product" --product-version "$version" \
	--description "The documentation of $title from documentation.suse.com, converted from its DocBook sources." \
	--site-url "https://documentation.suse.com/$site/html/%s/" \
	--source-url "https://github.com/SUSE/doc-modular/blob/$rev/%s" \
	--revision "$(echo "$rev" | cut -c1-7)"

root=${2:-$(cd "$out" && pwd)}
cat >"$out/$name.yaml" <<YAML
title: $title
root: $root
description: >
  SUSE's documentation of $title, the articles of documentation.suse.com:
  $about. One document per topic, under the ids of the site's sections.
index: README.md
instructions: >
  Each document's source front matter is its page on
  documentation.suse.com: cite it. articles/ lists each article's topics
  in order; a topic named ID--ARTICLE.md is that article's variant (an
  architecture's, for example).
YAML
echo "$out/$name.yaml"
