#!/bin/sh
# Really simple and sluggish binary patch
# for use with patch files generated with "cmp -l"
# Contributed by Miguel Angel <maacruz@navegalia.com>

ECHO=/bin/echo
AWK=/usr/bin/mawk

case "$1" in
"")
	$ECHO "Usage: 	binpatch [-r] binfile"
	$ECHO "	(and at standard input: bytepos(decimal) oldbyte(octal) newbyte(octal)"
	exit
	;;
"-R"|"-r")
	reverse=true
	shift 
	;;
esac

while read pos oldbyte newbyte ;
do
pos=`echo|$AWK "{print( $pos-1 )}"`
if [ "$reverse" != "" ] ; then 
	byte=\\$oldbyte;
else
	byte=\\$newbyte;
fi
if [ "$byte" != "" ] ; then 
	$ECHO -n -e "$byte" | dd of=$1 bs=1 seek=$pos count=1 conv=notrunc >/dev/null 2>&1
fi
done
