74 lines
1.9 KiB
Bash
74 lines
1.9 KiB
Bash
#!/bin/bash
|
|
|
|
function check_dependency() {
|
|
if ! command -v "$1" &> /dev/null; then
|
|
echo "Error: '$1' is not installed. Please install it."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# --- Dependency Checks ---
|
|
check_dependency git
|
|
check_dependency awk
|
|
|
|
# --- Configuration ---
|
|
CONFIG_FILE="patch-config.txt"
|
|
SHA_FILE="previous_patch_sha.txt"
|
|
|
|
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 using sed (didn't want to add another dependency in dos2unix)
|
|
sed -i 's/\r$//' "$CONFIG_FILE"
|
|
|
|
source "$CONFIG_FILE"
|
|
|
|
# --- Input Validation ---
|
|
if [[ -z "$username" || -z "$repo" || -z "$branch" ]]; then
|
|
echo "Error: Missing username, repo, or branch in config file ('$CONFIG_FILE')."
|
|
exit 1
|
|
fi
|
|
|
|
# --- Get Latest SHA ---
|
|
echo "Getting latest commit SHA hash..."
|
|
latest_patch_sha=$(git ls-remote "https://github.com/$username/$repo.git" "$branch" | awk '{print $1}')
|
|
|
|
if [[ -z "$latest_patch_sha" ]]; then
|
|
echo "Error: Failed to retrieve latest SHA hash."
|
|
exit 1
|
|
fi
|
|
|
|
# --- Check for Updates ---
|
|
if [ -f "$SHA_FILE" ]; then
|
|
previous_patch_sha=$(cat "$SHA_FILE")
|
|
|
|
if [ "$latest_patch_sha" == "$previous_patch_sha" ]; then
|
|
echo "Patch is up to date."
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
# --- Download and Apply Patch ---
|
|
echo "Update found! Patching..."
|
|
|
|
echo "Cloning the latest patch..."
|
|
git clone -b "$branch" "https://github.com/$username/$repo.git" "patch-$latest_patch_sha"
|
|
|
|
if [ ! -d "patch-$latest_patch_sha" ]; then
|
|
echo "Error cloning the repository!"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Applying patch..."
|
|
rm "patch-$latest_patch_sha/GameUpdate_linux.sh" # rm the script from the patch folder, so it doesn't override the running script.
|
|
cp -rf "patch-$latest_patch_sha/"* .
|
|
|
|
# --- Cleanup ---
|
|
echo "Cleaning up..."
|
|
rm -rf "patch-$latest_patch_sha"
|
|
|
|
# --- Store Latest SHA ---
|
|
echo "$latest_patch_sha" > "$SHA_FILE"
|
|
echo "Patching complete!"
|