#!/bin/sh

# Copyright © 2014-2018 Jakub Wilk <jwilk@jwilk.net>
#
# This file is part of pdf2djvu.
#
# pdf2djvu is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# pdf2djvu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

set -e -u

usage()
{
    printf '%s [--lang] <src> <dst>\n' "$0"
}

here=$(dirname "$0")
args=$(getopt -n "$0" -o 'h' --long 'help,lang' -- "$@")
eval set -- "$args"
opt_lang=
while true
do
    case "$1" in
        -h|--help) usage; exit 0;;
        --lang) opt_lang=y; shift;;
        --) shift; break;;
        *) printf '%s: internal error (%s)\n' "$0" "$1" >&2; exit 1;;
    esac
done
[ $# -eq 2 ] || { usage; exit 1; }
src="$1"
dst="$2"
xsl="$here/manpage.xsl"
xmllint='xmllint --valid --noout --nonet'
xsltproc='xsltproc --nonet'
[ -n "$opt_lang" ] && xsltproc="$xsltproc --param man.output.lang.in.name.enabled 1"
PS4='$ '
set -x
$xmllint "$src"
$xsltproc "$xsl" "$src"
perl -pi -e '# work-arounds for DocBook XSL capitalization bugs
if (/^[.]SH /) {
    s/(\\[(][,~\x27][a-z])/\U$1/g; # https://bugs.debian.org/758262#20
    s/\\[(]ss/SS/g; # https://bugs.debian.org/773444
}' "$dst"

# vim:ts=4 sts=4 sw=4 et
