#!/usr/bin/env bash

# The purpose of this script is to wrap up the necessary environment for the
# per-user NGINX (PUN) processes to run under. The PUN requires the following
# software in its environment:
#
# - ruby 2.4
# - node.js 6
# - git 2.9
#
# It also assumes Passenger 5 & NGINX 1.14+ are installed at /usr/bin/passenger and /usr/sbin/nginx


# Root directory for this library
#
ROOT_DIR="$(dirname "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")")"

# Allow admin to override the environment the PUN runs in
#
NGINX_PROFILE=${NGINX_PROFILE:-/etc/ood/profile}
if [[ -f "${NGINX_PROFILE}" ]]; then
  # Source in the custom environment
  source "${NGINX_PROFILE}"
else
  # Source in the default environment
  source "${ROOT_DIR}/etc/profile"
fi

# Environment is set, so call Ruby now
#
exec \
  /usr/bin/env ruby \
    -I"${ROOT_DIR}/lib" \
    -rnginx_stage \
    -e "NginxStage::Application.start" \
    -- "${@}"
