#!/bin/bash

function is_puma_installed() {
  [ ! -f Gemfile.lock ] && return 1
  grep ' puma ' Gemfile.lock >/dev/null
}

set -e

function check_number() {
  if [[ ! "$2" =~ ^[0-9]+$ ]]; then
    echo "$1 needs to be a non-negative number"
    exit 1
  fi
}
check_number PUMA_WORKERS     "${PUMA_WORKERS:-0}"
check_number PUMA_MIN_THREADS "${PUMA_MIN_THREADS:-0}"
check_number PUMA_MAX_THREADS "${PUMA_MAX_THREADS:-0}"

export RACK_ENV=${RACK_ENV:-"production"}

if is_puma_installed; then
  export_vars=$(cgroup-limits) ; export $export_vars

  exec bundle exec "puma --config ../etc/puma.cfg"
else
  echo "You might consider adding 'puma' into your Gemfile."

  if bundle exec rackup -h &>/dev/null; then
    if [ -f Gemfile ]; then
      exec bundle exec "rackup -E ${RAILS_ENV:-$RACK_ENV} -P /tmp/rack.pid --host 0.0.0.0 --port 8080"
    else
      exec rackup -E "${RAILS_ENV:-$RACK_ENV}" -P /tmp/rack.pid --host 0.0.0.0 --port 8080
    fi
  else
    echo "ERROR: Rubygem Rack is not installed in the present image."
    echo "       Add rack to your Gemfile in order to start the web server."
  fi
fi