#!/bin/bash -e

# Use jemalloc to reduce memory fragmentation if available
JEMALLOC="/usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2"
if [ -f "$JEMALLOC" ] && [ -z "${DISABLE_JEMALLOC}" ]; then
  export LD_PRELOAD="${LD_PRELOAD:+$LD_PRELOAD:}$JEMALLOC"
else
  [ -n "${DISABLE_JEMALLOC}" ] \
    && echo "WARNING: jemalloc disabled via DISABLE_JEMALLOC" \
    || echo "WARNING: jemalloc not found at $JEMALLOC, skipping"
fi

# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
  ./bin/rails db:prepare
fi

exec "${@}"
