#!/bin/bash

# This script will install dependencies for the node failure demo:
#   1) tiup
#   2) brew
#   3) jq
#   4) gcloud

echo -e "Checking dependencies...\n\nChecking for tiup...\c"
tiup status >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo -e "  Not installed.\nInstalling tiup...\c"
	( curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sed "s/curl /curl -s /" | sh ) >/dev/null
	. ~/.profile
	( tiup update --self && tiup update cluster ) >/dev/null
	# ( ~/.tiup/bin/tiup update cluster ) >/dev/null
	echo -e "  Done\n"
else
	echo -e "  Done. TiUP already installed.\n"
fi

echo -e "Checking for Homebrew...\c"
brew list >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo -e "  Not installed.\nInstalling Homebrew...\n***NOTE: You will need to enter your MacOS Login Password!\n\n"
	/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 
	echo >> /Users/djames/.zprofile
    echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/djames/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
	echo -e "  Done\n"
else
	echo -e "  Done. Homebrew IS installed.\n"
fi

. ~/.zprofile
echo -e "Checking for jq...\c"
echo " " | jq2 >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo -e "  Not installed.\nInstalling JQ...\c"
	brew install -q jq
	echo -e "  Done\n"
else
	echo -e "  Done. jq IS installed.\n"
fi

echo -e "Checking for gcloud (Google Cloud CLI)...\c"
gcloud version >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo -e "  Not installed.\nPress ENTER to bring up the Google Cloud CLI documentation for instructions\c"
	read anykey
	open https://cloud.google.com/sdk/docs/install-sdk
else
	echo -e "  Done. gcloud IS installed.\n"
fi
