// Copyright (c) 2010, Jens Peter Secher <jpsecher@gmail.com>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

// Structure for holding the relevant parts of a Debian package's changelog, see
// Debian Policy section 4.4.

// TODO: Rename to someting else because it is used to hold eg, parsed hg tags.

class DebianChangeLog
{
	public var source (default,null) : String;
	public var version (default,null) : DebianPackageVersion;
	
	public function new( source : String, version : String )
	{
		this.source = source;
		this.version = new DebianPackageVersion( version );
	}

	// Extract package information from the changelog file in the current
	// directory.  Aborts the program if a changelog cannot be found.
	public static function parseCurrentChangeLog( logger : ILog ) : DebianChangeLog
	{
		// Get the changelog.
		var changeLog = Process.runButExitOnError( "dpkg-parsechangelog", [], logger );
		// Parse the changelog.
		var version = "";
		var source = "";
		var versionRegExp = ~/^Version:[ \t]+(.+)$/;
		var sourceRegExp = ~/^Source:[ \t]+(.+)$/;
		for( line in changeLog )
		{
			if( versionRegExp.match( line ) )
			{
				version = versionRegExp.matched(1);
			}
			else if( sourceRegExp.match( line ) )
			{
				source = sourceRegExp.matched(1);
			}
		}
		return new DebianChangeLog( source, version );		
	}
}
