#!/bin/bash
# Pre-requisites for Google Healthcare API FHIR server
# exports variables used to contruct URIs and query data

unset MISSING


[ -z "$1" ] && echo "missing: GOOGLE_PROJECT_NAME the name of the project that will host the FHIR service." && MISSING="Y"
[ -z "$2" ] && echo "missing: GOOGLE_LOCATION the name of the region that will host API and buckets." && MISSING="Y"
[ ! -z "$MISSING" ] &&  echo "usage: fhir_env GOOGLE_PROJECT_NAME GOOGLE_LOCATION [GOOGLE_DATASET] [GOOGLE_DATASTORE] [BILLING_ACCOUNT] [CLIENT_SECRET] [SPREADSHEET_UUID] [OUTPUT_PATH]" && return 1

export GOOGLE_PROJECT_NAME=$1
export GOOGLE_LOCATION=$2

if [ -z "$GOOGLE_DATASET" ]; then
    export GOOGLE_DATASET=${3:-anvil-test}
fi

if [ -z "$GOOGLE_DATASTORE" ]; then
    export GOOGLE_DATASTORE=${4:-test}
fi

if [ -z "$GOOGLE_BILLING_ACCOUNT" ]; then
    # NIH-Awd.NHGRI.JHU.AnVIL
    export GOOGLE_BILLING_ACCOUNT=${5:-016D13-3B2B1C-E919A9}
fi

if [ -z "$CLIENT_SECRET" ]; then
    export CLIENT_SECRET=${6:-./client_secret.json}
fi

if [ -z "$SPREADSHEET_UUID" ]; then
    export SPREADSHEET_UUID=${7:-17VAXsRSOz9Y2K6RhYwSt2RJMxyeLtJq09M2O2kiSbRo}
fi

if [ -z "$OUTPUT_PATH" ]; then
    export OUTPUT_PATH=${8:-./DATA}
fi


export GOOGLE_PROJECT=$(gcloud projects list --filter=name=$GOOGLE_PROJECT_NAME --format="value(projectId)" )
if [ -z "$GOOGLE_PROJECT" ]; then
    # echo "Need to create project >$GOOGLE_PROJECT_NAME<"
    unset GOOGLE_PROJECT
fi    


if [ -z "$IMPLEMENTATION_GUIDE_PATH" ]; then
    export IMPLEMENTATION_GUIDE_PATH=$OUTPUT_PATH/IG
fi    


echo '***** env variables *****'

echo GOOGLE_PROJECT_NAME $GOOGLE_PROJECT_NAME  The root for the API, billing, buckets, etc.
echo GOOGLE_BILLING_ACCOUNT $GOOGLE_BILLING_ACCOUNT Google Cloud Billing Accounts allow you to configure payment and track spending in GCP.
echo GOOGLE_LOCATION $GOOGLE_LOCATION The physical location of the data
echo GOOGLE_DATASET $GOOGLE_DATASET  Datasets are top-level containers that are used to organize and control access to your stores. 
echo GOOGLE_DATASTORE $GOOGLE_DATASTORE A FHIR store is a data store in the Cloud Healthcare API that holds FHIR resources.
echo CLIENT_SECRET $CLIENT_SECRET Your google identity - used to retrieve the terra maintained spreadsheet.
echo SPREADSHEET_UUID $SPREADSHEET_UUID The spreadsheet key
echo OUTPUT_PATH $OUTPUT_PATH A directory on your local system, used to store work files.



return

# # point as this project by default.
# gcloud config set project $GOOGLE_PROJECT > /dev/null

# export TOKEN=$(gcloud auth application-default print-access-token)

# export GOOGLE_DATASTORES=$(gcloud beta healthcare fhir-stores list --dataset=$GOOGLE_DATASET --location=$GOOGLE_LOCATION --format="table[no-heading](ID)" | tr '\n' ',')

# export PYANVIL_CACHE_PATH=$OUTPUT_PATH/pyanvil-cache.sqlite

