#!/usr/bin/perl

#
# perl n64title v1.1
#   by Kyoo (1/28/97)
# for updates seek: http://www.jedi.net/n64
#
# v1.1 added detection for non image files.
# send bug reports via the web page.
#

if ($ARGV[0] eq '') {
  print $0, ": Wrong usage.  Try $0 <romfile> [<romfile> <romfile>]\n";
  print "$0: This program is used to identify information from N64 ROM images.\n";
  print "$0: Thrown together by Kyoo. See http://www.jedi.net/n64 for updates.\n";
}

while (@ARGV) {
  $t=$ARGV[0];
  shift;
  print $t,": ";
  if ( ! -r $t ) {
    print "file not found or readable\n";
    next;
  }
  open (FILE, $t );
  $size = -s FILE;
  binmode (FILE);
  sysread (FILE,$title,2);
  if ( $title ne '7€') { 
    print "Unrecognized image. Try upgrading versions.\n";
    next;
  } 
  sysread (FILE,$title,30);
  undef $title;
  sysread (FILE,$title,20);
  $title =~ s/((.)(.))/$3$2/g;
  sysread (FILE,$company,6);
  undef $company;
  sysread (FILE,$company,1);
  sysread (FILE,$country,4);
  undef $country;
  sysread (FILE,$country,1);

  print $title," ";
  if ( $company eq 'N') {
    print "(Nintendo";
  }
  else {
    print "(Unknown";
  }

  if ( $country eq 'J') {
    print "/Jap";
  } 
  elsif ( $country eq 'E') {
    print "/US";
  }
  elsif ( $country eq 'P') {
    print "/PAL";
  }
  else {
    print "/Unknown";
  }
  
  if ( ($size % 131072) ne 0 ) {
    print "/??M";
  }
  else {
    print "/",$size/131072,"M";
  }
  print ")\n";
  close (FILE);
}
exit();
