#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2026 Marcus Baw and Baw Medical Ltd
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Install the local build onto the user's PATH (cargo install --path .).
# `sct serve` (the FHIR R4 terminology server) is included by default.
#
# Add optional extra features (see Cargo.toml):
#   --tui    sct tui  - terminal UI
#   --gui    sct gui  - browser UI
#   --dmwb   sct dmwb - NHS Data Migration Workbench .mdb reader
#   --full   all extras: tui + gui + dmwb
#
# The resolved feature set is printed before building. For a minimal build
# without the server, use `cargo install --path . --no-default-features`.

set -euo pipefail
cd "$(git rev-parse --show-toplevel)"

features=()
for arg in "$@"; do
  case "$arg" in
    --serve) echo "note: sct serve is included by default; --serve is unnecessary" >&2 ;;
    --tui)   features+=("tui") ;;
    --gui)   features+=("gui") ;;
    --dmwb)  features+=("dmwb") ;;
    --full)  features+=("tui" "gui" "dmwb") ;;
    -h|--help) sed -n '5,15p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) echo "Unknown option: $arg (try: --tui --gui --dmwb --full)" >&2; exit 1 ;;
  esac
done

if [[ ${#features[@]} -gt 0 ]]; then
  # De-duplicate (e.g. `--full --gui`) and sort for a stable message.
  feature_list=$(printf '%s\n' "${features[@]}" | sort -u | paste -sd,)
  echo "Installing sct with sct serve (default) + extras: ${feature_list}" >&2
  exec cargo install --path . --features "$feature_list"
else
  echo "Installing sct with sct serve (default); add --tui/--gui/--dmwb/--full for more" >&2
  exec cargo install --path .
fi
