#!/usr/bin/awk -f

#
# This script can be used to convert the dtrace.spec into a Debian changelog
# file.  It is intended to be invoked from the 'prep-debian' make target, but
# it can be used manually as well:
#	mkChangelog debian/control dtrace.spec > debian/changelog
#

function error(msg) {
	print "E:"msg >"/dev/stderr";
	err = 1;
	exit(1);
}

BEGIN {
	if (!dist)
		dist = "UNRELEASED";
	else if (dist != "unstable" && dist != "experimental")
		error("Bad dist ("dist")");
}

/^Maintainer:/ {
	sub(/^Maintainer: +/, "");
	m_name = $0;
	i = index(m_name, " <");
	m_mail = substr(m_name, i + 1);
	m_name = substr(m_name, 1, i - 1);

	nextfile;
}

/^Name:/ {
	pkg = $2;
	next;
}

/^%changelog/ {
	in_changelog = 1;
	next;
}

!in_changelog {
	next;
}

function lineWrap(p, s, line, len, l, n, m, i) {
	n = length(p);
	line = p;
	len = n;

	m = split(s, arr, / +/);
	for (i = 1; i <= m; i++) {
		l = length(arr[i]);
		if (len + 1 + l >= 80) {
			print line;
			line = sprintf("%*s %s", n, "", arr[i]);
			len = n + 1 + l;
		} else {
			line = line " " arr[i];
			len += 1 + l;
		}
	}
	delete arr;

	print line;
}

function emitEntry(i, j, n) {
	# Loop through the contributions, determining who each contribution
	# should be credited to.  If there are multiple contributors, we pick
	# the first one who is not the commit author (if any).
	# If all contributions are from the commit author, we do not emit an
	# explicit credit line.
	for (i = 1; i <= lc; i++) {
		gsub(/ \[Orabug[:;] [1-9][0-9]*(, [1-9][0-9]*)*\]/, "", lv[i]);
		sub(/[ \t]+$/, "", lv[i]);
		gsub(/  +/, " ", lv[i]);

		cont = name;
		if (match(lv[i], /\([^)]+\)$/) > 0) {
			auth = substr(lv[i], RSTART + 1, RLENGTH - 2);
			lv[i] = substr(lv[i], 1, RSTART - 1);
			sub(/[ \t]+$/, "", lv[i]);
			n = split(auth, arr, /, */);

			# Find the first contributor not the commit author.
			for (j = 1; j <= n; j++) {
				if (arr[j] == name)
					continue;
				cont = arr[j];
				break;
			}
			delete arr;
		}

		# Add to the list of contributions for this contributor.
		if (cont in map)
			map[cont] = map[cont] " " i;
		else
			map[cont] = i;
	}

	print pkg" ("vers") "dist"; urgency=medium";

	# First emit contributions by people other than the commit author.
	n = 0;
	for (cont in map) {
		if (cont == name)
			continue;

		n++;
		print "\n  [ "cont" ]";
		$0 = map[cont];
		for (i = 1; i <= NF; i++)
			lineWrap("  *", lv[int($i)]);
	}
	if (n > 0)
		print "\n  [ "name" ]";
	else
		print "";

	$0 = map[name];
	for (i = 1; i <= NF; i++)
		lineWrap("  *", lv[int($i)]);

	delete map;

	if (m_name) {
		name = m_name;
		mail = m_mail;
		m_name = m_mail = 0;
	}
	print "\n -- "name" "mail"  "date" 00:00:00 +0000\n";
}

/^\* (Sun|Mon|Tue|Wed|Thu|Fri|Sat) [A-Z][a-z][a-z] [ 0-3][0-9] / {
	date = $2", "$4" "$3" "$5;
	i = index($0, " - ");
	vers = substr($0, i + 3);
	name = substr($0, 19, i - 19);
	i = index(name, " <");
	mail = substr(name, i + 1);
	name = substr(name, 1, i - 1);
	if (name == "")
		error("No name in: "$0);

	lc = 0;
	delete lv;

	next;
}

/^- / {
	sub(/^- +/, "");
	lv[++lc] = $0;
	next;
}

NF == 0 {
	emitEntry();
	date = "";
	next;
}

{
	lv[lc] = lv[lc] $0;
	next;
}

END {
	if (date && !err)
		emitEntry();
}
