#!/usr/bin/perl
#
# Turn off debugging stubs in a PRC.
#
# This program is used to generate non-debug version of a debug-PRC
# without reconfiguring the project and recompiling the PRC.
#
# The name `beef' comes from the magic value (0x12beef34) which
# is used in that debugging stub code.
#
#  10:   2f3c 6764 6253  movel #1734632019,%sp@-
#  16:   4e4f            trap #15
#  18:   a27b            0121173
#  1a:   0cae 12be ef34  cmpil #314502964,%fp@(-4)
#  20:   fffc
#  22:   6624            bnes 48 <.ef>
#
# This last `bne' (0x66) is rewritten as a unconditional `bra' (0x60).

$srcpat = "\xa2\x7b\x0c\xae\x12\xbe\xef\x34\xff\xfc\x66";
$dstpat = "\xa2\x7b\x0c\xae\x12\xbe\xef\x34\xff\xfc\x60";

$source = shift || die "No source file given.";
unless ($dest = shift) {
    $dest = $source;
    $dest .= "-nodbg" unless ($dest =~ s/^([^.]*)\./$1-nodbg./);
}

$/ = undef; # slurp mode
open (INPUT, "<$source");
binmode INPUT;
$data = <INPUT>;
close (INPUT);

die "Pattern not found." unless $data =~ s/$srcpat/$dstpat/os;
open (OUTPUT, ">$dest");
binmode OUTPUT;
syswrite OUTPUT, $data;
close (INPUT);
