#!/usr/bin/perl -w

##########################################################
#                                                        #
# This is to show decimal values for config.txt and      #
# tables.txt for the people that don't think in hex.     #
#                                                        #
##########################################################

use strict;

usage() unless($ARGV[0]);

open(INFILE, "$ARGV[0]") or die("Could not open $ARGV[0]: $!\n\n");

foreach my $line (<INFILE>){
  chomp $line;
  next if($line =~ /^\s*($|\/\/|\#|;)/); # Skip comments
  if ($line =~ /(\w+(\[\w+\])?)=([a-fA-F0-9]{1,2}\s){1,8}\b/) {
    my($arrayname, $arrayvalues) = split(/\=/,$line);
    my(@values) = split(/\s+/,$arrayvalues);
    my(@decvalues);
    foreach my $hexval (@values){
      my $tmpval = hex($hexval);
      my $decval = sprintf("%d",$tmpval);
      #print "My value is $hexval in hex, $decval in decimal\n";
      push(@decvalues, $decval);
    }
    print "$arrayname\= @decvalues\n";
  } elsif ($line =~ /(\w+(\[\w+\])?)=([a-fA-F0-9]{1,2})\b/) {  # match a name=FF or name[1]=FF
    my $varname = $1;
    my $varvalue = hex($3);
    my $decvalue = sprintf("%d",$varvalue);
    print "$varname\=$decvalue\n";
  }
}
  
sub usage {
  print "Usage:
  $0 <filename>
    Where <filename> would be either config.txt or tables.txt\n\n";
  exit;
}
