#!/usr/pkg/bin/perl
#####
# NAME
#	compare-version
# VERSION
#	$Id$
# CHANGELOG
#	$Log$
# USAGE
#	compare-version ver1 ver2
# DESCRIPTION
#

main:{
	local($ver1, $ver2) ;
	local($micro1, $minor1, $major1) ;
	local($micro2, $minor2, $major2) ;

	# get arguments
	if(($ver1 = shift(@ARGV)) eq ''){
		&error("ver1 argument is not given") ;
	}
	if(($ver2 = shift(@ARGV)) eq ''){
		&error("ver2 argument is not given") ;
	}
	#print "ver1: $ver1, ver2: $ver2\n" ;

	# get version numbers
	$major1 = &getMajor($ver1) ;
	$minor1 = &getMinor($ver1) ;
	$micro1 = &getMicro($ver1) ;
	$major2 = &getMajor($ver2) ;
	$minor2 = &getMinor($ver2) ;
	$micro2 = &getMicro($ver2) ;
	#print "ver1: major $major1, minor $minor1. micro: $micro1\n" ;
	#print "ver2: major $major2, minor $minor2. micro: $micro2\n" ;

	# compare major
	if($major1 < $major2){ exit 1 ; } 
	if($major1 > $major2){ exit 0 ; }
	if($minor1 < $minor2){ exit 1 ; } 
	if($minor1 > $minor2){ exit 0 ; }
	if($micro1 < $micro2){ exit 1 ; } 
	if($micro1 > $micro2){ exit 0 ; }

	exit 0 ;
}

sub error {
	local($message) = @_ ;
	print STDERR "compare-version [ERROR] $message\n" ;
	print STDERR "usage: compare-version ver1 ver2\n" ;
	exit 1 ;
}

sub warning {
	local($message) = @_ ;
	print STDERR "compare-version [WARNING] $message\n" ;
}

sub getMajor {
	local($ver) = @_ ;
	local($major) ;

	$major = '' ;
	if($ver =~ /^([0-9]+)/){
		$major = $1 ;
	} else {
		&error("major number is not found in \"$ver\"") ;
	}
	return $major ;
}

sub getMinor {
	local($ver) = @_ ;
	local($minor) ;

	$minor = '' ;
	if($ver =~ /^[0-9]+\.([0-9]+)/){
		$minor = $1 ;
	} else {
		&warning("minor number is not found in \"$ver\"") ;
		$minor = 0 ;
	}
	return $minor ;
}

sub getMicro {
	local($ver) = @_ ;
	local($micro) ;

	$micro = '' ;
	if($ver =~ /^[0-9]+\.[0-9]+\.([0-9]+)/){
		$micro = $1 ;
	} else {
		&warning("micro number is not found in \"$ver\"") ;
		$micro = 0 ;
	}
	return $micro ;
}

