#!/bin/sh
set -e

LAKEKEEPER_HOST="lakekeeper"
LAKEKEEPER_PORT="8181"
WAREHOUSE_NAME="iot-warehouse"
S3_BUCKET_NAME="iceberg-bucket"
S3_ENDPOINT="http://rustfs:9000"
S3_ACCESS_KEY="rustfs_admin"
S3_SECRET_KEY="rustfs_passw0rd"
S3_REGION="us-west-2"

echo "Waiting for Lakekeeper to be ready..."
until curl --silent "http://${LAKEKEEPER_HOST}:${LAKEKEEPER_PORT}/health" > /dev/null 2>&1; do
  echo "Lakekeeper not ready yet, waiting..."
  sleep 2
done
echo "Lakekeeper is ready!"

echo "Creating warehouse '${WAREHOUSE_NAME}'..."
# https://docs.lakekeeper.io/docs/latest/storage/#configuration-parameters
WAREHOUSE_RESPONSE=$(curl --silent --write-out "\n%{http_code}" --request POST "http://${LAKEKEEPER_HOST}:${LAKEKEEPER_PORT}/management/v1/warehouse" \
  --header "Content-Type: application/json" \
  --data "{
    \"warehouse-name\": \"${WAREHOUSE_NAME}\",
    \"storage-profile\": {
      \"type\": \"s3\",
      \"bucket\": \"${S3_BUCKET_NAME}\",
      \"endpoint\": \"${S3_ENDPOINT}\",
      \"region\": \"${S3_REGION}\",
      \"path-style-access\": true,
      \"flavor\": \"s3-compat\",
      \"sts-enabled\": false
    },
    \"storage-credential\": {
      \"type\": \"s3\",
      \"credential-type\": \"access-key\",
      \"aws-access-key-id\": \"${S3_ACCESS_KEY}\",
      \"aws-secret-access-key\": \"${S3_SECRET_KEY}\"
    }
  }")

HTTP_CODE=$(echo "${WAREHOUSE_RESPONSE}" | tail -n1)
RESPONSE_BODY=$(echo "${WAREHOUSE_RESPONSE}" | sed '$d')

if [ "${HTTP_CODE}" = "201" ] || [ "${HTTP_CODE}" = "200" ]; then
  echo "Warehouse '${WAREHOUSE_NAME}' created successfully!"
elif [ "${HTTP_CODE}" = "409" ]; then
  echo "Warehouse '${WAREHOUSE_NAME}' already exists."
else
  echo "Failed to create warehouse. HTTP ${HTTP_CODE}: ${RESPONSE_BODY}"
fi

echo "Lakekeeper warehouse setup complete!"
