#! /bin/sh
set -e

color() {
  tput bold
  tput setaf "$1"
  shift
  echo "$@"
  tput sgr0
}
red() {
  color 1 "$@"
}
green() {
  color 2 "$@"
}
yellow() {
  color 3 "$@"
}
blue() {
  color 4 "$@"
}

pushdir() {
  pushd "$@" >/dev/null
}
popdir() {
  popd >/dev/null
}

green '😀 Welcome to the Tachyon monorepo setup process! 😀'

unameOut="$(uname -s)"
case "${unameOut}" in
  Linux*) machine=Linux ;;
  Darwin*) machine=Darwin ;;
  CYGWIN*) machine=Cygwin ;;
  MINGW*) machine=MinGw ;;
  *) machine="UNKNOWN:${unameOut}" ;;
esac

if [ "$machine" != "Darwin" ]; then
  red '😖 This script only supports OS X currently 😖'
  blue 'You are on your own to install yarn and nvm, as well as run nvm install, yarn install'
  blue 'Dont forget to setup localhost.m.twitch.tv either.'
  exit 1
fi

red -e 'If things go badly, please ping someone in #mobile-web or #tachyon-lib-support. 😖\n'

echo -n "🤔  Checking if $(blue -n docker) is installed… "
if command -v docker >/dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  echo 'Visit https://docs.docker.com/install/ to install docker'
  exit 1
fi

echo -n "🤔  Checking if $(blue -n brew) is installed… "
if command -v brew >/dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing… '
  /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  green 'done'
fi

echo -n "🤔  Checking if $(blue -n caskroom) is installed… "
if brew info cask >/dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing… '
  brew tap caskroom/cask
  brew tap caskroom/versions
  green 'done'
fi

echo -n "🤔  Checking if $(blue -n hostess) is installed… "
if brew ls --versions hostess >/dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing… '
  brew install hostess
  green 'done'
fi

echo -n "🤔  Checking if $(blue -n /etc/hosts) is clean… "
if sudo hostess fmt >/dev/null 2>&1; then
  green 'done'
else
  red 'errors in /etc/hosts'
  exit 1
fi

echo -n "🤔  Checking if $(blue -n nvm) is installed… "
if [ -s "$HOME/.nvm" ]; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing… '
  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
  green 'done'
fi

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

echo -n "🤔  Checking if $(blue -n yarn) is installed… "
if brew ls --versions yarn >/dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing… '
  # If you are using NVM, you don't want Brew to also install a version of NPM
  brew install yarn --ignore-dependencies
  green 'done'
fi

echo -n "🤔  Checking if proper $(blue -n node) version is installed… "
echo -n "(if this fails, you might have installed nvm via brew which is not supported)"
if nvm ls | grep $(cat .nvmrc) >/dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing proper version of node… '
  nvm install
  green 'done'
fi

echo -n "🤔  Checking if $(blue -n watchman) is installed… "
if brew ls --versions watchman >/dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing… '
  brew install watchman
  green 'done'
fi

yellow '👉  Using proper version of node.'
nvm use

echo '👉 Setting up the monorepo'
yarn

echo '👉 Building all packages'
yarn build

# run tomorrow specific setup
pushdir apps/tomorrow
sh ./scripts/setup
popdir

# run starshot specific setup
pushdir apps/starshot
sh ./scripts/setup
popdir

# run valence specific setup
pushdir apps/valence
sh ./scripts/setup
popdir

# run moonbase specific setup
pushdir apps/moonbase
sh ./scripts/setup
popdir

green '😃 Looks like you are all set!'
