#!/usr/bin/env bash

set -e

if [[ "$1" == "--help" || "$1" == "-h" ]]; then
  cat <<EOF
USAGE

  $0 [--debug]

SUMMARY

  Compile the Tree-sitter WASM library. This will create two files in the
  \`lib/binding_web\` directory: \`tree-sitter.js\` and \`tree-sitter.wasm\`.

REQUIREMENTS

  You must have the \`docker\` command on your PATH for this to work.

OPTIONS

  --debug: Compile the library more quickly, with fewer optimizations and more runtime assertions.

EOF
  exit 0
fi

web_dir=lib/binding_web
exports=$(cat ${web_dir}/exports.json)
args="-Os"
minify=1

if [[ "$1" == "--debug" ]]; then
  minify=0
  args="-s ASSERTIONS=1 -s SAFE_HEAP=1 -Os"
fi

mkdir -p target/scratch

docker run                          \
  --rm                              \
  -v $(pwd):/src                    \
  -u $(id -u)                       \
  -e EMCC_FORCE_STDLIBS=libc++      \
  trzeci/emscripten-slim            \
                                    \
  emcc                              \
  -s WASM=1                         \
  -s TOTAL_MEMORY=33554432          \
  -s ALLOW_MEMORY_GROWTH            \
  -s MAIN_MODULE=2                  \
  -s NO_FILESYSTEM=1                \
  -s "EXPORTED_FUNCTIONS=${exports}" \
  $args                             \
  -std=c99                          \
  -D 'fprintf(...)='                \
  -I lib/src                        \
  -I lib/include                    \
  -I lib/utf8proc                   \
  --js-library ${web_dir}/imports.js   \
  --pre-js ${web_dir}/prefix.js        \
  --post-js ${web_dir}/binding.js      \
  lib/src/lib.c                     \
  ${web_dir}/binding.c                 \
  -o target/scratch/tree-sitter.js


if [[ "$minify" == "1" ]]; then
  if [ ! -d ${web_dir}/node_modules/terser ]; then
    (
      cd ${web_dir}
      npm install
    )
  fi
  ${web_dir}/node_modules/.bin/terser   \
    --compress                       \
    --mangle                         \
    --keep-fnames                    \
    --keep-classnames                \
    -- target/scratch/tree-sitter.js \
    > $web_dir/tree-sitter.js
else
  cp target/scratch/tree-sitter.js $web_dir/tree-sitter.js
fi

mv target/scratch/tree-sitter.wasm $web_dir/tree-sitter.wasm
