#!/bin/sh

set -e

if [ "$1" = "configure" ]; then
    if ! getent group _defectdojo >/dev/null; then
        addgroup --quiet --force-badname --system _defectdojo
    fi

    if ! getent passwd _defectdojo >/dev/null; then
        adduser --quiet --force-badname --system --ingroup _defectdojo --home /var/log/defectdojo --no-create-home _defectdojo
    fi
    if ! dpkg-statoverride --list /var/log/defectdojo >/dev/null 2>&1; then
        dpkg-statoverride --update --add _defectdojo _defectdojo 0755 /var/log/defectdojo
    fi
    if ! dpkg-statoverride --list /var/lib/defectdojo >/dev/null 2>&1; then
        dpkg-statoverride --update --add _defectdojo _defectdojo 0755 /var/lib/defectdojo
    fi
    if ! dpkg-statoverride --list /etc/defectdojo >/dev/null 2>&1; then
        dpkg-statoverride --update --add _defectdojo _defectdojo 0755 /etc/defectdojo
    fi
    if ! dpkg-statoverride --list /etc/defectdojo/settings.dist.py >/dev/null 2>&1; then
        dpkg-statoverride --update --add _defectdojo _defectdojo 660 /etc/defectdojo/settings.dist.py
    fi
    if [ -n "$2" ]; then
	cd /usr/lib/defectdojo && python3 manage.py collectstatic --noinput
    fi
fi

if [ -e /etc/redis/redis-defectdojo.conf ]; then
    chown redis:redis /etc/redis/redis-defectdojo.conf
fi

# we need to have access to the redis
usermod -aG redis _defectdojo

#DEBHELPER#

exit 0

