Skip to content

Instantly share code, notes, and snippets.

@jhead
Last active August 29, 2015 14:06
Show Gist options
  • Save jhead/52323a4e6483c637b2e4 to your computer and use it in GitHub Desktop.
Save jhead/52323a4e6483c637b2e4 to your computer and use it in GitHub Desktop.
Finds latest Spigot binary patch and applies to the specified file
#!/bin/bash
## Validate arguments
if [ "$#" -ne 2 ]; then
echo "Fetches latest Spigot patch and applies it."
echo
echo "Usage: <spigot-1649-jar-path> <output-file>"
exit 1
fi
## Vars
SOURCE=$1
OUTPUT=$2
PATCHER="./SpigotPatcher-1.0.jar"
BASEURL="http://www.spigotmc.org/spigot-updates"
## Spigot patch function
function patch_spigot () {
PATCHFILE=$1
if [ -e $OUTPUT ]; then
echo -n "Output file exists. Removing... "
rm -f $OUTPUT
echo "Done!"
fi
echo "Patching Spigot with $PATCHFILE..."
echo
java -jar $PATCHER $SOURCE $PATCHFILE $OUTPUT
echo
echo "Patching complete!"
}
## Grab latest patch file from directory listing
echo -n "Locating latest Spigot patch... "
LASTPATCH=$(curl -L -s $BASEURL/ | egrep 'spigot-(.*)\.bps' | cut -d '>' -f 2 | cut -d '<' -f 1 | sort | tail -n 1)
ID=$(echo $LASTPATCH | sed 's,\.bps,,g')
URL="$BASEURL/$ID.bps"
## Fetch HTTP status code
STATUS=$(curl -L -s -I $URL | grep HTTP | cut -d ' ' -f 2)
## Check if file exists via HTTP 200
if [ $STATUS -eq 200 ]; then
echo "Found: $ID"
TIMESTAMP=$(date +%s)
FILENAME="$ID-$TIMESTAMP.bps"
echo -n "Fetching checksums... "
SUMS=$(curl -L -s $BASEURL/$ID.md5)
SUM_ORIG=$(echo "$SUMS" | grep -i original | awk '{print $2}')
SUM_PATCH=$(echo "$SUMS" | grep -i patch | awk '{print $2}')
SUM_FINAL=$(echo "$SUMS" | grep -i result | awk '{print $2}')
echo "Done!"
echo -n "Verifying source file... "
TEMPSUM_ORIG=$(md5sum -b $SOURCE | awk '{print $1}')
if [ $TEMPSUM_ORIG != $SUM_ORIG ]; then
echo "Failed: $TEMPSUM_ORIG"
echo "Please ensure that you've provided the correct source version of Spigot (#1649)."
exit 3
else
echo "Success!"
fi
if [ -e $OUTPUT ]; then
echo -n "Output file exists. Checking if patch is necessary... "
TEMPSUM_FINAL=$(md5sum -b $OUTPUT | awk '{print $1}')
if [ $TEMPSUM_FINAL == $SUM_FINAL ]; then
echo "Patch not necessary!"
exit
else
echo "Checksum does not match desired value."
fi
fi
echo -n "Downloading patch file... "
curl -L -s -o ./$FILENAME $URL
echo "Done!"
echo -n "Verifying patch file... "
TEMPSUM_PATCH=$(md5sum -b $FILENAME | awk '{print $1}')
if [ $TEMPSUM_PATCH != $SUM_PATCH ]; then
echo "Failed: got $TEMPSUM_PATCH instead of $SUM_PATCH"
echo "Corrupt download? Try again later."
exit 4
else
echo "Success!"
fi
patch_spigot $FILENAME
rm -f ./$FILENAME
echo -n "Verifying final jar... "
TEMPSUM_FINAL=$(md5sum -b $OUTPUT | awk '{print $1}')
if [ $TEMPSUM_FINAL != $SUM_FINAL ]; then
echo "Failed: $TEMPSUM_FINAL"
echo "Something is wrong with the patch or patcher. Are you up-to-date?"
exit 5
else
echo "Success!"
fi
echo
echo "All done! Patched and updated file here: $OUTPUT"
exit
fi
echo "Received invalid response from server while trying to locate latest patch. Please try again later."
echo "Code: $STATUS"
exit 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment