#!/usr/bin/env bash
# List configured apps (backend, web, admin, landing, mobile, desktop).
source "$(dirname "${BASH_SOURCE[0]}")/common.sh"

usage() {
  cat <<EOF
Usage: ${RUN_CMD:-run-stack} apps

List the apps configured for this workspace: role, on/off, package name,
compose service, host port, and path on disk.

  -h, --help   Show this help
EOF
}

relpath_from_workspace() {
  local path="$1"
  local prefix="${WORKSPACE_DIR%/}/"
  case "${path}" in
    "${prefix}"*) printf './%s' "${path#"${prefix}"}" ;;
    *) printf '%s' "${path}" ;;
  esac
}

on_off() {
  if "$@"; then printf 'yes'; else printf 'no'; fi
}

print_apps() {
  local rows path service port package
  rows="$(printf '%s\t%s\t%s\t%s\t%s\t%s' "ROLE" "ON" "PACKAGE" "SERVICE" "PORT" "PATH")"

  # backend
  path="$(relpath_from_workspace "${BACKEND_APP_DIR%/}")"
  rows="${rows}$(printf '\n%s\t%s\t%s\t%s\t%s\t%s' \
    "backend" "yes" "${BACKEND_STACK:-laravel}" "backend" "${BACKEND_PORT:-8000}" "${path}")"

  # web (always on)
  package="${WEB_APP:-web}"
  path="$(frontend_app_dir "${package}")"
  [ -n "${path}" ] && path="$(relpath_from_workspace "${path}")" || path="—"
  rows="${rows}$(printf '\n%s\t%s\t%s\t%s\t%s\t%s' \
    "web" "yes" "${package}" "web" "${WEB_PORT:-5173}" "${path}")"

  # admin
  package="${ADMIN_APP:-admin}"
  path="$(frontend_app_dir "${package}")"
  [ -n "${path}" ] && path="$(relpath_from_workspace "${path}")" || path="—"
  rows="${rows}$(printf '\n%s\t%s\t%s\t%s\t%s\t%s' \
    "admin" "$(on_off is_true "${RUN_ADMIN:-true}")" "${package}" "admin" "${ADMIN_PORT:-5174}" "${path}")"

  # landing
  package="${LANDING_APP:-landing}"
  path="$(frontend_app_dir "${package}")"
  [ -n "${path}" ] && path="$(relpath_from_workspace "${path}")" || path="—"
  rows="${rows}$(printf '\n%s\t%s\t%s\t%s\t%s\t%s' \
    "landing" "$(on_off is_true "${RUN_LANDING:-true}")" "${package}" "landing" "${LANDING_PORT:-5175}" "${path}")"

  # mobile
  package="${MOBILE_APP:-mobile-client}"
  path="$(mobile_app_dir "${package}" 2>/dev/null || true)"
  [ -n "${path}" ] && path="$(relpath_from_workspace "${path}")" || path="—"
  rows="${rows}$(printf '\n%s\t%s\t%s\t%s\t%s\t%s' \
    "mobile" "$(on_off mobile_enabled)" "${package}" "mobile-client" "${MOBILE_CLIENT_PORT:-8081}" "${path}")"

  # desktop
  package="${DESKTOP_APP:-desktop}"
  path="$(frontend_app_dir "${package}")"
  [ -n "${path}" ] && path="$(relpath_from_workspace "${path}")" || path="—"
  rows="${rows}$(printf '\n%s\t%s\t%s\t%s\t%s\t%s' \
    "desktop" "$(on_off desktop_enabled)" "${package}" "desktop" "${DESKTOP_PORT:-5176}" "${path}")"

  # Extra apps, in the order EXTRA_APPS lists them.
  local name role
  while IFS= read -r name; do
    path="$(frontend_app_dir "${name}")"
    [ -n "${path}" ] && path="$(relpath_from_workspace "${path}")" || path="—"
    role=web
    extra_app_is_mobile "${name}" && role=mobile
    rows="${rows}$(printf '\n%s\t%s\t%s\t%s\t%s\t%s' \
      "${role}" "yes" "${name}" "${name}" "$(extra_app_port "${name}")" "${path}")"
  done < <(extra_app_names)

  _print_tsv_table "Apps" "${rows}"
}

case "${1:-}" in
  "" )
    ensure_env
    print_apps
    ;;
  -h|--help|help)
    usage
    ;;
  *)
    echo "unknown option: $1" >&2
    usage >&2
    exit 1
    ;;
esac
