#!/bin/bash

BUCKET="nextflow-ci-oss-kms"
PREFIX="work/ci-test/publish-s3/$(uuidgen)"
OUTDIR="s3://$BUCKET/$PREFIX"

function check_kms_key() {
  aws s3api head-object --bucket "$BUCKET" --key "$PREFIX/HELLO.tsv" | grep -c 28c34bc1-d8da-4682-9b2e-3356aeb77cc9
}

# Skip test if AWS keys are missing
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
  echo "Missing AWS credentials -- Skipping test"
  exit 0
fi

#
# run normal mode 
#
$NXF_RUN -c .config --outdir "$OUTDIR" | tee .stdout
[[ $(grep INFO .nextflow.log | grep -c 'Submitted process') == 1 ]] || false
[[ $(check_kms_key) == 1 ]] || false

#
# run resume mode 
#
$NXF_RUN -c .config --outdir "$OUTDIR"  -resume | tee .stdout
[[ $(grep INFO .nextflow.log | grep -c 'Cached process') == 1 ]] || false
[[ $(check_kms_key) == 1 ]] || false
