#!/usr/bin/perl

use warnings;
use strict;

use Cwd qw(getcwd);
use File::Slurp;
use File::stat;
use Test::More;

chdir($ENV{AUTOPKGTEST_TMP}) or die "chdir: $!\n";

# This assumes that ikiwiki-hosting.conf still has prefix_e=example.com.
ok(! system(qw(ikisite create foo.example.com --admin=root@localhost)));
ok(-d '/home/e-foo');
ok(-f '/home/e-foo/ikiwiki.setup');
ok(-f '/home/e-foo/public_html/ikiwiki/index.html');

my $uid = `id -u e-foo`;
chomp $uid;
my $stat = stat('/home/e-foo');
is($stat->uid, $uid);

ok(! system('getent passwd "$(id -nu)" > passwd'));
ok(! system('getent group "$(id -ng)" > group'));
write_file('hosts', <<EOF);
127.0.0.1 foo.example.com
EOF

$ENV{NSS_WRAPPER_PASSWD} = getcwd.'/passwd';
$ENV{NSS_WRAPPER_GROUP} = getcwd.'/group';
$ENV{NSS_WRAPPER_HOSTS} = getcwd.'/hosts';

SKIP: {
	# This only tests systemd, because we can't easily tell the
	# difference between sysv-rc and a chroot/container with no init
	# system at all
	skip('init system not necessarily running', 5) unless -d '/run/systemd/system';

	ok(! system(qw(ikisite changesetup --enable-plugin=branchable --set branchable=1 foo.example.com)));
	ok(! system(qw(systemctl status ikiwiki-hosting-web.service)));

	{
		local $ENV{LD_PRELOAD} = 'libnss_wrapper.so';
		ok(! system(qw(git clone git://foo.example.com/source.git)));
	}

	ok(! system('find source -name .git -prune -o -print >&2'));
	ok(-f 'source/.gitignore')
};

ok(! system(qw(ikisite delete foo.example.com)));
ok(! -e '/home/e-foo');

done_testing;
