#!/bin/bash

# Builds a local copy of KDE Docbook-based documentation for testing
# during development.

# SPDX-FileCopyrightText: 2011, 2022 Michael Pyne <mpyne@kde.org>
# SPDX-FileCopyrightText: 2023 Andrew Shark <ashark@linuxcomp.ru>
#
# SPDX-License-Identifier: BSD-2-Clause

# Turn on sanity checking
set -e
set -u
# set -o pipefail is a bashism unfortunately

# Change if you want to render to several separate files (a crutch to workaround non-working kde-nochunk.xsl)
SINGLE_FILE_OUTPUT=1

function render_docbook() {
    local PATH_TO_DOCBOOK="${PWD}/$1"
    local DEST_DIR="$2"
    local XSLT="$3"

    local LANG_ISO3166_2="en"

    # The kdoctools install process installs support files for DocBook handling to
    # the application data dir. We need some of these xsl files for meinproc to
    # use.
    local DATA_DIR
    DATA_DIR=$(kf5-config --install data)
    DATA_DIR=${DATA_DIR%/} # Remove trailing /

    local COMMON_DIR="kdoctools5-common"
    # The single quotes are exceedingly important for later. The dir path inside
    # the single quotes should be relative to make browser maximally happy; we use
    # a symlink later to make that possible but there's probably better ways to get
    # after this same goal
    local BROWSER_COMMON
    BROWSER_COMMON=$(printf "'%s/'" "$COMMON_DIR")

    local DOC_SRCDIR="$DATA_DIR/kf5/kdoctools"
    XSLT="$DOC_SRCDIR/customization/${XSLT}"

    mkdir -p "${DEST_DIR}"

    # Check for obvious errors
    if [ ! -e "${PATH_TO_DOCBOOK}" ]; then
        echo "Unable to find Docbook file. Try running from the documentation directory!" >&2
        exit 1
    fi

    if [ ! -e "${XSLT}" ]; then
        echo "Unable to find documentation style template. Is kdoctools installed?" >&2
        exit 1
    fi

    # We need to run this from right directory, i.e. the directory we want
    # generated output to go to. Ensure meinproc will find common files in this
    # directory.

    if [ ! -e "${DEST_DIR}/$COMMON_DIR" ]; then
        ln -s "$DATA_DIR/doc/HTML/$LANG_ISO3166_2/$COMMON_DIR" "${DEST_DIR}"
    fi

    local CUR_DIR
    CUR_DIR="${PWD}"
    cd "${DEST_DIR}"

    rm -rf ./*.html ./*.1  # cleaning up, because different stylesheets could render to files with different names. ".1" - extension for man.

    if [ "${SINGLE_FILE_OUTPUT}" == 1 ]; then
        PAR_OUTPUT="--output"
        PAR_OUTPUT_VAL="preview.html"
    else
        PAR_OUTPUT=""
        PAR_OUTPUT_VAL=""
    fi

    # Run meinproc to generate our output
    meinproc5 --srcdir "$DOC_SRCDIR" \
        --param kde.common="$BROWSER_COMMON" \
        --param chunker.output.encoding="UTF-8" \
        --param chunker.output.indent="yes" \
        --stylesheet "$XSLT" \
        "$PATH_TO_DOCBOOK" ${PAR_OUTPUT} ${PAR_OUTPUT_VAL}

    cd "$CUR_DIR"  # to set the correct starting folder when invoking this function for next docbooks
    echo "Rendered $1 to $2 using $3"
}

#STYLE=kde-chunk-common.xsl
STYLE=kde-chunk-online.xsl  # the official one, used in docs.kde.org website.
#STYLE=kde-chunk.xsl
#STYLE=kde-include-common.xsl  # renders with broken characters?
#STYLE=kde-include-man.xsl  # renders to man format, not html.
#STYLE=kde-navig-online.xsl
#STYLE=kde-navig.xsl
#STYLE=kde-nochunk.xsl  # still renders to several files?
#STYLE=kde-style.xsl
#STYLE=kde-ttlpg-online.xsl
#STYLE=kde-ttlpg.xsl
#STYLE=kde-web-navig.xsl
#STYLE=kde-web.xsl  # looks pretty similar to kde-chunk-online.xsl

# render_docbook usage: $1 = docbook_file, $2 = folder for rendered html files, $3 - stylesheet_file
render_docbook "index.docbook" "preview_doc_html" "$STYLE"
render_docbook "man-kdesrc-build.1.docbook" "preview_man_html" "$STYLE"
