#!/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 ondemand
# Software Collection.

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

# Source in the default environment
# shellcheck source=/etc/profile
source "${ROOT_DIR}/etc/profile"

# 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
  # shellcheck source=nginx_stage/etc/profile
  source "${NGINX_PROFILE}"
fi

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