#!/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 .).
# The default build already includes `sct serve` (the FHIR R4 terminology
# server) and the terminal UI (`sct tui`, plus the interactive `sct sayt`) -
# both the `serve` and `tui` Cargo features are on by default.
#
# Add optional extra features (see Cargo.toml):
#   --gui    sct gui  - browser UI
#   --dmwb   sct dmwb - NHS Data Migration Workbench .mdb reader
#   --diagram-svg     built-in SVG output for sct diagram
#   --full   all extras (serve + tui are already default)
#
# --serve and --tui are accepted but unnecessary (already in the default
# build). For a minimal build without the server or terminal UI, 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)   echo "note: sct tui is included by default; --tui is unnecessary" >&2 ;;
    --gui)   features+=("gui") ;;
    --dmwb)  features+=("dmwb") ;;
    --diagram-svg) features+=("diagram-svg") ;;
    --full)  features+=("gui" "dmwb" "diagram-svg") ;;
    -h|--help) sed -n '5,18p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) echo "Unknown option: $arg (try: --gui --dmwb --diagram-svg --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 the default build (sct serve + sct tui) + extras: ${feature_list}" >&2
  exec cargo install --path . --features "$feature_list"
else
  echo "Installing sct with the default build (sct serve + sct tui); add --gui/--dmwb/--diagram-svg/--full for more" >&2
  exec cargo install --path .
fi
