#!/usr/bin/perl
# 
# x2b.pl -- (c) Chris Gerber, 1998
#
#  This is a kludge. You have been warned.
#

$usage = "$0 (xbm file) (output file) [-r] [-xN] [-yN]\n";
$header = ">xbm2bin.h";

if ($#ARGV < 1) {
  print $usage;
  exit( 1 );
} else {
  process_args();
}

$xbm_file =~ /(\w*)\.xbm$/i; $prefix = $1;

print "Coding...\n";
open( H_FILE, $header ) || die "Failed to open $header.\n";
  print H_FILE "#include \"$xbm_file\"\n";
  print H_FILE "#define BITS $prefix","_bits\n";
  print H_FILE "#define WIDTH $prefix","_width\n";
  print H_FILE "#define HEIGHT $prefix","_height\n";
  print H_FILE "#define DEBUG\n" if $DEBUG;
close( H_FILE );

$comp_cmd = "cc -Aa -o xbm2bin xbm2bin.c";
$comp_cmd .= " -DDEBUG" if $DEBUG;

print "Compiling...\n % $comp_cmd\n";
print `$comp_cmd`;

$run_cmd = "xbm2bin $out_file $options";
print "Running...\n % $run_cmd\n";
print `$run_cmd`;

print "Done.\n";

sub process_args {
  my $i = 2;

  $xbm_file = $ARGV[0];
  $out_file = $ARGV[1];
  $options = "";

  while ($i <= $#ARGV)
  {
    if ($ARGV[$i] =~ /^-D/) {
      $DEBUG = 1;
    } else {
      $options .= $ARGV[$i] . " ";
    }
    $i++;
  }
}