#!/usr/bin/env bash

brt_portscan() {
  local host="${1:-localhost}"
  local port_spec="${2:-}"

  if [[ -z "$host" ]]; then
    brt_error "Usage: brt -portscan <host> [ports|start-end]"
    exit 1
  fi

  brt_info "Scanning ${host}..."
  local open=0 total=0

  while IFS= read -r port; do
    [[ -z "$port" ]] && continue
    ((total++)) || true
    if (echo >/dev/tcp/"$host"/"$port") 2>/dev/null; then
      echo -e "\033[32mOPEN\033[0m  ${host}:${port}"
      ((open++)) || true
    fi
  done < <(brt_parse_ports "$port_spec")

  echo
  brt_ok "Done: ${open}/${total} ports open"
}
