#!/usr/bin/perl

use strict;
use warnings;
use Date::Parse;
my $debug = 1;

our $sourcedir = '/home/darxus/cairo/cairo';
our $logdir =  '/home/darxus/cairo/log';

chdir $sourcedir or die "Couldn't chdir $sourcedir: $!";

unless (system("git reset --hard origin/master") == 0) {
  die "Failed to git reset to master.";
}

#my @commits = `git log --pretty=format:%H`;
my @allcommits = `git log --pretty=format:%H | head -n 3585`;
my @commits = ();
for my $commit (@allcommits) {
  chomp $commit;
  push @commits, $commit;
#  #if ($commit eq 'd31dc73f9ba1facdbeee722c022d438f15e5f264') { # 10th most recent known to be passing
#  if ($commit eq 'c7ff9bb32e20679d6da4e8a2856be716e5bd9e12') { # fixed bug below
#    # "undefined reference to `cairo_boilerplate_xmalloc'"
#    # https://bugs.freedesktop.org/show_bug.cgi?id=77060
#    print "Found oldest commit that might build with gcc 4.9.\n"; # 
#    last;
#  }
}
undef @allcommits;
print scalar(@commits) . " commits.\n";

our $date = '';
our $repo = '';
our $commitdir = '';

my $firstcommit = 1;
COMMIT: while (1) {

  my $loopbegin = time;
  print "Starting at $loopbegin\n";
  my $commit = '';
#  if ($firstcommit == 1) {
#    $commit = 'master';
#    $firstcommit = 0;
#  } else {
    $commit = $commits[ rand @commits ];
    chomp $commit;
#  }
  system "git reset --hard $commit";
  system "sed -i -e 's/-flto//g' build/configure.ac.warnings";
  $date = `git log -1 --pretty=format:%ct`;
  print "$date $commit\n";
  $commitdir = "$logdir/${date}_${commit}";
  # mkdir: cannot create directory ‘/home/darxus/cairo/log/1212017278_9b1cbcde3272dac176bd9184ceb21c953c30517d’: File exists
  #system "mkdir $commitdir";
  next COMMIT unless (system("mkdir $commitdir") == 0);

  &run("git clean -xfd", '1git-clean') or next COMMIT;
  &run("./autogen.sh --enable-xcb", '1autogen') or next COMMIT;
  &run("make", '1make') or next COMMIT;
  my $testresult = &run("DISPLAY=:2 CAIRO_TEST_TARGET=xcb make test", '1make-test');
  system "touch $commitdir/1test-suite.log";
  system "cp -a test/test-suite.log $commitdir/1test-suite.log";
  system "cp -a test/cairo-test-suite.log $commitdir/1cairo-test-suite.log";
  next COMMIT unless (system("cp -a ${sourcedir}/test/output ${commitdir}/reference") == 0);

  if ($testresult) {
    # Woo, passed without new references, skip second run.
  } else {
    # Re-run tests using first run as a reference.
    #next COMMIT unless (system("cp -a ${sourcedir}/cairo/test/output ${commitdir}/reference") == 0);
    system "rm test/test-suite.log";
    system "rm test/cairo-test-suite.log";

#    &run("git clean -xfd", '2git-clean') or next COMMIT;
#    &run("./autogen.sh --enable-xcb", '2autogen') or next COMMIT;
#    &run("make", '2make') or next COMMIT;
    $testresult = &run("DISPLAY=:2 CAIRO_REF_DIR=${commitdir}/reference CAIRO_TEST_TARGET=xcb make test", '2make-test');
    system "touch $commitdir/2test-suite.log";
    system "cp -a test/test-suite.log $commitdir/2test-suite.log";
    system "cp -a test/cairo-test-suite.log $commitdir/2cairo-test-suite.log";
    system("cp -a ${sourcedir}/test/output ${commitdir}/output");
  }
  my $elapsed = (time - $loopbegin) / 60;
  print "  $elapsed minutes for this commit.\n";
#  exit; ##############################################
}

exit; ###############################################

sub run {
  my $command = shift;
  my $logfile = shift or die("specify log file as second arg to run().");
  print "  $command\n" if $debug;
  $command = "$command > $commitdir/$logfile.log 2>&1";
  unless (system($command) == 0) {
    return 0; # failed
  } else {
    return 1; # succeded
  } 
}

