#!/bin/bash

# Update gh-pages demo
# ====================
#
# This script allows to automatically update the demo 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.
#      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 demo
npm run demo -- --minify

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

tmpFontsDir=$(mktemp -d)
tmpAssetsDir=$(mktemp -d)
tmpStylesDir=$(mktemp -d)
tmpDemoFile=$(mktemp)
tmpIndexFile=$(mktemp)
tmpFaviconFile=$(mktemp)
tmpWorkerFile=$(mktemp)
tmpMpdWasmFile=$(mktemp)
tmpDemoList=$(mktemp)

cp -rv demo/fonts "$tmpFontsDir"
cp -rv demo/assets "$tmpAssetsDir"
cp -rv demo/styles "$tmpStylesDir"
cp -v demo/bundle.js "$tmpDemoFile"
cp -v demo/index.html "$tmpIndexFile"
cp -v demo/plus.ico "$tmpFaviconFile"
cp -v demo/worker.js "$tmpWorkerFile"
cp -v dist/mpd-parser.wasm "$tmpMpdWasmFile"
cp -v scripts/generate_demo_list.mjs "$tmpDemoList"

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

rm -rf "versions/$current_version/demo"
mkdir -p "versions/$current_version/demo"
rm index.html
rm plus.ico
rm worker.js
rm mpd-parser.wasm
rm bundle.js
rm -rf fonts
rm -rf assets
rm -rf styles
mv "$tmpIndexFile" "versions/$current_version/demo/index.html"
mv "$tmpFaviconFile" "versions/$current_version/demo/plus.ico"
mv "$tmpDemoFile" "versions/$current_version/demo/bundle.js"
mv "$tmpWorkerFile" "versions/$current_version/demo/worker.js"
mv "$tmpMpdWasmFile" "versions/$current_version/demo/mpd-parser.wasm"
mv "$tmpFontsDir/fonts" "versions/$current_version/demo/fonts"
mv "$tmpAssetsDir/assets" "versions/$current_version/demo/assets"
mv "$tmpStylesDir/styles" "versions/$current_version/demo/styles"
mv "$tmpDemoList" generate_demo_list.mjs
ln -s "./versions/$current_version/demo/index.html" index.html
ln -s "./versions/$current_version/demo/plus.ico" plus.ico
ln -s "./versions/$current_version/demo/bundle.js" bundle.js
ln -s "./versions/$current_version/demo/worker.js" worker.js
ln -s "./versions/$current_version/demo/mpd-parser.wasm" mpd-parser.wasm
ln -s "./versions/$current_version/demo/fonts" fonts
ln -s "./versions/$current_version/demo/assets" assets
ln -s "./versions/$current_version/demo/styles" styles

node generate_demo_list.mjs
rm generate_demo_list.mjs

if [ -n "$(git status --porcelain bundle.js worker.js mpd-parser.wasm plus.ico styles fonts assets index.html "versions/$current_version/demo" demo_page_by_version.html)" ]; then
  echo "-- Current Status on gh-pages: --"
  echo ""
  git status bundle.js worker.js mpd-parser.wasm plus.ico styles fonts assets index.html "versions/$current_version/demo" demo_page_by_version.html

  while :; do
    echo ""
    echo "We will push the demo 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 bundle.js worker.js mpd-parser.wasm styles fonts assets index.html plus.ico "versions/$current_version/demo" demo_page_by_version.html
      git commit -m "demo: deploy $current_version to the gh-pages" -S
      git push origin gh-pages
      break
    elif [[ $REPLY =~ ^[Dd](iff)?$ ]]; then
      git diff bundle.js worker.js mpd-parser.wasm styles fonts assets index.html plus.ico "versions/$current_version/demo" demo_page_by_version.html || true # ignore when return 1
    elif [[ $REPLY =~ ^[Ss](tatus)?$ ]]; then
      git status bundle.js worker.js mpd-parser.wasm styles fonts assets index.html plus.ico "versions/$current_version/demo" demo_page_by_version.html
    elif [[ $REPLY =~ ^[Aa](bort)?$ ]]; then
      echo "exiting"
      exit 0
    elif [[ $REPLY =~ ^[Cc](heckout)?$ ]]; then
      git checkout bundle.js worker.js mpd-parser.wasm styles fonts assets index.html plus.ico "versions/$current_version/demo" demo_page_by_version.html
    elif [[ $REPLY =~ ^[Tt]$ ]]; then
      git stash save -u bundle.js worker.js mpd-parser.wasm styles fonts assets index.html plus.ico "versions/$current_version/demo" demo_page_by_version.html
      break
    fi
  done
else
  echo "nothing to do on the gh-pages branch"
fi

git checkout "$current_branch"
