#!/bin/bash

# Update gh-pages doc
# ====================
#
# This script allows to automatically update the generated documentation on the
# gh-pages branch.
#
# To use it:
#
#   1. Be sure that you're on a clean (no staged files and no diff) branch.
#
#   2. Call this script with the SEMVER version number in argument.
#      Some user interactions will be needed to avoid doing unwanted commits.
#
#   3. That's it!
#      A commit should have been pushed to the gh-pages.

set -e

current_branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
current_version="$1"

if [ -z "$current_version" ]; then
  echo "ERROR: Please call this script with your version in argument" >&2
	echo "For example: ./update_gh-pages_demo.sh 4.4.0" >&2
  exit 1
fi

# Generate documentation
npm run doc

if [ -n "$(git status --porcelain doc)" ]; then
  echo "ERROR: Please commit your modifications to \"$current_branch\"" >&2
  exit 1
fi

tmpDir=$(mktemp -d)
tmpDemoList=$(mktemp)
tmpDocList=$(mktemp)

cp -r doc/generated/* "$tmpDir"
cp -v scripts/generate_demo_list.mjs "$tmpDemoList"
cp -v scripts/generate_documentation_list.mjs "$tmpDocList"

# update gh-pages
git checkout gh-pages
git pull origin gh-pages

rm -rf "versions/$current_version/doc"
mkdir -p "versions/$current_version/doc"
rm -rf doc
mv "$tmpDir"/* "versions/$current_version/doc"
ln -s "./versions/$current_version/doc" doc
mv "$tmpDemoList" generate_demo_list.mjs
mv "$tmpDocList" generate_documentation_list.mjs

node generate_documentation_list.mjs
rm generate_documentation_list.mjs
rm generate_demo_list.mjs

if [ -n "$(git status --porcelain doc "versions/$current_version/doc" documentation_pages_by_version.html)" ]; then
  echo "-- Current Status on gh-pages: --"
  echo ""
  git status doc "versions/$current_version/doc" documentation_pages_by_version.html

  while :; do
    echo ""
    echo "We will push the documentation to gh-pages."
    REPLY=""
    read -p "do you want to continue [y/d/s/a/c/t/h] (h for help) ? " -n 1 -r
    echo ""

    if [[ $REPLY =~ ^[Hh](elp)?$ ]]; then
      echo ""
      echo ""
      echo "+- help -------------------------------------------------+"
      echo "| y: commit and continue                                 |"
      echo "| d: see diff                                            |"
      echo "| s: see status                                          |"
      echo "| a: abort script from here                              |"
      echo "| c: checkout from this commit and go to the next one    |"
      echo "| t: stash this commit and go to the next one            |"
      echo "| h: see this help                                       |"
      echo "+--------------------------------------------------------+"
    elif [[ $REPLY =~ ^[Yy](es)?$ ]]; then
      git add doc "versions/$current_version/doc" documentation_pages_by_version.html
      git commit -m "doc: deploy $current_version to the gh-pages" -S
      git push origin gh-pages
      break
    elif [[ $REPLY =~ ^[Dd](iff)?$ ]]; then
      git diff doc "versions/$current_version/doc" documentation_pages_by_version.html || true # ignore when return 1
    elif [[ $REPLY =~ ^[Ss](tatus)?$ ]]; then
      git status doc "versions/$current_version/doc" documentation_pages_by_version.html
    elif [[ $REPLY =~ ^[Aa](bort)?$ ]]; then
      echo "exiting"
      exit 1
    elif [[ $REPLY =~ ^[Cc](heckout)?$ ]]; then
      git checkout doc "versions/$current_version/doc" documentation_pages_by_version.html
    elif [[ $REPLY =~ ^[Tt]$ ]]; then
      git stash save -u doc "versions/$current_version/doc" documentation_pages_by_version.html
      break
    fi
  done
else
  echo "nothing to do on the gh-pages branch"
fi

git checkout "$current_branch"
