#!/bin/bash

message() {
  echo "CutBox Setup: ${1}"
}

WHICH_FAILED=""
WHICH_CHECK=0

which_check() {
  cross_mark="❌"
  check_mark="✓"

  if [ $# -ne 1 ]; then
    message "which_check: No arguments provided"
    exit 1
  else
    if [ -x "$(which $1)" ]; then
      printf "${check_mark}"
    else
      printf "${cross_mark}"
      WHICH_CHECK=1
      WHICH_FAILED="${WHICH_FAILED} ${1}"
    fi
  fi
}

NPM_FAILED=""
NPM_CHECK=0

npm_check() {
  cross_mark="❌"
  check_mark="✓"

  if [ $# -ne 1 ]; then
    message "npm_check: No arguments provided"
    exit 1
  else
    if [ -x "$(npm exec which $1)" ]; then
      printf "${check_mark}"
    else
      printf "${cross_mark}"
      NPM_CHECK=1
      NPM_FAILED="${NPM_FAILED} ${1}"
    fi
  fi
}


if [ -x "$(which brew)" ]; then
  message "Running brew bundle & npm install"
  brew bundle
  npm install
else
  message "Homebrew must be installed and working."
  exit 1
fi



echo "PLEASE NOTE: CutBox expects:

Build & release tooling...

- [$(which_check brew)] homebrew (in case anything is missing)
- [$(which_check pod)] cocoapods

For release...

- [$(which_check cmark)] cmark
- [$(which_check npm)] npm
- [$(npm_check appdmg)] appdmg
- [$(npm_check semver)] semver
"
echo "anything missing? (Get help at https://gitter.im/CutBox/Lobby)"

PROJECT_DIR=$(git rev-parse --show-toplevel)

echo "=========================================================================================="
echo "Install Pods"

pushd $PROJECT_DIR/CutBox || exit 1

pod install

popd ||  exit 1

echo "To build run: bin/build"

if [[ $WHICH_CHECK == 1 || $NPM_CHECK == 1 ]]; then
  echo "Something missing? Setup cannot find:
${WHICH_FAILED}
${NPM_FAILED}."
  exit 1
fi

exit 0
