#!/usr/bin/env ascript

import(re)

if (sys.argv.len() < 2) {
	sys.stderr.printf("usage: svnlog.az file\n")
	sys.exit(1)
}
isSeparator(line:string) = re.match("-{72,}", line)
isBlank(line:string) = re.match("^$", line)

stat = `STAT_Start
lineInfo = ""
header = ""
headerBlank = " " * 10
for (line in open(sys.argv[1]).readlines()) {
	if (stat == `STAT_Start) {
		if (isSeparator(line)) {
			stat = `STAT_Info
		}
	} elsif (stat == `STAT_Info) {
		lineInfo = line
		stat = `STAT_Comment
	} elsif (stat == `STAT_Comment) {
		if (isSeparator(line)) {
			stat = `STAT_Info
		} elsif (isBlank(line)) {
			// nothing to do
		} else {
			fields = re.split(r" \| ", lineInfo):list
			if (m = re.match(r"\d{4}-\d{2}-\d{2}", fields[2])) {
				headerNew = m.group(0)
				if (header == headerNew) {
					print(headerBlank, " ", line)
				} else {
					header = headerNew
					print(header, " ", line)
				}
			} else {
				print(headerBlank, " ", line)
			}
			stat = `STAT_CommentCont
		}
	} elsif (stat == `STAT_CommentCont) {
		if (isSeparator(line)) {
			stat = `STAT_Info
		} elsif (isBlank(line)) {
			// nothing to do
		} else {
			print(headerBlank, " ", line)
		}
	}
}
