#!/bin/bash
# Generate doxygen docs into outdir from bazel targets

set -euo pipefail

if [ $# -lt 2 ]
then
  echo "Usage: generate_doxygen.sh <output_directory> <target> [<targets>...]"
  echo "    output_directory    A directory or path where generated doxygen html and files will be placed."
  echo "                        Created relative to current working directory."
  echo "    target(s)           Any number of doxygen bazel targets."
  exit 1
fi

outdir="$PWD/$1"
mkdir -p "$outdir"
shift
bazel build "$@"

# Change directory to top level, find and unpack doxygen.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$DIR/../../"
for doc in $(find bazel-bin/ -name '*_doxygen.tgz')
do
  tar --extract --file "$doc" --directory "$outdir"
done
