#!/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 scripts directory.

# run this script from the project root using `./scripts/build_docs.sh`

usage() {
  echo "Usage: $0 [-b] [-o]"
  echo ""
  echo "Build AEPsych documentation. Must be executed from root of AEPsych repository."
  echo ""
  echo "  -b   Build static version of documentation (otherwise start server)."
  echo "  -o   Only Docusaurus (skip Sphinx, tutorials). Useful when just make change to Docusaurus settings."
  echo ""
  exit 1
}

echo "-----------------------------------"
echo "Checking environment "
echo "-----------------------------------"

os=$(uname -s)
echo "Operating System: $os"

check_command() {
    command=$1
    if ! command -v "$command" >/dev/null 2>&1; then
        echo "Error: $command is not installed."
        exit 1
    fi
}

check_command python
check_command node
check_command yarn
check_command make
check_command sphinx-build

missing_deps=$(python -m pip check -r requirements.txt 2>&1 | awk -F': ' '/is not installed/ {print $2}')

if [ -n "$missing_deps" ]; then
    echo "Error: Some dependencies in requirements.txt are not installed."
    echo "Missing dependencies: $missing_deps"
    echo "Please run 'pip install -r requirements.txt' in the root of the 'aepsych' directory."
    exit 1
fi

echo "All required dependencies are installed."

echo "python version: $(python --version)"
echo "node version: $(node --version)"
echo "yarn version: $(yarn --version)"
echo "Sphinx version: $(sphinx-build --version)"
echo "-----------------------------------"

BUILD_STATIC=false
ONLY_DOCUSAURUS=false

while getopts 'bho' flag; do
  case "${flag}" in
    b)
      BUILD_STATIC=true
      ;;
    h)
      usage
      ;;
    o)
      ONLY_DOCUSAURUS=true
      ;;
    *)
      usage
      ;;
  esac
done

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

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

if [[ $ONLY_DOCUSAURUS == false ]]; then
  # run script to parse html generated by sphinx
  echo "--------------------------------------------"
  echo "Parsing Sphinx docs and moving to Docusaurus"
  echo "--------------------------------------------"
  cd ..
  mkdir -p "website/pages/api/"

  cwd=$(pwd)
  python scripts/parse_sphinx.py -i "${cwd}/sphinx/build/html/" -o "${cwd}/website/pages/api/"

  SPHINX_JS_DIR='sphinx/build/html/_static/'
  DOCUSAURUS_JS_DIR='website/static/js/'

  mkdir -p $DOCUSAURUS_JS_DIR

  # move JS files from /sphinx/_build/html/_static/*:
  cp "${SPHINX_JS_DIR}documentation_options.js" "${DOCUSAURUS_JS_DIR}documentation_options.js"
  cp "${SPHINX_JS_DIR}jquery.js" "${DOCUSAURUS_JS_DIR}jquery.js"
  cp "${SPHINX_JS_DIR}underscore.js" "${DOCUSAURUS_JS_DIR}underscore.js"
  cp "${SPHINX_JS_DIR}doctools.js" "${DOCUSAURUS_JS_DIR}doctools.js"
  cp "${SPHINX_JS_DIR}language_data.js" "${DOCUSAURUS_JS_DIR}language_data.js"
  cp "${SPHINX_JS_DIR}searchtools.js" "${DOCUSAURUS_JS_DIR}searchtools.js"

  # searchindex.js is not static util
  cp "sphinx/build/html/searchindex.js" "${DOCUSAURUS_JS_DIR}searchindex.js"

  # copy module sources
  cp -r "sphinx/build/html/_sources/" "website/static/_sphinx-sources/"

  echo "-----------------------------------"
  echo "Generating tutorials"
  echo "-----------------------------------"
  mkdir -p "website/_tutorials"
  mkdir -p "website/static/files"
  python scripts/parse_tutorials.py -w "${cwd}"

  echo "-----------------------------------"
  echo "Generating demos"
  echo "-----------------------------------"
  mkdir -p "website/_demos"
  mkdir -p "website/static/files/demos"
  python scripts/parse_demos.py -w "${cwd}"

  cd website || exit

fi  # end of not only Docusaurus block

if [[ $BUILD_STATIC == true ]]; then
  echo "-----------------------------------"
  echo "Building static site"
  echo "-----------------------------------"
  yarn build || npm run build
else
  echo "-----------------------------------"
  echo "Starting local server"
  echo "-----------------------------------"
  yarn start || npm start
fi
