#!/usr/bin/env bash

set -e

EMSDK_DIR=target/emsdk
EMSCRIPTEN_VERSION=$(< cli/loader/emscripten-version)

{
  if [[ ! -f $EMSDK_DIR/emsdk ]]; then
    printf 'Downloading emscripten SDK...\n'
    git clone https://github.com/emscripten-core/emsdk.git $EMSDK_DIR
  fi

  cd $EMSDK_DIR

  printf 'Updating emscripten SDK...\n'
  git reset --hard
  git pull
  ./emsdk list

  printf 'Installing emscripten...\n'
  ./emsdk install "$EMSCRIPTEN_VERSION"

  printf 'Activating emscripten...\n'
  ./emsdk activate "$EMSCRIPTEN_VERSION"
} >&2
