#!/bin/bash

OPENSFM="$(dirname "${BASH_SOURCE[0]}")/"
NODE_MODULES="$OPENSFM/node_modules"
UNPKG="https://unpkg.com"
PKG="package.json"

# dat.gui
DAT_PKG="dat.gui"
DAT_V="$DAT_PKG@0.7.7"
DAT_DIST="build"
DAT_JS="dat.gui.module.js"
DAT_IN="$DAT_V/$DAT_DIST/$DAT_JS"
DAT_OUT="$NODE_MODULES/$DAT_PKG/$DAT_DIST/$DAT_JS"

curl -fS --create-dirs "$UNPKG/$DAT_IN" \
    -o "$DAT_OUT" || exit 1
curl -fS "$UNPKG/$DAT_V/$PKG" \
    -o "$NODE_MODULES/$DAT_PKG/$PKG" || exit 1

# gl-matrix
GLM_PKG="gl-matrix"
GLM_V="$GLM_PKG@3.3.0"
GLM_DIST="esm"
GLM_COM="common.js"
GLM_MAT="mat4.js"
GLM_VEC="vec3.js"
GLM_IN="$GLM_V/$GLM_DIST"
GLM_OUT="$NODE_MODULES/$GLM_PKG/$GLM_DIST"

curl -fS --create-dirs "$UNPKG/$GLM_IN/$GLM_COM" \
    -o "$GLM_OUT/$GLM_COM" || exit 1
curl -fS --create-dirs "$UNPKG/$GLM_IN/$GLM_MAT" \
    -o "$GLM_OUT/$GLM_MAT" || exit 1
curl -fS --create-dirs "$UNPKG/$GLM_IN/$GLM_VEC" \
    -o "$GLM_OUT/$GLM_VEC" || exit 1
curl -fS "$UNPKG/$GLM_V/$PKG" \
    -o "$NODE_MODULES/$GLM_PKG/$PKG" || exit 1

# mapillary-js
MJS_PKG="mapillary-js"
MJS_V="$MJS_PKG@4.0.0"
MJS_DIST="dist"
MJS_JS="mapillary.module.js"
MJS_CSS="mapillary.css"
MJS_IN="$MJS_V/$MJS_DIST"
MJS_OUT="$NODE_MODULES/$MJS_PKG/$MJS_DIST"

curl -fS --create-dirs "$UNPKG/$MJS_IN/$MJS_JS" \
    -o "$MJS_OUT/$MJS_JS" || exit 1
curl -fS --create-dirs "$UNPKG/$MJS_IN/$MJS_CSS" \
    -o "$MJS_OUT/$MJS_CSS" || exit 1
curl -fS "$UNPKG/$MJS_V/$PKG" \
    -o "$NODE_MODULES/$MJS_PKG/$PKG" || exit 1

# three
THR_PKG="three"
THR_V="$THR_PKG@0.127.0"
THR_DIST="build"
THR_JS="three.module.js"

THR_IN="$THR_V/$THR_DIST"
THR_OUT="$NODE_MODULES/$THR_PKG/$THR_DIST"
curl -fS --create-dirs "$UNPKG/$THR_IN/$THR_JS" \
    -o "$THR_OUT/$THR_JS" || exit 1
curl -fS "$UNPKG/$THR_V/$PKG" \
    -o "$NODE_MODULES/$THR_PKG/$PKG" || exit 1

THR_EXM="examples/jsm"
THR_EXM_JS="controls/OrbitControls.js"
THR_EXM_IN="$THR_V/$THR_EXM"
THR_EXM_OUT="$NODE_MODULES/$THR_PKG/$THR_EXM"
curl -fS --create-dirs "$UNPKG/$THR_EXM_IN/$THR_EXM_JS" \
    -o "$THR_EXM_OUT/$THR_EXM_JS" || exit 1
