#!/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' '1UT3l87bcabIXNpoKGxep-58AO93SyloUJdnVa3izeaI'
SPREADSHEET_UUID=${SPREADSHEET_UUID:-1UT3l87bcabIXNpoKGxep-58AO93SyloUJdnVa3izeaI}
GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS:-./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 "$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

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 -n $OUTPUT_PATH/Public  gs://$GOOGLE_BUCKET
gsutil -m cp -J -r -n $OUTPUT_PATH/GTEx    gs://$GOOGLE_BUCKET
gsutil -m cp -J -r -n $OUTPUT_PATH/NIMH    gs://$GOOGLE_BUCKET
gsutil -m cp -J -r -n $OUTPUT_PATH/NHGRI   gs://$GOOGLE_BUCKET
gsutil -m cp -J -r -n  $OUTPUT_PATH/CMG    gs://$GOOGLE_BUCKET
gsutil -m cp -J -r -n $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
