#!/usr/bin/perl -w

use strict;

#
# A simple and ugly Perl script for converting nALFS XML log
# files to user-friendly format.
#
# Usage:  ./flog < ~/.nALFS/package/diffutils-2.7.log
#    or:  cat ~/.nALFS/package/*.log | ./flog
#    or:  something else...
#

my %strings = (
	'<package mode="start">'
		=> "Package started               :",
	'<prebuild mode="start">'
		=> "    Prebuild stage started    :",
	'<build mode="start">'
		=> "    Build stage started       :",
	'<postbuild mode="start">'
		=> "    Postbuild stage started   :",
	'<stage mode="start">'
		=> "    Stage started             :",

	'<package mode="end" status="done">'
		=> "Package succeeded             :",
	'<prebuild mode="end" status="done">'
		=> "    Prebuild stage succeeded  :",
	'<build mode="end" status="done">'
		=> "    Build stage succeeded     :",
	'<postbuild mode="end" status="done">'
		=> "    Postbuild stage succeeded :",
	'<stage mode="end" status="done">'
		=> "    Stage succeeded           :",

	'<package mode="end" status="failed">'
		=> "Package failed                :",
	'<prebuild mode="end" status="failed">'
		=> "    Prebuild stage failed     :",
	'<build mode="end" status="failed">'
		=> "    Build stage failed        :",
	'<postbuild mode="end" status="failed">'
		=> "    Postbuild stage failed    :",
	'<stage mode="end" status="failed">'
		=> "    Stage failed              :"
);

while (<>) {
	if (m/<name>(.*)<\/name>/) {
		print "Package: $1\n";

	} elsif	(m/<version>(.*)<\/version>/) {
		print "Version: $1\n\n";

	} elsif (m/<handler_action>(.*)<\/handler_action>/) {
		print "        | $1\n";

	} elsif (m/<stopped>(.*)<\/stopped>/) {
		print "Stopped: $1\n";

	} elsif (m/<\/single_try>/) {
		print "\n\n";

	} else {
		foreach my $str (keys %strings) {
			if (m/$str(.*)<\//) {
				print $strings{$str} . " $1\n";
			}
		}
	}
}
