#!/bin/bash
#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
#
# Run an HTTP GET against the API, then print out the "name" attribute only
#
# Usage:
#
#     bgetn path [options]
#
#   path     : an API path
#   options  : bscript options, see bubble.main.BubbleScriptOptions (and parent classes) for more info
#
# Environment variables
#
#   BUBBLE_API     : which API to use. Default is local (http://127.0.0.1:PORT, where PORT is found in .bubble.env)
#   BUBBLE_USER    : account to use. Default is root
#   BUBBLE_PASS    : password for account. Default is root
#   BUBBLE_INCLUDE : path to look for JSON include files. default value is to assume we are being run from
#                      bubble repo, bubble-models repo, or bubble-client and use include files from minimal model.
#
SCRIPT="${0}"
SCRIPT_DIR=$(cd $(dirname ${SCRIPT}) && pwd)
. ${SCRIPT_DIR}/bubble_common

URL="${1:?no URL provided}"
shift

JSON="$(BUBBLE_QUIET=1 ${SCRIPT_DIR}/bubble get -U ${URL} ${@})"
if [[ ${JSON} = \{* ]] ; then
  echo "${JSON}" | jq -r .name
elif [[ "${JSON}" = \[* ]] ; then
  echo "${JSON}" | jq -r .[].name
else
  echo "not sure what to do with JSON:"
  echo "${JSON}"
fi
