#!/usr/bin/env sh

set -eu

dockerversion=19.03.1

bindir=$( cd "${0%/*}" && pwd )
targetbin=$( cd "$bindir"/.. && pwd )/target/bin
dockerbin=$targetbin/.docker-$dockerversion

if [ ! -f "$dockerbin" ]; then
  filename=docker-$dockerversion.tgz
  if [ "$(uname -s)" = Darwin ]; then
    os=mac
  else
    os=linux
  fi

  url=https://download.docker.com/$os/static/stable/x86_64/$filename
  tmp=$(mktemp -d -t docker.XXX)
  mkdir -p "$targetbin"
  (
      cd "$tmp"
      "$bindir"/scurl -o ./docker.tar.gz "$url"
      tar zf ./docker.tar.gz -x docker/docker
  )
  mv "$tmp/docker/docker" "$dockerbin"
  rm -rf "$tmp"
fi

"$dockerbin" "$@"
