#!/bin/bash

# Copyright 2013, 2016 Newport Software Technologies, UNICEN
#
# This file is part of XGAP.
#
# XGAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# XGAP 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with XGAP.  If not, see <http://www.gnu.org/licenses/>.

set -o nounset

MAIN_FILE=manual.txt
LANGS='es en'
DOCS_DIR="$( cd -P "$( dirname "$0" )" && pwd )"
# Another version. Command readlink is not always available.
#DOCS_DIR="$(dirname "$(readlink -fn  "$0")")"
BASE_DIR="${DOCS_DIR}/manual"

ASCIIDOC_DBLATEX_FILE='/etc/asciidoc/dblatex/asciidoc-dblatex.sty'
CUSTOM_DBLATEX_SRC_FILE="$DOCS_DIR/custom-dblatex.sty"
CUSTOM_DBLATEX_DST_FILE="$DOCS_DIR/custom-dblatex-full.sty"

[ -f "$ASCIIDOC_DBLATEX_FILE" ] || { echo "AsciiDoc's dblatex style file not found. Check ASCIIDOC_DBLATEX_FILE variable definition."; exit 1; }

# dblatex only supports a single style file and AsciiDoc already uses one, so we
# need to build a new one from AsciiDoc's file and ours, and pass it to dblatex.
cat "$ASCIIDOC_DBLATEX_FILE" "$CUSTOM_DBLATEX_SRC_FILE" > "$CUSTOM_DBLATEX_DST_FILE"

for dir in $LANGS ; do

  cd "$BASE_DIR/$dir"

  echo "Generating $dir..."

  # Generate HTML output
  for src in *.txt; do
    echo "  -> $src"
    asciidoc -a lang=$dir -b html "$src"
  done
  # Generate PDF output
  #   Options documentation at http://dblatex.sourceforge.net/
  #   "Appendix A. Dblatex XSL Parameter Reference"
  #   -P latex.output.revhistory=0                                Suprime el historial de revisiones
  #   -P doc.layout="coverpage toc frontmatter mainmatter index"  Secciones a incluir; las no mencionadas no se incluyen
  #   -P doc.lot.show=figure,table,equation,example               Listas de titulos a incluir
  echo "  -> PDF"
  a2x -a lang=$dir -f pdf \
    --dblatex-opts "-P doc.lot.show=figure,example -P latex.output.revhistory=0 -s '$CUSTOM_DBLATEX_DST_FILE'" \
    "$MAIN_FILE"
done
