#!/usr/bin/env sh
set -eu
base_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
distribution=3.9.9
user_cache=${MAVEN_USER_HOME:-${HOME:-/tmp}/.m2}
install_dir="$user_cache/wrapper/dists/apache-maven-$distribution"
maven="$install_dir/apache-maven-$distribution/bin/mvn"
if [ ! -x "$maven" ]; then
  mkdir -p "$install_dir"
  archive="$install_dir/maven.zip"
  if command -v curl >/dev/null 2>&1; then
    curl --fail --location --silent --show-error "https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/$distribution/apache-maven-$distribution-bin.zip" -o "$archive"
  elif command -v wget >/dev/null 2>&1; then
    wget -q "https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/$distribution/apache-maven-$distribution-bin.zip" -O "$archive"
  else
    echo 'curl or wget is required to bootstrap pinned Maven 3.9.9.' >&2
    exit 1
  fi
  expected=8beac8d11ef208f1e2a8df0682b9448a9a363d2ad13ca74af43705549e72e74c9378823bf689287801cbbfc2f6ea9596201d19ccacfdfb682ee8a2ff4c4418ba
  if command -v sha512sum >/dev/null 2>&1; then actual=$(sha512sum "$archive" | cut -d ' ' -f 1); elif command -v shasum >/dev/null 2>&1; then actual=$(shasum -a 512 "$archive" | cut -d ' ' -f 1); else echo 'sha512sum or shasum is required to verify Maven.' >&2; exit 1; fi
  [ "$actual" = "$expected" ] || { echo 'Maven distribution checksum verification failed.' >&2; exit 1; }
  unzip -q -o "$archive" -d "$install_dir"
  rm -f "$archive"
fi
exec "$maven" -f "$base_dir/pom.xml" "$@"
