#!/bin/sh
set -e
cd "$(dirname "$0")/.."

if [ "$(uname -s)" = "Darwin" ] && command -v brew >/dev/null 2>&1; then
  echo "==> Installing Homebrew dependencies…"
  brew update >/dev/null
  brew bundle check &>/dev/null || brew bundle

  brew bootstrap-rbenv-ruby
  brew bootstrap-nodenv-node
fi

if ! command -v bundle >/dev/null 2>&1; then
  echo "Bundler required but not found: gem install bundler" >&2
  exit 1
fi

echo "==> Installing gem dependencies…"
if [ "$RACK_ENV" = "production" ]; then
  export NOKOGIRI_USE_SYSTEM_LIBRARIES=1
  export BUNDLE_JOBS=4
  bundle install --without development staging test benchmark --path vendor/cache --local --deployment
else
  bundle check --path vendor/cache 2>&1 || \
    bundle install --path vendor/cache
fi

echo "==> Installing npm dependencies…"
if [ "$RACK_ENV" = "production" ]; then
  npm install --production
else
  npm install
fi
