#!/bin/bash
set -e

USER="enterprise-search"
BIN_PATH="/app/bin"

# Set default UID:GID values
ELASTIC_UID=${ELASTIC_UID:-1000}
ELASTIC_GID=${ELASTIC_GID:-1000}

## Change UID:GID for the product's user to passed in values
usermod -u $ELASTIC_UID ${USER}
groupmod -g $ELASTIC_GID ${USER}

# Change owner UID:GID for directories required to be written to
chown -R ${USER}: /app

shopt -s extglob
# Install startup scripts
if [ -d "/app/etc/my_init.d" ]; then
    mkdir -p /etc/my_init.d
    # we copy everything except potential leftovers .new files that can be generated by the allocator
    cp -f /app/etc/my_init.d/!(*.new) /etc/my_init.d/
    chmod -R +x /etc/my_init.d/
fi
shopt -u extglob

exec ${BIN_PATH}/my_init --skip-runit -- ${BIN_PATH}/setuser ${USER} /app/enterprise_search.sh
