#!/bin/bash

pattern="\xff\xe9\x75\x02\x00\x00\x85\xdb\x74\x68\x4d\x8b\x7e\x10\x49\x89"
offset=8
file="/opt/resolve/bin/resolve"

# https://stackoverflow.com/a/17168777
matches=$(LANG=C grep -obUaP "$pattern" "$file")
matchcount=$(echo "$matches" | wc -l)
if [[ -z $matches ]]; then echo "pattern not found";
elif [[ $matchcount -ne 1 ]]; then echo "pattern returned $matchcount matches instead of 1";
else
	patternOffset=$(echo $matches | cut -d: -f1)
	instructionOffset=$(($patternOffset + $offset))
	echo "patching byte '0x$(hexdump -s $instructionOffset -n 1 -e '/1 "%02x"' "$file")' at offset $instructionOffset"
	printf '\x75' | dd conv=notrunc of="$file" bs=1 seek=$instructionOffset count=1;
fi
