#!/bin/bash
#
# n64title v3.2  Keep an eye on http://www.jedi.net/n64 for the latest version.
#
# Here we go.. a piece of shit script that should work on any unix OS to 
# identify your "backed up" Nintendo-64 ROM images.  It uses standard UNIX
# commands.  You'll need '/bin/bash' and 'dd' to run this.
# if bash is located somewhere else (like /usr/local/bin) change line 1
# 
# BTW, this works on the first releases from ATX.. no guarantees that it will
# continue to work..  But I'll try to keep up with it.. visit the web site
# for the latest version..
#
# Cheers....  --Kyoo on IRC (1/26/97)
#
# New in version 2.0: Ability to distinguish Japanese and US carts.. I took
# a guess at the country code location.. Let me know if I screwed up..
#
# New in version 3.0: You no longer need 'cut', but you do need 'dd'.  I
# switched to using dd because it was SO much faster and had a built in
# byte swap function.  Hopefully all dd's have the same options as mine.
# This one also uses a tmp file, defined at the top of the code.  Since dd
# likes to print messages to the console, I had to trick it a bit. 
# This version also finds the manufacturer code.. i.e. Nintendo.  Unfortunately
# I only know that one manufacturer code.. but when more roms are released,
# I'll add more in and release a new version.
#
# New in version 3.1: ROM sizes added.. 64M, 128M, etc..  This will probably
# not work when a format other than VD64 comes out.  Let's hope that all the
# companies pick a standard image type this time around!
# This version requires 'bc' for some simple calculations.  Every UNIX I have
# been on has had this tool, so I hope you have it.
#
# New in version 3.2: Lots of bugs fixed.  Turns out I was using bash instead
# of sh.. little did I know it was linked on my system.  So now, bash is 
# required.  3.1 stupidly used an uncommon binary called filesize.. 3.2 now
# uses 'ls -lg' output, and hopefully that command is the same on all
# unix flavors.  I tested bsd, linux, sunos, and hp-ux. 
# Another bug was found on bsd. Apparently bsd 'dd' is a bit picky, so I
# had to rework the method of extracting the title and swapping the bytes.
# The new command line works on bsd and linux for sure.
# Also added are error checking lines, to make sure the necessary files
# exist.. Hopefully it will reduce the number of random shell errors that
# don't make any sense. 
#
# If you find a bug, please goto the website (mentioned above) and submit it.
#
# hackers: if you want to incorporate my tiny bit of work into a larger
# project sometime in the future, here are the offsets for the two things:
#       CART Title: 20h-33h Odd & Even bytes are reversed.
#       CART Country: 3Fh
#       CART Manufacturer: 3Ah

#### File locations, if they aren't in your path, fill in full paths. #######
DD=dd
BC=bc
LS=ls
RM=rm
CAT=cat
ECHO=echo
TMPFILE=/tmp/.title.$$$
#############################################################################

if [ "$1" = "" ]; then
  $ECHO "$0: Wrong usage.  Try \"$0 <romfile> [<romfile> <romfile>]"
  $ECHO "$0: This program is used to identify information from N64 ROM images."
  $ECHO "$0: Thrown together by Kyoo.  See http://www.jedi.net/n64 for updates."
  exit;
fi

# Let's do some checking first
if [ ! `type -p $RM` ]; then
  $ECHO "$0: $RM not found in path."
  exit;
fi
if [ ! `type -p $LS` ]; then
  $ECHO "$0: $LS not found in path."
  exit;
fi
if [ ! `type -p $BC` ]; then
  $ECHO "$0: $BC not found in path."
  exit;
fi
if [ ! `type -p $DD` ]; then
  $ECHO "$0: $DD not found in path."
  exit;
fi
if [ ! `type -p $CAT` ]; then
  $ECHO "$0: $CAT not found in path."
  exit;
fi
$ECHO > $TMPFILE
if [ ! -w `type -p $TMPFILE` ]; then
  $ECHO "$0: /tmp directory not writable.  Please modify \$TMPFILE definition in $0."
  $RM $TMPFILE
  exit;
fi
$RM $TMPFILE

# Start the loop through the files.
for t in $*; do {
  $ECHO -n "$t: "
  if [ ! -e $t ]; then
    $ECHO "File not found."
    continue;
  fi
  TITLE=`$DD if=$t count=2 ibs=1 of=$TMPFILE >&/dev/null;$CAT $TMPFILE;$RM $TMPFILE`
  if [ ! $TITLE = "7" ]; then
     $ECHO "Unrecognizable by this lame script."
     continue;
  fi
  TITLE=`$DD conv=swab if=$t bs=2 skip=16 count=10 of=$TMPFILE >&/dev/null;$CAT $TMPFILE;$RM $TMPFILE`
  COUNTRY=`$DD if=$t skip=63 count=1 ibs=1 of=$TMPFILE >&/dev/null;$CAT $TMPFILE;$RM $TMPFILE`
  COMPANY=`$DD if=$t skip=58 count=1 ibs=1 of=$TMPFILE >&/dev/null;$CAT $TMPFILE;$RM $TMPFILE`
  $ECHO -n $TITLE

# Company Codes
  if [ $COMPANY = "N" ]; then
     $ECHO -n " (Nintendo/"
#  elif [ $COMPANY = "E" ]; then
#     $ECHO -n " (reserved/"
  else
     $ECHO -n " (unknown/"
  fi

# Country Codes
  if [ $COUNTRY = "J" ]; then
     $ECHO -n "Jap"
  elif [ $COUNTRY = "E" ]; then
     $ECHO -n "US"
  else
     $ECHO -n "unknown"
  fi

# FILE SIZES
  SIZE=`$LS -lg $t | awk '{print $5}'`
  if [ ! `$ECHO "$SIZE%131072" | $BC` = 0 ]; then
     $ECHO -n "/??M)"
  else
     $ECHO -n "/`$ECHO "$SIZE/131072" | $BC`M)"
  fi

  $ECHO
}; done
