#!/usr/bin/env bash
# shellcheck disable=SC2188

<<COMMENT
As oart of onboarding, make a pretty valiant attempt to install all dependencies idempotently

Called By: Humans on their personal laptops
COMMENT

set -euo pipefail

echo "Watch the output carefully, this script tries to be polite about existing configurations and will fail linking."
echo "You will also need to do some manual configuration of Docker Desktop"
echo

echo "Installing AWS"
# jq is used to parse some output in our aws outputs
brew install awscli jq

if [ ! -f "$HOME"/.aws/credentials ]; then
	aws configure
else
  echo 'Previous AWS creds detected.  Please run `aws configure --profile <something>` in order to setup your IAM credentials if you have not already'
fi
echo "AWS installed"

echo "Installing NodeJS dependencies"
NODE_VERSION=20
PNPM_VERSION=8
if ! which node; then
	brew install node@${NODE_VERSION?}
	brew link node@${NODE_VERSION?}
else
	MAJOR_VERSION=$(node --version | cut -d . -f 1)
	if [[ "${MAJOR_VERSION?}" != "v${NODE_VERSION?}" ]]; then
		# nvm is a possibility so let's respect that
		brew install --overwrite node@${NODE_VERSION?} || \
			nvm use node@${NODE_VERSION} || \
			brew link --overwrite node@${NODE_VERSION?}
	fi
fi

npm install -g npm@10
npm install -g pnpm@${PNPM_VERSION}
echo "Done installing NodeJS dependencies"

echo "Installing Docker"
set +e
brew install --cask Docker
set -e
echo "Docker installed"

echo "Installing Postgres 14"
brew install postgresql@14
brew link postgresql@14
echo "Postgres 14 installed"

KUBERNETES_VERSION=1.22
echo "Installing provisioning tools"
brew tap weaveworks/tap
brew install weaveworks/tap/eksctl \
	terraform tflint helm helmfile kubectl@${KUBERNETES_VERSION?} pulumi
# Required for keg-only casks
brew link --overwrite kubectl@${KUBERNETES_VERSION?}
echo "Done installing provisioning tools"

echo "Installing Linear (issue tracking tool)"
set +e
brew install linear-linear
set -e
echo "Done installing Linear"

echo "Installing localstack"
python3 -m pip install localstack
python3 -m pip install awscli-local
echo "Done installing localstack"

echo "Installing everything else we sometimes need"
# Coreutils is GNU utilities as g<command>
# Specifically, we use this for (g)timeout
brew install coreutils fzf gh make
echo "All dependencies installed!  Enjoy your new laptop."
