#!/usr/bin/perl
$file="sys.bin";
$vshift0=19;

open(IN, $file);
binmode(IN);
#skip
sysread(IN,$buf,$vshift0);
#size read
sysread(IN,$buf,3);
$vshift=ord(substr($buf,0,1))*256*256+
        ord(substr($buf,1,1))*256+ord(substr($buf,2,1));
#skip
sysread(IN,$buf,$vshift+5);

#file counter set
sysread(IN,$buf,1);
$fnum=ord($buf);

for($i=0;$i<$fnum;$i++){
  sysread(IN,$buf,16);
  for($j=0;$j<12;$j++){
    if(ord(substr($buf,$j,1))==0){
      last;
    }
  }
  $fn=substr($buf,0,$j);
  printf "$fn";
  $nname[$i]=$fn;
  for($j=0;$j<13-length($fn);$j++){
    printf " ";
  }
  $nsize[$i]=ord(substr($buf,14,1))*256+ord(substr($buf,15,1));
  printf (" %03d",$nsize[$i]);
  printf "\n";
}

#file output
for($i=0;$i<$fnum;$i++){
  sysread(IN,$buf,$nsize[$i]);
  if( (ord(substr($buf,0,1))==0x1f) && (ord(substr($buf,1,1))==0x8b) ){
    open(OUT, "> data/$nname[$i].gz");
  } else {
    open(OUT, "> data/$nname[$i]");
  }
  binmode(OUT);
  print OUT $buf;
  close(OUT);
}

close(IN);
