#!/bin/sh
set -eu

RESOURCE_DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
BIN="${RESOURCE_DIR}/bin/monarc-local"
COMPOSE_DIR="${RESOURCE_DIR}/compose"
MONARC_HOME_DIR="${MONARC_HOME_DIR:-${HOME}/.monarc-local}"
USER_COMPOSE_DIR="${MONARC_HOME_DIR}/compose"
USER_ENV_FILE="${USER_COMPOSE_DIR}/.env"

export PATH="/opt/homebrew/bin:/usr/local/bin:/Applications/Docker.app/Contents/Resources/bin:/usr/bin:/bin:/usr/sbin:/sbin:${PATH:-}"

mkdir -p "${USER_COMPOSE_DIR}"
if [ ! -f "${USER_ENV_FILE}" ] && [ -f "${COMPOSE_DIR}/.env.example" ]; then
  cp "${COMPOSE_DIR}/.env.example" "${USER_ENV_FILE}"
fi

if [ -z "${MONARC_COMPOSE_FILE:-}" ]; then
  export MONARC_COMPOSE_FILE="${COMPOSE_DIR}/docker-compose.release.yml"
fi

if [ -z "${MONARC_ENV_FILE:-}" ]; then
  export MONARC_ENV_FILE="${USER_ENV_FILE}"
fi

if [ "$#" -eq 0 ]; then
  set -- help
fi

exec "${BIN}" "$@"
