#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# shellcheck source=start-utils
. "${SCRIPTS:-/}start-utils"
isDebugging && set -x

sharedArgs=(--version="$VERSION")
if isFalse "${ONLINE_MODE:-true}"; then
  sharedArgs+=( --offline )
fi

if [[ -v OPS_FILE ]]; then
  mc-image-helper manage-users \
    "${sharedArgs[@]}" \
    --type=JAVA_OPS \
    --input-is-file \
    "$OPS_FILE"
fi
if [[ -v OPS ]]; then
  args=()
  if isTrue "${APPEND_OPS:-false}" || isFalse "${OVERRIDE_OPS:-true}"; then
    args+=(--append-only)
  fi
  # shellcheck disable=SC2086
  mc-image-helper manage-users \
    "${sharedArgs[@]}" "${args[@]}" \
    --type=JAVA_OPS \
    $OPS
fi

if [[ -v WHITELIST_FILE ]]; then
  mc-image-helper manage-users \
    "${sharedArgs[@]}" \
    --type=JAVA_WHITELIST \
    --input-is-file \
    "$WHITELIST_FILE"
fi
if [[ -v WHITELIST ]]; then
  args=()
  if isTrue "${APPEND_WHITELIST:-false}" || isFalse "${OVERRIDE_WHITELIST:-true}"; then
    args+=(--append-only)
  fi
  # shellcheck disable=SC2086
  mc-image-helper manage-users \
    "${sharedArgs[@]}" "${args[@]}" \
    --type=JAVA_WHITELIST \
    $WHITELIST
fi

exec "${SCRIPTS:-/}start-finalExec" "$@"
