#! /usr/bin/perl -w

# $Id$

# This will be a full lmpc check in the far future.

use strict;

(my $script=$0) =~ s,.*/,,g;
my $opt="";

if ($#ARGV<1) { die "usage: $script lmpc demofile ...\n"; }

my $lmpc = shift;

if (! -x $lmpc) { die "can't execute $lmpc\n"; }

foreach (@ARGV) {
	my $demofile = $_;
	if (!-f $demofile) {
		die "no original file $demofile\n";
	}
	(my $filename=$demofile) =~ s,.*/,,g;
	(my $basename=$filename) =~ s,\..*,,g;

	my $basedir = $ENV{"HOME"} . "/games/democheck/$basename-$$";
	if (!mkdir $basedir, 0777) {
		die "can't create directory '$basedir': $!\n";
	}
	my $d_b = "$basedir/b";
	my $command;
	if ($filename=~/\.bz2/) {
		$command = "bzip2 -dc $demofile > $d_b";
	}
	else {
		$command = "cp $demofile $d_b";
	}
	my $error = system $command;
	if ($error != 0) {
		die "Command '$command' exit with $error\n";
	}
	if (!-f $d_b) {
		die "no base file $d_b\n";
	}
  my $d_bt="$basedir/bt";
  system "$lmpc --to-txt -m 1 $opt $d_b $d_bt";
  my $d_btt="$basedir/btt";
  system "$lmpc --to-txt $opt $d_bt $d_btt";
  my $d_bttt="$basedir/bttt";
  system "$lmpc --to-txt $opt $d_btt $d_bttt";
  my $d_btttb="$basedir/btttb";
  my $a = system "$lmpc --to-bin $opt $d_bttt $d_btttb";
  if ($a != 0) {
    print "return=$a\n";
  }
  my $d_btttbb="$basedir/btttbb";
  system "$lmpc --to-bin $d_btttb $d_btttbb";
  if (system ("diff $d_b $d_btttbb") != 0) {
    die "problems with $basename\n";
  }
  else {
    system "rm -rf $basedir";
  }
}

