#!/bin/bash

# Generate docs for a package, and update the docs on the `gh-pages` branch.

set -e

# These docs are generated by TypeDoc+Gitbook
GITBOOK_DOCS=(abi light.js)
# These docs are simple READMEs
README_DOCS=(api contracts electron light.js-react)

# The directory that will be pushed to gh-pages
GHPAGES_DIR="$(pwd)/gh_pages"
mkdir -p $GHPAGES_DIR

# Generate GITBOOK_DOCS
for SCOPE in "${GITBOOK_DOCS[@]}"
do
    # Generate latest version of docs
    echo "Generating docs for $SCOPE"
    pushd . # We're in the root folder
    cd "packages/$SCOPE"
    yarn docs
    cd docs
    gitbook build # Outputs inside a local `_book` folder

    # Copy these generated html docs temporarily in a temp folder
    cp -r "_book" "$GHPAGES_DIR/$SCOPE"
    popd # Go back to root folder
done

# Generate README_DOCS
for SCOPE in "${README_DOCS[@]}"
do
    # Copy the README inside $GHPAGES_DIR
    echo "Generating docs for $SCOPE"
    mkdir -p "$GHPAGES_DIR/$SCOPE"
    cp "packages/$SCOPE/README.md" "$GHPAGES_DIR/$SCOPE"
done

# Finally, copy the main README into $GHPAGES_DIR
cp README.md $GHPAGES_DIR

# Add a .nojekyll file to bypass Jekyll on Github Pages
# See https://github.com/paritytech/js-libs/issues/146
touch "$GHPAGES_DIR/.nojekyll"

# Docs are updated on master, we commit back
git checkout master
git add .
git commit -m "docs: update docs [ci skip]"
