#!/bin/bash
set -e

# This script is used in order to download notebooks that are hosted on
# Read the Docs, that'll be linked from the documentation generated by
# Sphinx-Gallery.
#
# This may fail if run from branch other than 'main' or those built on
# Read the Docs.

# Extract branch/tag name from request
branch="$(basename "${BINDER_REQUEST:-/main}")"

# Special case as RTD uses 'latest' not 'main'
if [ "$branch" == "main" ];then
  branch="latest"
fi

# Download notebooks
for section in tutorials examples demos;do
  file="auto_${section}_jupyter.zip"
  # Download URL based on hash of location (generated by Sphinx).
  hash=$(echo -n "auto_${section}/${file}" | md5sum | cut -f1 -d\ )
  curl -O "https://stonesoup.readthedocs.io/en/${branch}/_downloads/${hash}/${file}"
  # Extract out into notebooks area
  mkdir -p "notebooks/auto_${section}"
  unzip -d "notebooks/auto_${section}" "${file}"
done

# Link sample files used in demos
ln -s ../../docs/demos/SolentAIS_20160112_130211.csv notebooks/auto_demos/
ln -s ../../docs/demos/UAV_Rot.csv notebooks/auto_demos/
ln -s ../../docs/demos/OpenSky_Plane_States.csv notebooks/auto_demos/

# Install Stone Soup and any extra packages required
pip install -e . folium
