#!/usr/bin/env perl

# FIXME: Delete this after git conversion. (thomasvandoren, 2014-07-10)

$svnlogfile = "./SVNLOG";
$svnnumfile = "./.SVNLOG-revnum";
$completelog = 0;

open SVNLOGNUM, "$svnnumfile" or $completelog = 1;
open SVNLOG, "$svnlogfile" or $completelog = 1;

if ($completelog == 0) {
    my @svnlognum = <SVNLOGNUM>;
    close(SVNLOGNUM);
    close(SVNLOG);
    $revnumline = $svnlognum[0]; chomp($revnumline);
    if ($revnumline =~ m/r(\d+)$/) {
        $completelog = 0;
        $revnum = $1;
    } else {
        $completelog = 1;
    }
}

#
# if set to 1, this will only update to match the user's local version;
# currently the default is to update to match the latest version that's
# committed
#
$update_to_users_local_version = 0;

if ($update_to_users_local_version == 0) {
    $svninfoflags = "-rHEAD";
}

$revline = `svn info $svninfoflags . | grep Revision`;
chomp($revline);

if ($revline =~ m/Revision: (\d*)/) {
    $currentrevnum = $1;
} else {
    print "ERROR: couldn't determine revision number of this directory\n";
    exit(1);
}

if ($completelog == 1) {
    print "SVNLOG being recomputed from scratch\n";
    `svn log --verbose --incremental -r1:$currentrevnum > SVNLOG`;
} else {
    if ($revnum eq $currentrevnum) {
        print "SVNLOG is already up-to-date\n";
    } else {
        $revnum += 1;
        print "SVNLOG being updated from $revnum to $currentrevnum\n";
        `svn log --verbose --incremental -r$revnum:$currentrevnum >> SVNLOG`;
    }
}

open SVNLOGNUM, ">$svnnumfile" or die "Cannot open $svnnumfile for writing";
print SVNLOGNUM "r$currentrevnum\n";
close (SVNLOGNUM);
