#!/usr/bin/env bash

# Please Use Google Shell Style: https://google.github.io/styleguide/shell.xml

# ---- Start unofficial bash strict mode boilerplate
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o errexit  # always exit on error
set -o errtrace # trap errors in functions as well
set -o pipefail # don't ignore exit codes when piping output
set -o posix    # more strict failures in subshells
# set -x          # enable debugging

IFS=$'\n\t'
# ---- End unofficial bash strict mode boilerplate

version="${KUSTOMIZE_VERSION:-4.0.0}"

# Download kustomize
cd "$(mktemp -d /tmp/kustomize-XXX)"
wget -q "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv${version}/kustomize_v${version}_linux_amd64.tar.gz"
tar xfz "kustomize_v${version}_linux_amd64.tar.gz"
# Use sudo if needed, empty string if not
sudo_command=$(command -v sudo)

if [ -z "$sudo_command" ]
then
  install -m 755 kustomize /usr/local/bin/kustomize
else
  sudo install -m 755 kustomize /usr/local/bin/kustomize
fi

echo '✓' installed "$(kustomize version)"
