#!/usr/bin/env bash

set -e

GIT_VERSION=r80

[ -n "${SAGE_ROOT}" ] || SAGE_ROOT="$(pwd)/../../../"


# Fetch latest version.
URL1="https://raw.githubusercontent.com/mrdoob/three.js/${GIT_VERSION}/build/three.min.js"
echo "Downloading $URL1"
curl -OL "$URL1"

URL2="https://raw.githubusercontent.com/mrdoob/three.js/${GIT_VERSION}/examples/js/controls/OrbitControls.js"
echo "Downloading $URL2"
curl -OL "$URL2"

URL3="https://raw.githubusercontent.com/mrdoob/three.js/${GIT_VERSION}/LICENSE"
echo "Downloading $URL3"
curl -OL "$URL3"

# Package
tar czf "$SAGE_ROOT/upstream/threejs-${GIT_VERSION}.tar.gz" 'three.min.js' 'OrbitControls.js' 'LICENSE'
rm -rf 'three.min.js' 'OrbitControls.js' 'LICENSE'

# Update package info
echo "${GIT_VERSION}" > 'package-version.txt'
"$SAGE_ROOT"/sage --package fix-checksum

