#!/usr/bin/perl
#
# Set up sbuild so that packages for Debian unstable can be built and
# maintenance is done automatically via a daily update cronjob.
# Copyright © 2017 Michael Stapelberg <stapelberg@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, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

use strict;
use warnings;
use v5.10;
use Getopt::Long;
use Sbuild qw(help_text);

my $dist  = "debian";
my $suite = "unstable";
chomp(my $host_arch = `dpkg --print-architecture`);
my $arch = $host_arch;

my @options = (
	'distribution=s' => \$dist,
	'suite=s'        => \$suite,
	'arch=s'         => \$arch,
	'help'           => sub { help_text(1, "sbuild-debian-developer-setup") },
);
GetOptions(@options);

print "Deprecated: Note that this uses the schroot backend which is no\n";
print "longer used on the Debian buildds. Please switch to the unshare\n";
print "backend. It does not need any prior setup nor regular cron jobs.\n";
print "You can do that by running sbuild --chroot-mode=unshare.\n";
print "Don't run newgrp sbuild for the unshare backend as it will break\n";
print "the unshare calls.\n";

my $username;

if (defined($ENV{SUDO_USER}) && defined($ENV{DOAS_USER})) {
	die("Both SUDO_USER and DOAS_USER are set, this is unexpected, aborting!");
}

$username //= $ENV{SUDO_USER};
$username //= $ENV{DOAS_USER};
if (!defined($username)) {
	die "Please run sudo/doas $0";
}

system("adduser", "--", $username, "sbuild") == 0
  or die "adduser failed: $?";

sub chroot_exists {
	system("schroot -i -c chroot:$suite-$arch-sbuild >/dev/null 2>&1") == 0;
}

if (!chroot_exists()) {
	my @aliases     = ();
	my $is_unstable = $suite eq "unstable" || $suite eq "sid";
	my $othersuite  = $suite eq "unstable" ? "sid" : "unstable";
	if ($is_unstable) {
		push @aliases, "--alias=$othersuite-$arch-sbuild";
		push @aliases, "--alias=UNRELEASED-$arch-sbuild";
		if ($arch eq $host_arch) {
			push @aliases, "--alias=UNRELEASED";
			push @aliases, "--alias=$othersuite";
		}
	}
	my @cmd = (
		"sbuild-createchroot",
		"--command-prefix=eatmydata",
		"--include=eatmydata",
		@aliases,
		"--arch=$arch",
		"$suite",
		"/srv/chroot/$suite-$arch-sbuild",
		"http://localhost:3142/deb.debian.org/debian"
	);
	say "Running:\n    \$ @cmd";
	system(@cmd) == 0
	  or die "sbuild-createchroot failed: $!";
} else {
	say "chroot $suite-$arch-sbuild already exists";
}

say
"Your current user is now part of the sbuild group (no need to run sbuild-adduser) and a chroot environment exists in /srv/chroot/$suite-$arch-sbuild";

say "Now run `newgrp sbuild', or log out and log in again.";
