#!/usr/bin/env bash
#
# Compile keymanweb predictive-text components.

## START STANDARD BUILD SCRIPT INCLUDE
# adjust relative paths as necessary
THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")"
. "${THIS_SCRIPT%/*}/../../../../resources/build/builder-full.inc.sh"
## END STANDARD BUILD SCRIPT INCLUDE

. "$KEYMAN_ROOT/resources/build/utils.inc.sh"

# ################################ Main script ################################

builder_describe "Builds predictive-text components used within Keyman Engine for Web (KMW)." \
  "clean" \
  "configure" \
  "build" \
  "test" \
  ":templates                Builds the model templates utilized by compiled lexical models" \
  ":wordbreakers             Builds the wordbreakers provided for lexical model use" \
  ":worker-main              Builds the predictive-text worker interface module" \
  ":worker-thread            Builds the predictive-text worker" \
  ":_all                     (Meta build target used when targets are not specified)"

# Possible TODO?
# "upload-symbols   Uploads build product to Sentry for error report symbolification.  Only defined for $DOC_BUILD_EMBED_WEB" \

builder_parse "$@"

config=release
if builder_is_debug_build; then
  config=debug
fi

builder_describe_outputs \
  configure                     "/node_modules" \
  build:templates               "/web/src/engine/predictive-text/build/obj/index.js" \
  build:wordbreakers            "/web/src/engine/wordbreakers/build/main/obj/index.js" \
  build:worker-main             "/web/src/engine/worker-main/build/obj/lmlayer.js" \
  build:worker-thread           "/web/src/engine/worker-thread/build/obj/worker-main.wrapped.js"

BUNDLE_CMD="node ${KEYMAN_ROOT}/web/src/tools/es-bundling/build/common-bundle.mjs"

#### Build action definitions ####

# We can run all clean & configure actions at once without much issue.

builder_run_child_actions clean
builder_run_child_actions configure

## Build actions

builder_run_child_actions build:wordbreakers
builder_run_child_actions build:templates
builder_run_child_actions build:worker-thread
builder_run_child_actions build:worker-main

builder_run_child_actions test
