#!/bin/sh

set -e

if [ "$1" = "configure" ]; then
    # Create the "registry" user
    if ! getent passwd registry > /dev/null; then
        adduser --quiet --system --group --home /var/lib/registry \
                --no-create-home --gecos "Registry daemon" \
                registry
    fi

    # Create the "registry" group, if it is missing, and set the
    # primary group of the "puppet" user to this group
    if ! getent group registry > /dev/null; then
        addgroup --quiet --system registry
        usermod -g registry registry
    fi

    # Set correct permissions and ownership for registry directories
    if ! dpkg-statoverride --list /etc/registry >/dev/null 2>&1; then
        dpkg-statoverride --update --add registry registry 0750 /etc/registry
    fi

    # Enable uwsgi application
    ln -fs /etc/uwsgi/apps-available/registry-search.yaml /etc/uwsgi/apps-enabled/

    # Restart uwsgi server to load new uwsgi module
    invoke-rc.d uwsgi restart
fi
