#@files=("first.dxf","second.dxf");
$dirname="tiles_classified";
opendir ( DIR, $dirname );
@files= readdir(DIR); 
closedir(DIR);

$out="terje_vegetation.dxf";
open(UFIL,">$out");
$min1=1e99;$max1=-1e99;
$min2=1e99;$max2=-1e99;
$print="";
foreach $file (@files)
{
  if ($file=~/\.dxf$/ig)
  {
    print "$file\n";
    open(IFIL,"$dirname/$file");
    @lines=<IFIL>;
    close(IFIL);
    $skipit=1;
    for ($ii=0;$ii<$#lines;$ii++)
    {
      if ($lines[$ii] =~/\$EXTMIN/)
      { 
        $thismin1 = 0+$lines[$ii+2];
        $thismin2 = 0+$lines[$ii+4];
        if ($thismin1<$min1)
        {$min1=$thismin1;}
        if ($thismin2<$min2)
        {$min2=$thismin2;}
      }
      if ($lines[$ii] =~/\$EXTMAX/)
      { 
        $thismax1 = 0+$lines[$ii+2];
        $thismax2 = 0+$lines[$ii+4];
        if ($thismax1>$max1)
        {$max1=$thismax1;}
        if ($thismax2>$max2)
        {$max2=$thismax2;}
      }
      if ($lines[$ii]=~/SECTION/)
      {
        if ($skipit==1)
        { $skipit++;}
        elsif ($skipit==2)
        { $skipit=0;}
      }
      if ($lines[$ii]=~/EOF/)
      {
        $skipit=1;
      }
      if ($skipit==0)
      {
        $print.=$lines[$ii];
      }
    }
  }
}

print UFIL "0
SECTION
2
HEADER
9
\$EXTMIN
10
$min1.00
20
$min2.00
9
\$EXTMAX
10
$max1.00
20
$max2.00
0
ENDSEC
0
";
print UFIL $print;
print UFIL "EOF\n";
close(UFIL);
