#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Exit if any error occurs
set -e

usage() {
  echo "Usage: $0 [-n] [-o]"
  echo ""
  echo "  -n   Enable openssl legacy providers from Node (for Docusaurus builds)"
  echo "  -o   Only Docusaurus (skip Sphinx, tutorials). Useful when just make change to Docusaurus settings."
  echo "Build balance documentation. Must be executed from root of balance repository."
  echo ""
  exit 1
}

NODE_LEGACY_SSL=false
ONLY_DOCUSAURUS=false

autosync_doc() {
  sed -i -e '/<\!--AUTOGENERATED START-->/r '"$1" \
    -e 's/<\!--AUTOGENERATED END-->/&/' "$2" || exit
}

cleanup_doc() {
  sed -i '/<\!--AUTOGENERATED START-->/,/<\!--AUTOGENERATED END-->/{/^<\!--AUTOGENERATED START-->/!{/<\!--AUTOGENERATED END-->/!d}}' "$1" || exit
}

while getopts 'hno' flag; do
  case "${flag}" in
    h)
      usage
      ;;
    n)
      NODE_LEGACY_SSL=true
      ;;
    o)
      ONLY_DOCUSAURUS=true
      ;;
    *)
      usage
      ;;
  esac
done


echo "--------------------------------------------"
echo "Pre-process Docusaurus docs"
echo "--------------------------------------------"
# For idempotency/safety, remove any content between the autogen delimiters
cleanup_doc "website/docs/docs/overview.md"
cleanup_doc "website/docs/docs/contributing.md"

# Copy the contents of README.md -> website/docs/docs/overview.md
autosync_doc "README.md" "website/docs/docs/overview.md"
autosync_doc "CONTRIBUTING.md" "website/docs/docs/contributing.md"

if [[ $ONLY_DOCUSAURUS == false ]]; then
  echo "-----------------------------------"
  echo "Generating API reference via Sphinx"
  echo "-----------------------------------"
  cd sphinx || exit
  make html
  cd .. || exit

  echo "--------------------------------------------"
  echo "Moving Sphinx artifacts to Docusaurus"
  echo "--------------------------------------------"
  mkdir -p "website/static/api_reference/"
  cp -r sphinx/_build/* website/static/api_reference/ || exit

  echo "-----------------------------------"
  echo "Building tutorial HTML"
  echo "-----------------------------------"
  jupyter nbconvert tutorials/balance_quickstart.ipynb --execute --to html \
    --output-dir website/static/html/tutorials
  jupyter nbconvert tutorials/balance_quickstart_cbps.ipynb --execute --to html \
    --output-dir website/static/html/tutorials
  jupyter nbconvert tutorials/balance_quickstart_rake.ipynb --execute --to html \
    --output-dir website/static/html/tutorials
  jupyter nbconvert tutorials/balance_transformations_and_formulas.ipynb --execute --to html \
    --output-dir website/static/html/tutorials
  jupyter nbconvert tutorials/comparing_cbps_in_r_vs_python_using_sim_data.ipynb --execute --to html \
    --output-dir website/static/html/tutorials
fi

echo "-----------------------------------"
echo "Getting Docusaurus deps"
echo "-----------------------------------"
cd website || exit
yarn

echo "-----------------------------------"
echo "Building static Docusaurus site"
echo "-----------------------------------"
if [[ $NODE_LEGACY_SSL == true ]]; then
  NODE_OPTIONS=--openssl-legacy-provider yarn build
else
  yarn build
fi
cd .. || exit


echo "--------------------------------------------"
echo "Clean up"
echo "--------------------------------------------"
cleanup_doc "website/docs/docs/overview.md"
cleanup_doc "website/docs/docs/contributing.md"
