#!/usr/bin/perl

$location=$ENV{SVNGEN_URL};
$revision=$ENV{SVNGEN_REVISION};

#!/usr/bin/perl
use List::Util qw(max);

$branch='';
$url='';
$verbose='';
$out='';

$clock_only = '';
$clock_format = '';
$clock_zone = 'local';
$revision_only = '';
$date_only = '';
$url_only = '';

###
### Set LC_TIME and LC_MESSAGES to values which cause the "date" and "svn info" commands
### to speak English. There used to be a problem (with some AIPS++/CASA scripts) where
### LC_TIME=Japanese was causing a runtime crash...
###
$ENV{'LC_TIME'} = 'en_US.UTF-8';
$ENV{'LC_MESSAGES'} = 'en_US';

foreach ( @ARGV ) {
    m|^branch=(.+)| && ( $branch = $1, next );
    m|^url=(.+)| && ( $url = $1, next );
    m|^out=(.+)| && ( $out = $1, next );
	m/^(?:-t|t=(.+))$/ && ( $clock_only = 'yes', $clock_format=$1, next );
	m/^(?:-u|u=(.+))$/ && ( $clock_only = 'yes', $clock_format=$1, $clock_zone='utc', next );
	m|^-revision$| && ( $revision_only = 'yes', next );
	m|^-date$| && ( $date_only = 'yes', next );
	m|^-url$| && ( $url_only = 'yes', next );
	m|^-v$| && ( $verbose = 'yes', next );
    m|^-help$| && ( usage( ), exit );
	usage("unrecognized argument: $_");
}

###
### open output file...
###
$fh = *STDOUT;
if ( $out ) {
	open( $fh, ">> $out" ) or die "cannot open $out for writing";
}

if ( $clock_only ) {
	my $flag = '';
	if ( $clock_zone eq 'utc' ) {
		$flag = '-u';
	}
	open( OUT, "date $flag " . ($clock_format ? "'$clock_format'" : "'+%Y/%m/%d %H:%M:%S'") . " |" ) or die $!;
	$out = <OUT>;
	close( OUT );
	print $fh $out;
} else {

	unless ( $location ) { 
		if ( $url ) {
			$location = $url;
			$revision = '';
		} elsif ( $branch ) {
			$location = find_url($branch);
			$revision = '';
		} else {
			die usage("could not find a subversion URL/path");
		}
	}

	#build_time = "Sat 2014/08/23 07:50:42 UTC"
	chomp($build_date = `date -u "+%a %Y/%m/%d %H:%M:%S UTC"`);

	unless ( $location ) { die "no location available" }

	open( INFO, "echo t | svn info $location |" ) or die "could not get subversion information";

	$subversion_url='';
	$subversion_revision='';
	$subversion_date='';
	foreach ( <INFO> ) {
		m|URL: (\S+)| && ( $subversion_url = $1, next );
		m|Last Changed Rev: (\S+)| && ( $subversion_revision=$1, next );
		m|Last Changed Date:\s+(.+)| && ( $subversion_date=$1, next );
	}
	close( INFO );

	unless( $subversion_url && $subversion_revision && $subversion_date ) { die "incomplete subversion information" }
	output( $fh );
}

###
### open output file...
###
if ( $out ) {
	close( $fh );
}

exit 0;

sub output {
	my $fh = shift(@_);
	my $rev = ($revision ? $revision : $subversion_revision);
	my $sdate = $subversion_date;
	my $url = $subversion_url;
	if ( $revision_only ) {
		print $fh "$rev\n";
	} elsif ( $date_only ) {
		print $fh "$sdate\n";
	} elsif ( $url_only ) {
		print $fh "$url\n";
	} else {
		$rev =~ s|"|\\"|g;
		print $fh "subversion_revision = \"$rev\"\n";
		$sdate =~ s|"|\\"|g;
		print $fh "subversion_date = \"$sdate\"\n";
		$url =~ s|"|\\"|g;
		print $fh "subversion_url = \"$url\"\n";
		my $bdate = $build_date;
		$bdate =~ s|"|\\"|g;
		print $fh "build_time = \"$bdate\"\n";
	}
}

sub usage {
    my $error = shift(@_);
    print "usage:\n\tgenerate-svninfo [-help] [-v] [url=<URL-OR-PATH>] [branch=<BRANCH-LABEL>] [out=<OUTPUT-FILE>]\n";
    print "\t\t-v\t\tverbose\n";
    print "\t\t-help\t\tdisplay this message and exit\n";
    die $error if $error;
}

sub find_url {
	my $branch = shift(@_);
	my $result='';
	my $current = '';
	my %branches = ( );
	open( my $branches, "echo t | svn info --depth immediates https://svn.cv.nrao.edu/svn/casa/branches 2> /dev/null |" );
	foreach ( <$branches> ) {
		if ( m|Path:\s+$branch-(\S+)| ||
		     m|Path:\s+$branch$| && $branch =~ m|((?:[0-9]+_)+[0-9]+)| ) {
			my $suffix = $1;
			next unless $suffix =~ m|_|;
			if ( $verbose ) { print STDERR "found $branch-$suffix\n" }
			my @ver = split('_',$suffix);
			my $place = 1;
			$current = 0;
			foreach $val ( reverse @ver ) {
				$current += $val * $place;
				$place *= 10;
			}
			if ( $verbose ) { print STDERR "\tcalculated numeric value of $current\n" }
		} elsif ( m|URL: (\S+)| && $current ) {
			if ( $verbose ) { print STDERR "\tfound URL $1 for $current\n" }
			$branches{$current} = $1;
			$current = 0;
		}
	}
	close($branches);

	my $version = max(keys %branches);
	my $url = $branches{$version};
	unless( $url ) { die "could not find URL" }
	return $url;
}
