#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASE=$DIR/..
DOCKER_MACHINE_CLI_VERSION=$(node -pe "JSON.parse(process.argv[1])['docker-machine-version']" "$(cat $BASE/package.json)")
DOCKER_MACHINE_CLI_FILE=docker-machine-$DOCKER_MACHINE_CLI_VERSION
DOCKER_CLI_VERSION=$(node -pe "JSON.parse(process.argv[1])['docker-version']" "$(cat $BASE/package.json)")
DOCKER_CLI_FILE=docker-$DOCKER_CLI_VERSION

pushd $BASE/resources > /dev/null

if [ ! -f $DOCKER_MACHINE_CLI_FILE ]; then
  echo "-----> Downloading Docker Machine CLI..."
  rm -rf docker-machine*
  curl -L -o $DOCKER_MACHINE_CLI_FILE https://github.com/docker/machine/releases/download/v$DOCKER_MACHINE_CLI_VERSION/docker-machine_darwin-amd64
  chmod +x $DOCKER_MACHINE_CLI_FILE
fi

if [ ! -f $DOCKER_CLI_FILE ]; then
  echo "-----> Downloading Docker CLI..."
  rm -rf docker-*
  curl -L -o docker-$DOCKER_CLI_VERSION.tgz https://get.docker.com/builds/Darwin/x86_64/docker-$DOCKER_CLI_VERSION.tgz
  tar xvzf docker-$DOCKER_CLI_VERSION.tgz --strip=3
  rm docker-$DOCKER_CLI_VERSION.tgz
  mv docker docker-$DOCKER_CLI_VERSION
  chmod +x $DOCKER_CLI_FILE
fi

popd > /dev/null
