#!/usr/bin/env bash
# Build (if needed) and start the whole stack.
source "$(dirname "${BASH_SOURCE[0]}")/common.sh"
require_docker; ensure_env; check_layout
preflight_ports
bash "$(dirname "${BASH_SOURCE[0]}")/sync-mobile-port.sh" quiet
bash "$(dirname "${BASH_SOURCE[0]}")/gen-packages.sh"
bash "$(dirname "${BASH_SOURCE[0]}")/gen-apps.sh"
bash "$(dirname "${BASH_SOURCE[0]}")/gen-resources.sh"

# Only on a full up: with an explicit service list, every other service is
# legitimately absent from the request rather than switched off.
# RUN_UP_BUILD=0 skips --build (used by restart without --build).
UP_ARGS=(-d)
if [ "${RUN_UP_BUILD:-1}" = 1 ]; then
  UP_ARGS+=(--build)
fi
if [ $# -eq 0 ]; then
  UP_ARGS+=(--remove-orphans)
  prune_disabled
  if ! mobile_enabled; then
    UP_ARGS+=(--scale mobile-client=0 --scale mobile-packages=0 --scale mobile-deps=0)
  fi
fi

if ! dc up "${UP_ARGS[@]}" "$@"; then
  status=$?
  report_failures || true
  exit "${status}"
fi
start_host_open

# Remember this project's services for autocomplete (Kiro / zsh / bash).
remember_and_list_services >/dev/null || true

# A service can also fail after compose returns, so check either way.
report_failures || exit 1

echo
echo "${PROJECT_LABEL:-${PROJECT_NAME}} is starting. First run installs composer + pnpm deps and may take a few minutes."
print_ports_recap
if desktop_enabled; then
  echo "  Desktop shell ${RUN_CMD:-run-stack} desktop   (${DESKTOP_STACK:-none} runs on the host)"
fi
if mobile_enabled; then
  echo "  Open iOS      ${RUN_CMD:-run-stack} ios   (or the iOS button on the dashboard)"
  echo "  Open Android  ${RUN_CMD:-run-stack} android"
fi
echo
echo "Follow startup with: ${RUN_CMD:-run-stack} logs"
echo "Re-print ports:      ${RUN_CMD:-run-stack} ports --show"
