#!/usr/bin/perl
$file="PFS.IMG";
$num=0;

open(IN, $file);
binmode(IN);
sysread(IN,$buf,14);
sysread(IN,$buf,2);
$fnum=ord(substr($buf,1,1))*256+ord(substr($buf,0,1));
print "file count : $fnum\n";

for($i=0;$i<$fnum;$i++){
  sysread(IN,$buf,52);
  for($j=0;$j<40;$j++){
    if(ord(substr($buf,$j,1))==0){
      last;
    }
  }
  $fn=substr($buf,0,$j);
  printf "$fn";
  $nname[$num]=$fn;
  for($j=0;$j<40-length($fn);$j++){
    printf " ";
  }
  for($j=44;$j<52;$j++){
    printf (" %02x",ord(substr($buf,$j,1)));
  }
  $nstart[$num]=ord(substr($buf,46,1))*256*256+
                ord(substr($buf,45,1))*256+ord(substr($buf,44,1));
  $nsize[$num]=ord(substr($buf,50,1))*256*256+
               ord(substr($buf,49,1))*256+ord(substr($buf,48,1));
  printf (" %03d %03d",$nstart[$num],$nsize[$num]);
  $num++;
  printf "\n";
}

for($i=0;$i<$num;$i++){
  if($nsize[$i]==0){
    next;
  }
  sysread(IN,$buf,$nsize[$i]);
  $nname[$i] =~ s/\\/\//g;
  $spos=0;
  while(index($nname[$i],"\/",$spos) > 0){
    $ndir=substr($nname[$i],0,index($nname[$i],"\/",$spos));
    $spos=index($nname[$i],"\/",$spos)+1;
    unless(-d $ndir){
      mkdir($ndir);
    }
  }
  
#  printf "$nname[$i]\n";
  open(OUT, "> $nname[$i]");
  binmode(OUT);
  print OUT $buf;
  close(OUT);
}

close(IN);
