#!/bin/sh
set -e

export PATH=${PATH}:/usr/sbin:/sbin

# first arg is `-f` or `--some-option`
# or first arg is `something.conf`
if [ "${1#-}" != "$1" ] || [ "${1%.conf}" != "$1" ]; then
    set -- redis-server "$@"
fi

# change to redis user to run
if [ "$1" = 'redis-server' -a "$(id -u)" = '0' ]; then
    chown redis:redis /var/lib/redis
    chown redis:redis /data
    exec su redis -s /bin/sh -- "$0" "$@"
fi

if [ -n "${REDIS_PASSWORD}" ]; then
   set -- "$@" --requirepass "${REDIS_PASSWORD}"
fi

exec "$@"
