#!/usr/bin/env bash
function curl-options(){
  curl -sL "$@"
}
## pre-configure
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
## Install
#pip install -q --user --upgrade pip awscli aws-shell cfn-flip yamllint \
#  && echo "installed user-level pip things: aws-shell awscli cfn-flip yamllint"
# I do this from pipx now, each gets a venv that doesn't overlap
mkdir ~/.local/bin #doesn't get made by `pip --user` if your python comes from brew?
pushd ~/.local/bin #now this should work
# Getting Started with Amazon EKS - Install and Configure kubectl for Amazon EKS  - https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html#get-started-kubectl
#curl-options -O "https://amazon-eks.s3-us-west-2.amazonaws.com/1.12.7/2019-03-27/bin/${OS}/amd64/aws-iam-authenticator" \
#  && chmod 700 aws-iam-authenticator \
#  && echo "installed aws-iam-authenticator"
#curl-options -O "https://amazon-eks.s3-us-west-2.amazonaws.com/1.12.7/2019-03-27/bin/${OS}/amd64/kubectl" \
#  && chmod 700 kubectl \
#  && echo "installed kubectl"
echo "install kubectl with homebrew, aws-iam-authenticator no longer necessary, replaced by aws cli"
#kubectx/kubens #os independent bash only
curl-options -O https://raw.githubusercontent.com/ahmetb/kubectx/master/kubectx \
  && chmod 700 kubectx \
  && echo "installed kubectx"
curl-options -O https://raw.githubusercontent.com/ahmetb/kubectx/master/kubens \
  && chmod 700 kubens \
  && echo "installed kubens"
#kube-ps1
#curl-options -O https://raw.githubusercontent.com/jonmosco/kube-ps1/master/kube-ps1.sh \
#  && chmod 700 kube-ps1.sh \
#  && echo "installed kube-ps1.sh, you must source it in your .bashrc: source ~/.local/bin/kube-ps1.sh"
echo 'set up https://starship.rs to put k8s cluster in your prompt'
# Kustomize install script
#OS=linux  # or darwin, or windows 
# //TODO: check for rate-limiting by GitHub API, lol.
#curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest |\
#  grep browser_download |\
#  grep -i ${OS} |\
#  cut -d '"' -f 4 |\
#  xargs curl -s -O -L
# NOPE, just doing it my way, w/ explicit versioning
#curl-options -O "https://github.com/kubernetes-sigs/kustomize/releases/download/v2.0.3/kustomize_2.0.3_${OS}_amd64"
#mv kustomize_*_${OS}_amd64 ~/.local/bin/kustomize
#chmod u+x ~/.local/bin/kustomize \
#    && echo "installed kustomize"
echo "kustomize. 'Now, built into kubectl as apply -k.'"
# jq - JSON Query - awk for JSON
# os names aren't normalized to uname output (darwin vs osx)
# binary name has os and arch embedded, remove it
case ${OS} in
  linux) curl-options -o jq "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-${OS}64" && \
    chmod u+x ~/.local/bin/jq && \
    echo "installed jq"
    ;;
  darwin) #curl-options -o jq "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64"
    echo "install jq with homebrew"
    ;;
  # does any of this actually work in windows? WSL vs other bash on windows things?
  windows) curl-options -o jq "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-win64"
   ;;
esac
## Download archive-based things to /tmp/
pushd /tmp
# Helm
# I don't use -O, since tar supports streaming
#curl-options "https://storage.googleapis.com/kubernetes-helm/helm-v2.13.1-${OS}-amd64.tar.gz" | tar xz -C /tmp
#mv /tmp/${OS}-amd64/helm ~/.local/bin/
#echo "installed helm 2"
#~/.local/bin/helm init --client-only
echo 'install helm 3 from homebrew, docs no longer have initialization for a standard repo, they point to bitnami... :('
# eksctl eksctl.io written by weaveworks
# I don't use -O, since tar supports streaming
#curl-options "https://github.com/weaveworks/eksctl/releases/download/latest_release/eksctl_${OS}_amd64.tar.gz" | tar xz -C /tmp
#mv /tmp/eksctl ~/.local/bin
#echo "installed eksctl"
echo "install eksctl from homebrew"
# JSON Incremental Digger - interactive jq-ish
# not available for arm, always amd64
curl-options -O "https://github.com/fiatjaf/jiq/releases/download/v0.7.2/jiq_${OS}_amd64"
#jiq isn't a zip
mv /tmp/jiq_${OS}_amd64 ~/.local/bin/jiq && \
chmod u+x ~/.local/bin/jiq && echo "installed jiq"
echo "jid hasnt updated since 2019, use jiq - It's jid with jq."
#kubectl 1.12+ only, install the krew kubectl plugin manager, *exactly* with their published instructions to ~/.krew/bin/
# https://krew.sigs.k8s.io/docs/user-guide/setup/install/
(
  set -x; cd "$(mktemp -d)" &&
  OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
  KREW="krew-${OS}_${ARCH}" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
  tar zxvf "${KREW}.tar.gz" &&
  ./"${KREW}" install krew
)
echo "remember to add ~/.krew/bin to your PATH and add KUBECONFIG variable in ~/.bashrc or equivalent and source it"
#end archive downloads
popd
## Config Files
if [ ! -d ~/.kube ]; then mkdir ~/.kube; fi
echo "**IF** you made the cluster with this user:"
echo "run: aws eks update-kubeconfig --name cluster_name"
echo "else"
echo 'youll have to make and add your context to the kubeconfig manually: echo "export KUBECONFIG=~/.kube/:$KUBECONFIG" >> ~/.bashrc'
## END
popd