exorcist-evangeline/gameupdate/patch.sh
2025-10-10 13:42:43 -05:00

96 lines
2.6 KiB
Bash

#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(pwd)"
CONFIG_FILE="$SCRIPT_DIR/patch-config.txt"
check_dependency() {
if ! command -v "$1" > /dev/null 2>&1; then
echo "Error: '$1' is not installed. Please install it using 'pkg install $1'."
exit 1
fi
}
# Check for jq, unzip, and curl
check_dependency jq
check_dependency unzip
check_dependency curl
# Check if CONFIG_FILE exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file '$CONFIG_FILE' not found! Assuming no patching needed."
exit 0
fi
# Convert line endings to Unix format
sed -i 's/\r$//' "$CONFIG_FILE"
# Debug information
echo "Root directory: $ROOT_DIR"
echo "Config file path: $CONFIG_FILE"
# Read configuration from file
. "$CONFIG_FILE"
# Get the latest hash
echo "Getting latest commit SHA hash"
latest_patch_sha=$(curl -s "https://gitgud.io/api/v4/projects/$username%2F$repo/repository/branches/$branch" | jq -r '.commit.id')
download_extract() {
# Download zip file into root
echo "Downloading latest patch..."
curl -sL "https://gitgud.io/$username/$repo/-/archive/$branch/$repo-$branch.zip" -o "$ROOT_DIR/repo.zip"
if [ $? -ne 0 ]; then
echo "Download failed!"
rm -f "$ROOT_DIR/repo.zip"
rm -rf "$ROOT_DIR/$repo-$branch"
return 1
fi
# Extract contents, overwriting conflicts into root
echo "Extracting..."
unzip -qo "$ROOT_DIR/repo.zip" -d "$ROOT_DIR"
if [ $? -ne 0 ]; then
echo "Extraction failed!"
rm -f "$ROOT_DIR/repo.zip"
rm -rf "$ROOT_DIR/$repo-$branch"
return 1
fi
echo "Applying patch..."
cp -r "$ROOT_DIR/$repo-$branch/"* "$ROOT_DIR/"
if [ $? -ne 0 ]; then
echo "Patch application failed!"
rm -f "$ROOT_DIR/repo.zip"
rm -rf "$ROOT_DIR/$repo-$branch"
return 1
fi
echo "Cleaning up..."
rm -f "$ROOT_DIR/repo.zip"
rm -rf "$ROOT_DIR/$repo-$branch"
rm -f "$ROOT_DIR/latest_patch_sha.txt"
# Store latest SHA for next check in gameupdate
echo "$latest_patch_sha" > "$SCRIPT_DIR/previous_patch_sha.txt"
}
# Check if previous_patch_sha.txt exists in gameupdate
if [ ! -f "$SCRIPT_DIR/previous_patch_sha.txt" ]; then
echo "Previous SHA hash not found!"
echo "Assuming first time patching..."
download_extract
else
# Read the stored SHA from previous check
previous_patch_sha=$(cat "$SCRIPT_DIR/previous_patch_sha.txt")
# Compare trimmed SHAs
if [ "$latest_patch_sha" != "$previous_patch_sha" ]; then
echo "Update found! Patching..."
download_extract
else
echo "Patch is up to date."
fi
fi