#!/bin/sh -e

USER=$1
CHROOT=$2
DEFAULT_CHROOT=/usr/local/chroot/unstable

usage() {
	cat << EOF
Usage: fixchroot user [chroot_full_path]
  - in the chroot [default $DEFAULT_CHROOT]:
     - create a build directory for user
     - create the srcdep-lock directory

  NOTE: this only writes to the chroot.  You will have to
     first add the user to sbuild group and sudoers.  See
     README.Debian: I. modifications in the base install
EOF
	exit 0
}

if [ "$#" -lt 1 ]; then usage; fi

if ! id -u "$USER" >/dev/null 2>&1; then
	echo ..the user \"$USER\" doesn\'t appear to exist
	exit 1
fi

if [ -z $CHROOT ]; then CHROOT=$DEFAULT_CHROOT;fi

if [ ! -d $CHROOT ]; then
	echo "..the chroot root directory \"$CHROOT\" doesn't exist"
	exit 1
fi

echo "..creating $USER's build directory in $CHROOT"
mkdir -p $CHROOT/build/$USER
chown $USER $CHROOT/build/$USER
chgrp -R sbuild $CHROOT/build
chmod -R 775 $CHROOT/build

echo "..creating lock directory in $CHROOT"
# add a lockdir in the chroot for sbuild
mkdir -p $CHROOT/var/lib/sbuild/srcdep-lock
chgrp -R sbuild $CHROOT/var/lib/sbuild
chmod -R 775 $CHROOT/var/lib/sbuild

echo "..copying some /etc/ files to $CHROOT"
cd $CHROOT
cp \
	/etc/group \
	/etc/host.conf \
	/etc/hostname \
	/etc/hosts \
	/etc/hosts.allow \
	/etc/hosts.deny \
	/etc/mailname \
	/etc/nsswitch.conf \
	/etc/passwd \
	/etc/resolv.conf \
	/etc/services \
	/etc/shadow \
	/etc/sudoers \
	/etc/timezone \
	etc/
