#!/bin/bash
# --------------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed
# with this work for additional information regarding copyright
# ownership. The ASF licenses this file to You under the Apache
# License, Version 2.0 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of the
# License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# --------------------------------------------------------------------

set -e

WORKSPACE_DIR=${WORKSPACE_DIR:-/home/gpadmin/workspace}
MINIO_BIN=${WORKSPACE_DIR}/minio
MC_BIN=${WORKSPACE_DIR}/mc
MINIO_DATA_DIR=${MINIO_DATA_DIR:-${WORKSPACE_DIR}/minio-data}
MINIO_PORT=${MINIO_PORT:-9000}
MINIO_CONSOLE_PORT=${MINIO_CONSOLE_PORT:-9001}

export MINIO_ROOT_USER=${MINIO_ROOT_USER:-admin}
export MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-password}
export MINIO_API_SELECT_PARQUET=${MINIO_API_SELECT_PARQUET:-on}

echo "MinIO credentials: rootUser=${MINIO_ROOT_USER} rootPassword=${MINIO_ROOT_PASSWORD}"

mkdir -p ${MINIO_DATA_DIR}

echo "Starting MinIO server on port ${MINIO_PORT}..."
${MINIO_BIN} server ${MINIO_DATA_DIR} \
  --address ":${MINIO_PORT}" \
  --console-address ":${MINIO_CONSOLE_PORT}" &

MINIO_PID=$!
echo "MinIO started with PID: ${MINIO_PID}"

sleep 3

echo "Creating test bucket 'gpdb-ud-scratch'..."
${MC_BIN} alias set local http://localhost:${MINIO_PORT} ${MINIO_ROOT_USER} ${MINIO_ROOT_PASSWORD}
${MC_BIN} mb local/gpdb-ud-scratch --ignore-existing

export PROTOCOL=minio
export ACCESS_KEY_ID=${MINIO_ROOT_USER}
export SECRET_ACCESS_KEY=${MINIO_ROOT_PASSWORD}

echo "MinIO is ready!"
echo "  Console: http://localhost:${MINIO_CONSOLE_PORT}"
echo "  API: http://localhost:${MINIO_PORT}"
