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

use strict;

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

my ($pre, $post, $alg, @f, $H, $fh, $fn);
FILE: for (@ARGV) {
	$fn = $_;
	next unless open($fh, "<$fn");
	$pre = ""; $post = ""; $alg = 0;
	while (<$fh>) {
		if (/^alg:/) {
			$pre .= $_; $alg = 1; chomp; @f = split(":");
			unless ($f[1] == 384 || $f[1] == 512) {
				close($fh); next FILE;
			}
		}
		elsif (/^H:/) { $H = $_ }
		elsif (/^HQ:/) { s/Q//; $H = $_ }
		elsif ($alg) { $post .= $_ }
		else { $pre .= $_ }
	}
	close($fh);
	next FILE unless open($fh, ">$fn");
	print $fh $pre, $H, $post;
	close($fh);
}
