Skip to content

Instantly share code, notes, and snippets.

@refola

refola/update-unciv.sh

Last active Nov 18, 2020
Embed
What would you like to do?
#!/usr/bin/bash
# update-unciv.sh
# update ./unciv/PKGBUILD to the given version
# License: WTFPL v2 "You just DO WHAT THE FUCK YOU WANT TO."
echo "Flags: ($*)"
# Check that everything's okay to run this
if ! cd "$(dirname "$(readlink -f "$0")")/unciv"; then
echo "Could not 'cd' to package's directory."
exit 1
fi
echo "cd'd to '$PWD'"
PKGREL=1
PATCH=
if [ "$#" = "2" ]; then
PATCH="$2"
((PKGREL+=PATCH))
set "$1"
echo "PATCH=$PATCH"
fi
if [ "$#" != "1" ]; then
echo "$0 newversion [patch]"
echo "Updates the PKGBUILD to the new version, removes old downloads, and builds."
echo "With '-' as the new version, rebuild for the old version."
exit 1
fi
echo "enough args"
NEW="$1"
echo "NEW=$NEW"
NEW_FULL="$NEW${PATCH:+-patch$PATCH}"
echo "NEW_FULL=$NEW_FULL"
echo "PKGREL=$PKGREL"
OLD="$(grep -E "pkgver=[0-9.]+" PKGBUILD | cut -c8-)"
echo "OLD=$OLD"
OLD_PATCH="$(grep -E "_patch=[0-9.]*" PKGBUILD | cut -c8-)"
OLD_FULL="$OLD${OLD_PATCH:+-patch$OLD_PATCH}"
echo "OLD_FULL=$OLD_FULL"
if [ "$OLD_FULL" = "$NEW_FULL" ]; then
echo "Already at version $OLD. Use '-' to keep the same version."
exit 1
fi
echo "tested OLD vs NEW"
if [ "$NEW_FULL" = "-" ]; then
NEW_FULL="$OLD_FULL"
NEW="$OLD"
NEW_PATCH="$OLD_PATCH"
fi
# Save source old files, and recover new ones if previously saved.
echo "Shuffling current files as applicable..."
FILES=(Unciv.jar
Unciv%20icon%20v3%20for%20google%20play.png
linuxFilesForJar.zip)
mkdir -p old
for f in "${FILES[@]}"; do
if [ -e "$f" ]; then
mv "$f" "old/$f.$OLD_FULL"
echo "Moved '$f' to 'old/$f.$OLD_FULL'"
fi
if [ -e "old/$f.$NEW_FULL" ]; then
echo "Using file at 'old/$f.$NEW_FULL'."
mv "old/$f.$NEW_FULL" "$f"
else
echo "No file at 'old/$f.$NEW_FULL'."
fi
done
# Remove previously generated source and package files
rm ./*.src.tar.gz ./*.pkg.tar
# Switch PKGBUILD to new version
echo "Updating version in PKGBUILD..."
sed -r "s/^pkgver=$OLD$/pkgver=$NEW/g" -i ./PKGBUILD
sed -r "s/[0-9a-f]{64,}/SKIP/g" -i ./PKGBUILD
if [ -n "$PATCH" ]; then
echo "Setting patch version $PATCH."
sed -r "s/^#?_patch=[0-9]*/_patch=$PATCH/g" -i ./PKGBUILD
else
echo "Setting nil patch version."
sed -r "s/^#?_patch=[0-9]*/#_patch=/g" -i ./PKGBUILD
fi
sed -r "s/^pkgrel=[0-9.]+$/pkgrel=$PKGREL/g" -i ./PKGBUILD
# Download and update checksums for new version
echo "Getting sources..."
makepkg --nobuild
echo "Updating checksums..."
makepkg --geninteg | grep -Eo '[0-9a-f]{64,}' |
while read -r; do
# Per https://stackoverflow.com/a/33416489, this GNU sed
# command matches the find pattern ('SKIP') from line 0 or
# later, uses it in an "s/find/replace/" expression (while
# reusing the find pattern with "//"), and replaces it with
# the replace pattern ('$REPLY').
sed -r "0,/'SKIP'/ s//'$REPLY'/" -i ./PKGBUILD
done
# Make source tarball to upload to the CCR and local package file to install
echo "Making source tarball"
makepkg --source
echo "Making package file"
makepkg
# Clean up packaging directories
rm -r ./pkg ./src
echo "Done! ^.^"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.