#!/bin/sh
# Converts the documentation of a SUSE product of the 16.0 or 16.1 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 the family's branch (maintenance/SLE16.0,
# maintenance/SLE16.1; from 16.1 the sources are below en/). PRODUCT is
# sles-16.0 (SUSE Linux Enterprise Server, the default), sle-ha-16.0 (SUSE
# Linux Enterprise High Availability), sles-sap-16.0 (SUSE Linux
# Enterprise Server for SAP applications), or the same for 16.1.
#
#   [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}
# The family: its branch, the pinned revision, and where the sources are
# (rev and branch above are 16.0's, which source-tarball reads).
version=${name##*-}
subdir=
case $version in
16.0) ;;
16.1)
	rev=1a9929880092d38b658b5b590578206bb6d4178b
	branch=maintenance/SLE16.1
	subdir=en/
	;;
*)
	echo "convert: unknown version in PRODUCT $name (16.0, 16.1)" >&2
	exit 2
	;;
esac
case ${name%-*} in
sles)
	product='SUSE Linux Enterprise Server'
	site=sles/$version
	# The product's file tells SLES from what is built on it: SUSE Linux
	# Micro 6.2 is ID=sles, VERSION_ID=16.0 in its os-release, too.
	system="--system-os-release VERSION_ID=$version --system-file /etc/products.d/SLES.prod"
	about='installation, administration, security, virtualization, containers'
	;;
sle-ha)
	product='SUSE Linux Enterprise High Availability'
	site=sle-ha/$version
	# An extension: the system's version, and its product file.
	system="--system-os-release VERSION_ID=$version --system-file /etc/products.d/sle-ha.prod"
	about='cluster installation, fencing and SBD, resources and constraints, maintenance, crmsh and Hawk'
	;;
sles-sap)
	product='SUSE Linux Enterprise Server for SAP applications'
	site=sles-sap/$version
	system="--system-os-release VERSION_ID=$version --system-file /etc/products.d/SLES_SAP.prod"
	about="installing and configuring a system for SAP applications, saptune, Ansible for SAP, and the administration of SLES $version as it applies to them"
	;;
*)
	echo "convert: unknown PRODUCT $name (sles, sle-ha, sles-sap; -16.0 or -16.1)" >&2
	exit 2
	;;
esac
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/$subdir" --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/$subdir%s" \
	--revision "$(echo "$rev" | cut -c1-7)" $system

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
# The index file, last: of the documents as they are now, read by the
# server where it would build the index from every document.
"$convert" --index "$out"
echo "$out/$name.yaml"
