#! /bin/bash
set -e

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

green '😀 Welcome to the mobile web monorepo setup process! 😀'
red -e 'If things go badly, please ping someone in #mobile-web. 😖\n'

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 nvm) is installed… "
if brew ls --versions nvm > /dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing…'
  brew install nvm
  green 'done'
fi

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…'
  brew install yarn
  green 'done'
fi

export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"

echo -n "🤔  Checking if proper $(blue -n node) version is installed… "
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
  mkdir ~/.nvm
  green 'done'
fi

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

echo -n "🤔  Checking if $(blue -n lerna) is installed… "
if command -v lerna > /dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow -n 'installing via yarn…'
  yarn global add lerna
  green 'done'
fi

echo -n "🤔  Checking if $(blue -n localhost.m.twitch.tv) is configured… "
if grep localhost.m.twitch.tv /etc/hosts > /dev/null 2>&1; then
  green 'yes, configured'
else
  red 'not found'
  yellow 'Setting localhost.m.twitch.tv in /etc/hosts (this will require root access)'
  sudo sed -i "/127.0.0.1       localhost/c127.0.0.1       localhost, localhost.m.twitch.tv" /etc/hosts
  green 'done'
fi

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

echo -n "🤔  Checking if $(blue -n python 2.7.6) is installed… "
if pyenv versions 2>/dev/null | grep 2.7.6 > /dev/null 2>&1; then
  green 'ok'
else
  red 'not found'
  yellow -n 'installing python 2.7.6...'
  pyenv install 2.7.6 -f
  pyenv global 2.7.6
  $(pyenv which pip) install tox
  green 'done'
fi

echo -n "🤔  Checking if $(blue -n pyenv shims) are in $PATH… "
if [[ "$(which python)" = *shims* ]]; then
  green 'ok'
else
  red 'not found'
  yellow -n 'adding pyenv to $PATH in profile... '
  if [[ ${SHELL} = *zsh* ]]; then
      echo 'export PATH=$(pyenv root)/shims:$PATH' >> ~/.zprofile
  else
      echo 'export PATH=$(pyenv root)/shims:$PATH' >> ~/.profile
  fi
  export PATH=$(pyenv root)/shims:$PATH
  green 'done'
fi

sh ./scripts/setup

cd packages/mobile-web
yarn setup

green '😃 Looks like you are all set!'
