#!/bin/sh
set -e

#DEBHELPER#

case "$1" in
    configure)
	if ! getent passwd repository >/dev/null 2>&1; then
            adduser --system repository
	fi
	if ! getent group repo >/dev/null 2>&1; then
            addgroup --system repo
	fi

	if ! getent passwd git >/dev/null 2>&1; then
	    adduser --system --home /srv/git --shell /usr/bin/git-shell git
	fi

	if ! [ -d /srv/git ]; then
	    mkdir -p /srv/git
	fi

        cat >>/etc/sudoers <<EOF
### BEGIN invirt-dev
%repo   ALL=(repository)        ALWAYS_SET_HOME,NOPASSWD: /usr/bin/reprepro
### END invirt-dev
EOF

        update-inetd --add \
            'git\tstream\ttcp\tnowait\tgit\t/usr/bin/git\tgit daemon --inetd --syslog --verbose --export-all --base-path=/srv/git /srv/git'

        echo "-----"
        echo "invirt-dev: run"
        echo "  adduser --disabled-password \$user"
        echo "  adduser \$user repo"
        echo "  adduser \$user sbuild"
        echo "to make individual users that will build packages."
        echo "-----"

	invirt-configure-git-hooks || {
            echo "-----"
            echo "invirt-dev: Could not configure git hooks."
	    echo "Run 'invirt-configure-git-hooks' once you"
	    echo "have configured your repositories appropriately."
            echo "-----"
	}
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

if [ -x /etc/init.d/invirtibuilder ]; then
    if hash invoke-rc.d 2>/dev/null; then
        invoke-rc.d invirtibuilder start
    else
        /etc/init.d/invirtibuilder start
    fi
fi