#!/bin/bash
# Load transformed json into FHIR service by study_accession

# stop on errors
set -e

# check env
# SS_KEY = '1tNPrdUarVkdryPPgYLQEGRjua5OU6RnAZbzOEQR6t68'
# MY_COPY_SS_KEY = '17VAXsRSOz9Y2K6RhYwSt2RJMxyeLtJq09M2O2kiSbRo'
SPREADSHEET_UUID=${SPREADSHEET_UUID:-17VAXsRSOz9Y2K6RhYwSt2RJMxyeLtJq09M2O2kiSbRo}
CLIENT_SECRET=${CLIENT_SECRET:-./client_secret.json}

unset MISSING
[ -z "$OUTPUT_PATH" ] && echo "missing: OUTPUT_PATH see anvil_etl" && MISSING="Y"
[ -z "$GOOGLE_PROJECT" ] && echo "missing: GOOGLE_PROJECT" && MISSING="Y"
[ -z "$GOOGLE_LOCATION" ] && echo "missing: GOOGLE_LOCATION" && MISSING="Y"
[ -z "$GOOGLE_DATASET" ] && echo "missing: GOOGLE_DATASET" && MISSING="Y"
[ -z "$GOOGLE_BUCKET" ] && echo "missing: GOOGLE_BUCKET" && MISSING="Y"
[ -z "$SPREADSHEET_UUID" ] && echo "missing: SPREADSHEET_UUID google spreadsheet uuid 'AnVIL_Data_Ingestion_Attributes_210225'" && MISSING="Y"
[ -z "$CLIENT_SECRET" ] && echo "missing: CLIENT_SECRET required to read broad's spreadsheet See https://gspread.readthedocs.io/en/latest/oauth2.html#for-bots-using-service-account" && MISSING="Y"
[ -z "$GOOGLE_BILLING_ACCOUNT" ] && echo "missing: GOOGLE_BILLING_ACCOUNT billing" && MISSING="Y"
[ ! -z "$MISSING" ] &&  echo "please set required env variables. see fhir_setup" && exit

# first grab spreadsheet that contains study_accession, dataUseRestriction, etc.
data_ingestion_spreadsheet \
    --output_path $OUTPUT_PATH/spreadsheet.json \
    --spreadsheet_key $SPREADSHEET_UUID \
    --json_keyfile_path $CLIENT_SECRET

accession_to_datastore dashboard \
    --output_path $OUTPUT_PATH/ \
    --accession_mapping $OUTPUT_PATH/spreadsheet.json \
    --google_bucket $GOOGLE_BUCKET \
    --user_project $GOOGLE_BILLING_ACCOUNT


# gcloud beta healthcare fhir-stores delete dev --dataset=anvil-test --location=us-west2 --quiet
# gcloud beta healthcare fhir-stores create dev --dataset=anvil-test --location=us-west2 --version=R4 --enable-update-create
# gcloud beta healthcare fhir-stores import gcs dev --dataset=anvil-test --location=us-west2 --gcs-uri=gs://fhir-test-11-329119/IG/*.json --content-structure=resource-pretty  # --async
# gcloud beta healthcare fhir-stores import gcs dev --dataset=anvil-test --location=us-west2 --gcs-uri=gs://fhir-test-11-329119/IG/valuesets/valuesets.json --content-structure=bundle-pretty # --async
# gcloud beta healthcare fhir-stores import gcs dev --dataset=anvil-test --location=us-west2 --gcs-uri=gs://fhir-test-11-329119/IG/valuesets/valueset-mimetypes.json --content-structure=resource-pretty  # --async



# create data stores, load IG into each
accession_to_datastore initialize \
    --output_path $OUTPUT_PATH/ \
    --accession_mapping $OUTPUT_PATH/spreadsheet.json \
    --google_location $GOOGLE_LOCATION \
    --google_dataset $GOOGLE_DATASET \
    --google_bucket $GOOGLE_BUCKET \
    --user_project $GOOGLE_BILLING_ACCOUNT

# adjust the partOf field in the ResearchStudy to point to full URL path
accession_to_datastore config \
    --output_path $OUTPUT_PATH/ \
    --accession_mapping $OUTPUT_PATH/spreadsheet.json \
    --google_bucket $GOOGLE_BUCKET \
    --user_project $GOOGLE_BILLING_ACCOUNT


# copy json data to bucket
gsutil -m cp -J -r $OUTPUT_PATH/Public  gs://$GOOGLE_BUCKET
gsutil -m cp -J -r $OUTPUT_PATH/GTEx    gs://$GOOGLE_BUCKET
gsutil -m cp -J -r $OUTPUT_PATH/NIMH    gs://$GOOGLE_BUCKET
gsutil -m cp -J -r $OUTPUT_PATH/NHGRI   gs://$GOOGLE_BUCKET
gsutil -m cp -J -r $OUTPUT_PATH/CMG     gs://$GOOGLE_BUCKET
gsutil -m cp -J -r $OUTPUT_PATH/CCDG    gs://$GOOGLE_BUCKET

# load from bucket to data stores
accession_to_datastore load \
    --output_path $OUTPUT_PATH/ \
    --accession_mapping $OUTPUT_PATH/spreadsheet.json \
    --google_bucket $GOOGLE_BUCKET \
    --user_project $GOOGLE_BILLING_ACCOUNT
