#! /bin/bash

# Render one PCRE2 AsciiDoc source as both a manpage and an HTML document.

if [ $# != 3 ] ; then
  echo "Usage: $0 input.adoc output.1-or-3 output.html" >&2
  exit 1
fi

if ! command -v asciidoctor >/dev/null ; then
  echo "asciidoctor is required to render documentation" >&2
  exit 1
fi

if ! ruby -r rouge -e '' 2>/dev/null ; then
  echo "the Rouge Ruby gem is required to render documentation" >&2
  exit 1
fi

input=$1
man_output=$2
html_output=$3
script_dir=$(cd "$(dirname "$0")" && pwd)

mkdir -p "$(dirname "$man_output")" "$(dirname "$html_output")"
asciidoctor --failure-level WARN --require "$script_dir/AsciidocConverterPcre2.rb" --backend manpage --out-file "$man_output" "$input"
asciidoctor --failure-level WARN --require "$script_dir/AsciidocConverterPcre2.rb" --backend html5 --attribute nofooter --out-file "$html_output" "$input"