#!/bin/bash
set -e

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

## Change UID:GID for `elastic` user to passed in values
usermod -u $ELASTIC_UID elastic
groupmod -g $ELASTIC_GID elastic

# Change owner UID:GID for directories required to be written to
chown -R elastic: /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 /sbin/my_init --skip-runit -- setuser elastic /app/apm.sh
