#!/bin/sh
# Copyright © 2005-2008  Roger Leigh <rleigh@debian.org>
#
# sbuild is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# sbuild is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#####################################################################

set -e

if [ -f "$CHROOT_SCRIPT_CONFIG" ]; then
    . "$CHROOT_SCRIPT_CONFIG"
elif [ "$2" = "ok" ]; then
    echo "script-config file '$CHROOT_SCRIPT_CONFIG' does not exist"
    exit 1
fi

if [ $1 = "setup-start" ] && [ "$SBUILD" = "true" ]; then

    # If the session user is a member of the sbuild group, make sure
    # that the chroot environment has the sbuild build directory
    # exists, and has the correct ownership permissions.

    if getent group $(id -G "${AUTH_USER}") | \
	sed -e 's/^\([^:]*\):.*$/\1/' | \
	grep -q '^sbuild$'; then

	if [ ! -d "${CHROOT_PATH}/home/${AUTH_USER}" ]; then
	    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
		echo "Creating user home directory: ${CHROOT_PATH}/home/${AUTH_USER}"
	    fi
	    mkdir -p "${CHROOT_PATH}/home/${AUTH_USER}"

	    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
		echo "Setting ownership of ${CHROOT_PATH}/home/${AUTH_USER} to ${AUTH_USER}:sbuild"
	    fi
	    chown "${AUTH_USER}:sbuild" "${CHROOT_PATH}/home/${AUTH_USER}"
	fi

	if [ ! -d "${CHROOT_PATH}/build" ]; then
	    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
		echo "Creating sbuild build directory: ${CHROOT_PATH}/build"
	    fi
	    mkdir "${CHROOT_PATH}/build"
	fi
	chown root:sbuild "${CHROOT_PATH}/build"
	chmod 0775 "${CHROOT_PATH}/build"

	if [ ! -d "${CHROOT_PATH}/build/${AUTH_USER}" ]; then
	    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
		echo "Creating sbuild build directory: ${CHROOT_PATH}/build/${AUTH_USER}"
	    fi
	    mkdir "${CHROOT_PATH}/build/${AUTH_USER}"
	fi

	if [ "$AUTH_VERBOSITY" = "verbose" ]; then
	    echo "Setting ownership of ${CHROOT_PATH}/build/${AUTH_USER} to ${AUTH_USER}:sbuild"
	fi
	chown "${AUTH_USER}:sbuild" "${CHROOT_PATH}/build/${AUTH_USER}"

	if [ "$AUTH_VERBOSITY" = "verbose" ]; then
	    echo "Setting permissions of ${CHROOT_PATH}/build/${AUTH_USER} to 770"
	fi
	chmod 0770 "${CHROOT_PATH}/build/${AUTH_USER}"

	if [ ! -d "${CHROOT_PATH}/var/lib/sbuild/srcdep-lock" ]; then
	    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
		echo "Creating srcdep-lock directory: ${CHROOT_PATH}/var/lib/sbuild/srcdep-lock"
	    fi
	    mkdir -p "${CHROOT_PATH}/var/lib/sbuild/srcdep-lock"
	fi

	if [ "$AUTH_VERBOSITY" = "verbose" ]; then
	    echo "Setting ownership of ${CHROOT_PATH}/var/lib/sbuild to root:sbuild"
	fi
	chown -R root:sbuild "${CHROOT_PATH}/var/lib/sbuild"

	if [ "$AUTH_VERBOSITY" = "verbose" ]; then
	    echo "Setting permissions of ${CHROOT_PATH}/var/lib/sbuild to 02775"
	fi
	chmod -R 02775 "${CHROOT_PATH}/var/lib/sbuild"

    else

	if [ "$AUTH_VERBOSITY" = "verbose" ]; then
	    echo "User ${AUTH_USER} is not in group sbuild; skipping sbuild setup"
	fi

    fi

fi
