# fixdump: updates pre-v2.3 shadump files to new format

use strict;

die "Usage: fixdump file [ file ... ]\n" unless (@ARGV);

my ($fn, $fh, @lines, $fix, $lnum, $h, $hq);
FILE: for (@ARGV) {
	$fn = $_;
	next unless open($fh, "<$fn");
	@lines = <$fh>; close($fh);
	($fix, $lnum, $h, $hq) = (0, 0, 0, 0);
	for (@lines) {
		if (/^H:/) { $h = $lnum }
		elsif (/^HQ:/) { $hq = $lnum; $fix = 1 }
		$lnum++;
	}
	next FILE unless ($fix && open($fh, ">$fn"));
	($lines[$h] = $lines[$hq]) =~ s/^HQ:/H:/;
	$lines[$hq] = "";
	print $fh @lines;
	close($fh);
}
