#!/bin/bash

debug="no"

function usage
{
  echo "Usage  1 : $0 -t {ped|veh|obj|wea|sfx|scen|mapinf|objive} -i indexes -f files" 
  echo " Example : $0 -t veh -i {0,5,{8..10}} -f GAME01_{1..3}.BIN"
  echo "Usage  2 : $0 file1 file2" 
  echo " Example : $0 GAME01_1.BIN GAME01_2.BIN"
}

opt="none"
type=""
indexes=""
files=""

for i
do
  case "$i" in
    -t)
      opt="type";;
    -i)
      opt="indexes";;
    -f) 
      opt="files";;
    *) 
      if [ "X$opt" = "Xtype" ]
      then
         type="$i"
      elif [ "X$opt" = "Xindexes" ]
      then
         indexes="$indexes $i"
      elif [ "X$opt" = "Xfiles" ]
      then
         files="$files $i"
      fi
      ;;
  esac
done

if [ "X$debug" = "Xyes" ]
then
  echo "type    : " $type
  echo "indexes : " $indexes
  echo "files   : " $files
  echo "\$#      : " $#
fi

if [ "X$type" = "X" -o "X$indexes" = "X" -o "X$files" = "X" ]
then
  if [ $# = 2 ]
  then
    tmp1=_$1
    tmp2=_$2
    out=diff_$1_$2.diff
    echo "" > $out
    # Header     : -j      0 -N 6 --width=6
    echo "Header" >> $out
    od -A d -t x1  -j      0 -N 6 --width=6 $1 > $tmp1
    od -A d -t x1  -j      0 -N 6 --width=6 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Offsets    : -j      6 -N 32770 --width=32  
    echo "Offsets" >> $out
    od -A d -t x1  -j      6 -N 32770 --width=32 $1 > $tmp1
    od -A d -t x1  -j      6 -N 32770 --width=32 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Peds       : -j  32776 -N 23552 --width=92 
    echo "Peds" >> $out
    od -A d -t x1  -j  32776 -N 23552 --width=92 $1 > $tmp1
    od -A d -t x1  -j  32776 -N 23552 --width=92 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Vehicles   : -j  56328 -N  2688 --width=42 
    echo "Vehicles" >> $out
    od -A d -t x1  -j  56328 -N  2688 --width=42 $1 > $tmp1
    od -A d -t x1  -j  56328 -N  2688 --width=42 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Objects    : -j  59016 -N 12000 --width=30
    echo "Objects" >> $out
    od -A d -t x1  -j  59016 -N 12000 --width=30 $1 > $tmp1
    od -A d -t x1  -j  59016 -N 12000 --width=30 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Weapons    : -j  71016 -N 18432 --width=36 
    echo "Weapons" >> $out
    od -A d -t x1  -j  71016 -N 18432 --width=36 $1 > $tmp1
    od -A d -t x1  -j  71016 -N 18432 --width=36 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Sfx        : -j  89448 -N  7680 --width=30
    echo "Sfx" >> $out
    od -A d -t x1  -j  89448 -N  7680 --width=30 $1 > $tmp1
    od -A d -t x1  -j  89448 -N  7680 --width=30 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Scenario   : -j  97128 -N 16384 --width=8
    echo "Scenario" >> $out
    od -A d -t x1  -j  97128 -N 16384 --width=8 $1 > $tmp1
    od -A d -t x1  -j  97128 -N 16384 --width=8 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Unknown    : -j 113512 -N   448 --width=32 
    echo "Unknown" >> $out
    od -A d -t x1  -j 113512 -N   448 --width=32 $1 > $tmp1
    od -A d -t x1  -j 113512 -N   448 --width=32 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Mapinfos   : -j 113960 -N    14 --width=14
    echo "Mapinfos" >> $out
    od -A d -t x1  -j 113960 -N    14 --width=14 $1 > $tmp1
    od -A d -t x1  -j 113960 -N    14 --width=14 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Objectives : -j 113974 -N    98 --width=14 
    echo "Objectives" >> $out
    od -A d -t x1  -j 113974 -N    98 --width=14 $1 > $tmp1
    od -A d -t x1  -j 113974 -N    98 --width=14 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    # Unknown    : -j 114072 -N  1938 --width=32 
    echo "Unknown" >> $out
    od -A d -t x1  -j 114072 -N  1938 --width=32 $1 > $tmp1
    od -A d -t x1  -j 114072 -N  1938 --width=32 $2 > $tmp2
    sdiff -s -w 800 $tmp1 $tmp2 | sed -e 's/[\t ]*[|<>][\t ]*/\n/g' >> $out

    rm $tmp1 $tmp2
    cat $out
    rm $out
  else
    usage $0
  fi
  exit
fi

base=0
width=0
typename=""

case "$type" in
'ped')
  base=32776
  width=92
  typename="Pedestrian"
  ;;
'veh')
  base=56328
  width=42
  typename="Vehicle"
  ;;
'obj')
  base=59016
  width=30
  typename="Object"
  ;;
'wea')
  base=71016
  width=36
  typename="Weapon"
  ;;
'sfx')
  base=89448
  width=30
  typename="Sfx"
  ;;
'scen')
  base=97128
  width=8
  typename="Scenario"
  ;;
'mapinf')
  base=113960
  width=14
  typename="Map infos"
  ;;
'objive')
  base=113974
  width=14
  typename="Objectives"
  ;;
*)
esac

if [ "X$debug" = "Xyes" ]
then
  echo "base    : " $base
  echo "width   : " $width
  exit
fi

echo "--- $typename base=$base size=$width"
for file in $files; 
do
  echo "+++ $file"
done

for i in $indexes; 
do
  offset=$[$base + $i * $width]
  echo "$type #$i"
  echo -n "$offset +  "
  for ((j=0;j<=$[$width / 10];j+=1)); 
  do
     echo -n "$[$j * 10]                            "
  done
  echo ""
  for file in $files; 
  do
    data=`od -A d -j $offset -N $width --width=$width -t x1 $file | head -1 | cut -d ' ' -f 2-`
    echo "       " $data
  done
done
