Skip to content

Instantly share code, notes, and snippets.

@galenzhao
Forked from comuttun/resign.sh
Last active August 29, 2015 14:04
Show Gist options
  • Save galenzhao/dd5d1cdfaac9da588fd2 to your computer and use it in GitHub Desktop.
Save galenzhao/dd5d1cdfaac9da588fd2 to your computer and use it in GitHub Desktop.
#!/bin/bash -v
MYDIR=`pwd`
#MYDIR=$(cd $(dirname $0) && pwd)
IPA="$1"
PROVISION="$2"
CERTIFICATE="$3"
usage() {
echo "Usage: $0 <ipa file> <provision profile> <certificate name>" >&2
exit 99
}
if [ -z "$IPA" ]; then
usage
fi
if [ -z "$PROVISION" ]; then
usage
fi
if [ -z "$CERTIFICATE" ]; then
usage
fi
if [ ! -f "$IPA" ]; then
echo "ERROR: ipa file '$IPA' not found" >&2
usage
fi
if [ ! -f "$PROVISION" ]; then
echo "ERROR: Provision file '$PROVISION' not found" >&2
usage
fi
WORKDIR=$(mktemp -d /tmp/resign.XXXXXX)
if [ $? -ne 0 ] || [ ! -d "$WORKDIR" ]; then
echo "ERROR: Cannot create temporary directory" >&2
exit 1
fi
unzip -q "$IPA" -d $WORKDIR
if [ $? -ne 0 ]; then
echo "ERROR: Cannot unzip ipa to directory" >&2
exit 1
fi
# remove the signature
rm -rf $WORKDIR/Payload/*.app/_CodeSignature $WORKDIR/Payload/*.app/CodeResources
# replace the provision
cp -p "$PROVISION" $WORKDIR/Payload/*.app/embedded.mobileprovision
if [ $? -ne 0 ]; then
echo "ERROR: Cannot replace provision file" >&2
exit 1
fi
# sign with the new certificate
/usr/bin/codesign -f -s "$CERTIFICATE" --resource-rules $WORKDIR/Payload/*.app/ResourceRules.plist $WORKDIR/Payload/*.app
if [ $? -ne 0 ]; then
echo "ERROR: Cannot resign with certificate '$CERTIFICATE'" >&2
exit 1
fi
# zip it back up
resigned_ipa="$MYDIR"/${IPA%.ipa}-resigned.ipa
(cd $WORKDIR && zip -qr $resigned_ipa Payload)
if [ $? -ne 0 ]; then
echo "ERROR: Cannot create resigned ipa" >&2
exit 1
fi
rm -rf $WORKDIR
echo "SUCCESS: Resigned ipa is successfully out to $resigned_ipa" >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment