#!/usr/bin/env sh

if ! command -v log >/dev/null 2>&1; then
  . "${COMPOSE_DIR}/scripts/logging.include.sh"
fi

log INFO "Wait for S3 service to be available"
for i in $(seq ${THREDDS_S3_RETRY_ATTEMPTS}); do
    if [ "$(docker inspect -f '{{ .State.Health.Status }}' s3)" = "healthy" ]; then
        ln -s /thredds-data "${S3_DATA_STORE}/data/${THREDDS_S3_BUCKET_NAME}"
        ${COMPOSE_DIR}/scripts/make-s3-bucket.sh ${THREDDS_S3_BUCKET_NAME}
        log INFO -p "OK"
        exit
    fi
    log INFO -p -n "."
    sleep ${THREDDS_S3_RETRY_SLEEP_SECONDS}
done

log ERROR "S3 service is not available so thredds data could not be automatically mounted to the S3 service.
To try again, make sure that the S3 service is available and run: $(readlink -f "$0" || realpath "$0")"
