#! /bin/bash
set -e

# Must be run from the root Tomorrow application directory

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 $@
}

function pushdir {
  pushd $@ > /dev/null
}
function popdir {
  popd > /dev/null
}

echo "😀 Setting up Tomorrow… "

echo -n "🤔  Checking if $(blue -n localhost.m.twitch.tv) is configured… "
if hostess has localhost.m.twitch.tv > /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 hostess add localhost.m.twitch.tv 127.0.0.1
  green 'done'
fi

# Required for Android Studio / Android Platform Tools
# echo -n "🤔  Checking if $(blue -n java) is installed… "
# if brew cask ls --versions java8 > /dev/null 2>&1; then
#   green 'found'
# else
#   red 'not found'
#   yellow -n 'installing… '
#   brew cask install java8
# fi

# Lets us programmatically manage Android Virtual Devices: https://developer.android.com/studio/releases/platform-tools
# echo -n "🤔  Checking if $(blue -n android-platform-tools) is installed… "
# if brew cask ls --versions android-platform-tools > /dev/null 2>&1; then
#   green 'found'
# else
#   red 'not found'
#   yellow -n 'installing… '
#   brew cask install android-platform-tools
#   green 'done'
# fi

# echo -n "🤔  Checking if $(blue -n android-platform-tools) are in \$PATH… "
# if [[ "$ANDROID_HOME" = *Android* ]]; then
#   green 'found'
# else
#   red 'not found'
#   yellow -n 'adding android-platform-tools to $PATH in profile… '
#
#   if [[ ${SHELL} = *zsh* ]]; then
#     echo -e 'export ANDROID_HOME=$HOME/Library/Android/sdk' >> ~/.zprofile
#     echo -e 'export PATH=$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$PATH' >> ~/.zprofile
#   else
#     echo -e 'export ANDROID_HOME=$HOME/Library/Android/sdk' >> ~/.bash_profile
#     echo -e 'export PATH=$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$PATH' >> ~/.bash_profile
#   fi
#
#   green 'done'
# fi

# AVDs can also be created manually using Android Studio: https://developer.android.com/studio/releases/platform-tools
# See: https://developer.android.com/studio/run/managing-avds
# echo -n "🤔  Checking if $(blue -n Android Virtual Device) exists... "
# if avdmanager list avd | grep 'MWEB_ANDROID_8.0' > /dev/null 2>&1; then
#   green 'found'
# else
#   red 'not found'
#   yellow -n 'installing… '
#   avdmanager create avd --name MWEB_ANDROID_8.0 --device "pixel_xl" --package 'system-images;android-26;google_apis;x86'
#   green 'done'
# fi

# This helps get the initial cold start of the AVD out of the way. It appears to help ensure that our
# Android emulation commands execute as expected without timing out.
# echo -n "🤔  Checking if $(blue -n Android Virtual Device) can boot... "
# if emulator -avd MWEB_ANDROID_8.0 -writable-system -no-snapshot -no-boot-anim > /dev/null 2>&1 & then
#   green 'success'
# else
#   red 'something went wrong'
# fi

echo -n "🤔  Checking if $(blue -n tomorrow cert) is in keychain… "
if security find-certificate -c tv.twitch.m.localhost /Library/Keychains/System.keychain > /dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow 'creating… '
  pushdir proxy
  sh addCert
  popdir
  green 'done'
fi

echo -n "🤔  Checking if $(blue -n proxy container image) is built… "
if docker image inspect tomorrow-proxy > /dev/null 2>&1; then
  green 'found'
else
  red 'not found'
  yellow 'creating… '
  yarn proxy:build
  green 'done'
fi
