#!/usr/bin/perl -w
#
# Copyright (C) 2006-2007 Roger Leigh <rleigh@debian.org>
#
# This program 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.
#
# This program 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

use strict;
use warnings;

package main;

sub usage {
	print STDERR "Usage: $0 <username>\n";
	exit 1;
}

usage() if (@ARGV < 1);

my $status = 0;

foreach (@ARGV) {
	my $user = getpwnam($_);

	if (defined $user) {
		$status += system("/usr/sbin/adduser", "$_", "sbuild");
	} else {
		print STDERR "W: User \"$_\" does not exist\n";
		$status++;
	}
}

if ($status == 0) {
	print STDOUT <<EOF;

Next, copy the example sbuildrc file to the user's home directory and
set the variables for your system:

EOF

	foreach (@ARGV) {
		my $home = (getpwnam($_))[7];
		print STDERR "  cp /usr/share/doc/sbuild/examples/example.sbuildrc $home/.sbuildrc\n";
	}
	print STDOUT <<EOF;

Now try a build:

  cd /path/to/source
  /usr/share/sbuild/chrapt <distribution> apt-get update
  /usr/share/sbuild/chrapt <distribution> apt-get upgrade
  (or "/usr/share/sbuild/chrapt <distribution> apt-get -f install"
       first if the chroot is broken)
  sbuild -d <distribution> <package>_<version>
EOF
}

exit ($status ? 1:0);

