Skip to content

Instantly share code, notes, and snippets.

@apemberton
Last active April 1, 2018 13:34
Show Gist options
  • Save apemberton/94db4ae8057afc7d1e1037b3b4adea86 to your computer and use it in GitHub Desktop.
Save apemberton/94db4ae8057afc7d1e1037b3b4adea86 to your computer and use it in GitHub Desktop.
A Mac-specific bash script to rename wine binary - inspired by http://everydaywithlinux.blogspot.com/2012/11/patch-strings-in-binary-files-with-sed.html
#!/bin/bash
FILE="./target/wine-patched/bin/wine"
PATTERN="\s{5}<key>CFBundleName<\/key>\s{5}<string>Wine<\/string>\s{5}"
REPLACEMENT="<key>CFBundleName</key><string>$1</string>"
if [ -z "$1" ]
then
echo 'Please supply alternate name for bin/wine'
exit 1
fi
# Find all unique strings in FILE that contain the pattern
FOUND=$(strings ${FILE} | pcregrep -M -o ${PATTERN})
if [ "${FOUND}" != "" ] ; then
echo "File '${FILE}' contains '${PATTERN}'"
# Create null terminated ASCII HEX representations of the strings
MATCH_HEX=$(echo -n "${FOUND}" | xxd -g 0 -u -ps -c 256)""
REPLACEMENT_HEX=$(echo -n "${REPLACEMENT}" | xxd -g 0 -u -ps -c 256)""
if [ ${#REPLACEMENT_HEX} -le ${#MATCH_HEX} ] ; then
# Pad the replacement string with SPACES so the length matches the original string
while [ ${#REPLACEMENT_HEX} -lt ${#MATCH_HEX} ] ; do
REPLACEMENT_HEX="${REPLACEMENT_HEX}20"
done
# Now, replace every occurrence of FOUND with NEW_STRING
hexdump -ve '1/1 "%.2X"' ${FILE} | \
sed "s/${MATCH_HEX}/${REPLACEMENT_HEX}/g" | \
xxd -r -p > ${FILE}.tmp
chmod +x ${FILE}.tmp
mv ${FILE}.tmp ${FILE}
else
echo "New string '${NEW_STRING}' is longer than old string '${OLD_STRING}'. Can't rename."
exit 1
fi
else
echo "No match for '${PATTERN}' in file '${FILE}'. Can't rename."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment