#! /usr/bin/perl

### flex just outputs a single line:

## flex version 2.5.4


### but bison is a bit more wordy

## bison (GNU Bison) 1.875c
## Written by Robert Corbett and Richard Stallman.
## 
## Copyright (C) 2003 Free Software Foundation, Inc.
## This is free software; see the source for copying conditions.  There is NO
## warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

### based on this, the version number of the program:
###   a. in the first line of output
###   b. is the last "word" of that line

$program = $ARGV[0];
$comparefunc = $ARGV[1];
$argver = $ARGV[2];
$progver1 = `$program --version`;
@progver2 = split /^/, $progver1;
$numproglines = scalar(@progver2);
chop $progver2[0];
@progver3 = split " ", $progver2[0];
$numprogwords = scalar(@progver3);
$progver = $progver3[$numprogwords-1];

@progverlist = split /\./, $progver;
$numprogverlist = scalar(@progverlist);
@argverlist = split /\./, $argver;
$numargverlist = scalar(@argverlist);

## debug ## printf "prog=%s  compare=%s argver=%s progver=%s numprogwords \n", $program, $comparefunc, $argver, $progver;


## debug ## printf "firstline = %s;  lastword= %s\n", $progver2[0], $progver;

## debug ## printf "numargverlist = %d; numprogverlist = %d\n", $numargverlist, $numprogverlist;

## debug ## foreach $ver1 (@argverlist)
## debug ## {
## debug ## 	print "argver: ", $ver1, "\n"
## debug ## }

## debug ## print "\n";

## debug ## foreach $ver1 (@progverlist)
## debug ## {
## debug ## 	print "progver: ", $ver1, "\n"
## debug ## }

if ( $numprogverlist < $numargverlist )
{
	for( $i=$numprogverlist; $i < $numargverlist; $i += 1 )
	{
		$progverlist[$i] = "0";
		$numprogverlist++;
	}
}
elsif ( $numargverlist < $numprogverlist )
{
	for( $i=$numargverlist; $i < $numprogverlist; $i += 1 )
	{
		$progverlist[$i] = "0";
		$numargverlist++;
	}
}

## both lists are now the same length, with zero version numbers filling to the right for whichever was shorter

## now do the compare

## Do a numeric comparison if both are plain numbers; otherwise, do a string comparison. Hopefully this will suffice.

for( $i=0; $i < $numargverlist; $i += 1 )
{
## debug ## 	print "i=", $i, "\n";

	###  Any non-numeric chars in the version number of either the progver or argver at this position?

	if( $progverlist[$i] =~ /[^0-9]/ || $argverlist[$i] =~ /[^0-9]/ )
	{
		if( $progverlist[$i] ne $argverlist[$i] )
		{
## debug ## 			print $progverlist[$i],  " ne ",  $argverlist[$i],  "\n";
	
			if( $progverlist[$i] lt  $argverlist[$i] )
			{
## debug ## 				print $progverlist[$i],  " lt ",  $argverlist[$i],  "\n";
				if( $comparefunc eq "=" )
				{
					print "false\n";
					exit 0;
				}
				elsif ( $comparefunc eq "<" || $comparefunc eq "<=" )
				{
					print "true\n";
					exit 0;
				}
				elsif ( $comparefunc eq ">" || $comparefunc eq ">=" )
				{
					print "false\n";
					exit 0;
				}
			}
			elsif ( $progverlist[$i] gt  $argverlist[$i] )
			{
## debug ## 				print $progverlist[$i],  " gt ",  $argverlist[$i],  "\n";
				if( $comparefunc eq "=" )
				{
					print "false\n";
					exit 0;
				}
				elsif ( $comparefunc eq "<" || $comparefunc eq "<=" )
				{
					print "false\n";
					exit 0;
				}
				elsif ( $comparefunc eq ">" || $comparefunc eq ">=" )
				{
					print "true\n";
					exit 0;
				}
			}
		}
	}
	else
	{
		if( $progverlist[$i] != $argverlist[$i] )
		{
## debug ## 			print $progverlist[$i],  " != ",  $argverlist[$i],  "\n";
	
			if( $progverlist[$i] <  $argverlist[$i] )
			{
## debug ## 				print $progverlist[$i],  " < ",  $argverlist[$i],  "\n";
				if( $comparefunc eq "=" )
				{
					print "false\n";
					exit 0;
				}
				elsif ( $comparefunc eq "<" || $comparefunc eq "<=" )
				{
					print "true\n";
					exit 0;
				}
				elsif ( $comparefunc eq ">" || $comparefunc eq ">=" )
				{
					print "false\n";
					exit 0;
				}
			}
			elsif ( $progverlist[$i] >  $argverlist[$i] )
			{
## debug ## 				print $progverlist[$i],  " > ",  $argverlist[$i],  "\n";
				if( $comparefunc eq "=" )
				{
					print "false\n";
					exit 0;
				}
				elsif ( $comparefunc eq "<" || $comparefunc eq "<=" )
				{
					print "false\n";
					exit 0;
				}
				elsif ( $comparefunc eq ">" || $comparefunc eq ">=" )
				{
					print "true\n";
					exit 0;
				}
			}
		}
	}
}
	
if( $comparefunc == "=" )
{
	print "true\n";
}
elsif ( $comparefunc == "<=" || $comparefunc == ">=" )
{
	print "true\n";
}
else
{
	print "false\n";
}

exit 0;
