#!/bin/bash

set -e

# Parse arguments
spec_path=''
output_path=''
while [[ "$#" -gt 0 ]]; do case $1 in
  --spec-path) spec_path="$2"; shift;;
  --output-path) output_path="$2"; shift;;
esac; shift; done

echo "Generating HTML Redoc documentation..."

working_dir=$(pwd)
script_dir="$( cd -- "$(dirname $([ -L "${BASH_SOURCE[0]:-$0}" ] && readlink -f "${BASH_SOURCE[0]:-$0}" || echo "${BASH_SOURCE[0]:-$0}"))" >/dev/null 2>&1 ; pwd -P )";

# load common package manager helper functions
. "$script_dir/../../common/common.sh"

# Create a temporary directory
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp/}generate-docs-html-redoc.XXXXXXXXX")
cd $tmp_dir

log "html-redoc :: tmp_dir :: $tmp_dir"

# Install dependencies
install_packages

# Generate
run_command redocly build-docs "$working_dir/$spec_path" --output "$working_dir/$output_path/index.html"

echo "HTML Redoc documentation generation done!"

# Clean up
cd $working_dir
rm -rf $tmp_dir
