#!/usr/bin/perl
#
# Usage:
#
#    wine --debugmsg trace+all winmx331.exe 2>&1 | wineparse.pl
#
#

$maxlines = 500;
$file     = "/tmp/wineparse.log";

$wrapped  = 0;
$pos      = 0;
@tempbuffer;

# Remove the file
system ( "/bin/rm $file &> /dev/null" );

while (<stdin>) {

  @tempbuffer[$pos] = $_;

  if ( $_ =~ /wine: Unhandled exception, starting debugger(.*)/ ) {

     open ( FILE, ">$file" );
 
     if (($full == 0) || ($pos == 0)) 
     { 
       print FILE @tempbuffer; 
     }
     else 
     {
       for ($i = ($pos+1) % $maxlines; $i != $pos; $i = ($i+1) % $maxlines) {
           print FILE $tempbuffer[$i];
       }
       print FILE "wine: Unhandled exception, starting debugger...\n";
     }
     close (FILE);
     exit 0;
  }

  if ( $pos+1 == $maxlines ) { $full = 1; }
  $pos = ($pos+1) % $maxlines;

} 

