#!/bin/bash

v=2.7.3

set -euo pipefail

basedir="${0%/*}"
self="${0##*/}"

s=$(uname -s)
case "$s"
in
  Darwin) s=darwin ;;
  Linux) s=linux ;;
  *) echo "Unsupported system: $s" >/dev/stderr; exit 1 ;;
esac

m=$(uname -m)
case "$m"
in
  x86_64) m=amd64 ;;
  arm64) ;;
  *) echo "Unsupported machine: $m" >/dev/stderr; exit 1 ;;
esac

url="https://dl.influxdata.com/influxdb/releases/influxdb2-client-${v}-${s}-${m}.tar.gz"

tar="$basedir/.$self/$self-${v}.tar.gz"
bin="$basedir/.$self/$self-${v}/$self"

if [[ ! -x "$bin" ]]
then
  mkdir -p "${tar%/*}"
  wget -O "$tar" "$url"

  mkdir -p "${bin%/*}"
  tar xf "$tar" -C "${bin%/*}"
  chmod +x "$bin"
fi

exec "$bin" "${@}"