#!/usr/bin/env perl

chomp(my $dirty = `git status --untracked --porcelain share`);
!length($dirty) and die 'Cannot proceed: share/ is not dirty. Run "git submodule update --remote"';

chomp($dirty = `git status --untracked --porcelain Changes`);
length($dirty) and die 'Cannot proceed: Changes is already dirty';

use Path::Tiny;
chomp(my $str = `git submodule status`);
my ($sha) = split(' ', $str, 2);
$sha =~ s/^\+//;

my ($seen_next, $seen_blank);
path('Changes')->edit_lines_utf8(sub {
  if ($seen_next ||= /^\{\{\$NEXT\}\}/ and not $seen_blank and /^$/) {
    s/^$/        - updated test suite to commit $sha\n/;
    $seen_blank = 1;
  }
});

exec 'git commit -m"update test suite to latest commit" Changes share';
