Skip to content

Instantly share code, notes, and snippets.

@foopis23
Created March 28, 2025 16:34
Show Gist options
  • Save foopis23/525380797aab9c912cde4e07af695a9a to your computer and use it in GitHub Desktop.
Save foopis23/525380797aab9c912cde4e07af695a9a to your computer and use it in GitHub Desktop.
Half Baked Script To Update Server Mods From Modrinth Modpack
#!/bin/bash
# Define paths
MODS_DIR="./mods"
BACKUP_DIR="./backup"
MRPACK_URL="$1" # Replace with your actual Modrinth .mrpack URL
TEMP_DIR="./temp" # Temporary directory to unzip the .mrpack file
# Create the backup directory if it doesn't exist
rm -rf "$TEMP_DIR" # Remove any existing temp directory
mkdir -p "$BACKUP_DIR"
mkdir -p "$TEMP_DIR"
# Function to backup the existing mods folder
backup_mods() {
if [ ! -d "$MODS_DIR" ]; then
echo "Mods directory $MODS_DIR does not exist. Nothing to backup."
return
fi
# Create a timestamped backup
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
BACKUP_PATH="$BACKUP_DIR/mods_backup_$TIMESTAMP.zip"
# Zip the current mods folder
echo "Backing up mods to $BACKUP_PATH..."
zip -r "$BACKUP_PATH" "$MODS_DIR"
if [ $? -eq 0 ]; then
echo "Backup completed: $BACKUP_PATH"
else
echo "Error: Backup failed."
exit 1
fi
}
# Function to download a file
download_file() {
local url=$1
local dest=$2
echo "Downloading $url to $dest..."
curl -sSL "$url" -o "$dest"
if [ $? -eq 0 ]; then
echo "Downloaded successfully: $dest"
else
echo "Error: Failed to download $url"
exit 1
fi
}
# Function to unzip the .mrpack file
unzip_mrpack() {
local mrpack_url=$1
local temp_dir=$2
echo "Downloading .mrpack file from $mrpack_url..."
local mrpack_file="$temp_dir/modpack.mrpack"
download_file "$mrpack_url" "$mrpack_file"
echo "Unzipping .mrpack file..."
unzip -q "$mrpack_file" -d "$temp_dir"
if [ $? -eq 0 ]; then
echo ".mrpack file unzipped successfully."
else
echo "Error: Failed to unzip .mrpack file."
exit 1
fi
}
# Function to update mods from the Modrinth JSON file
update_server_mods() {
# Unzip the .mrpack file and extract the JSON
unzip_mrpack "$MRPACK_URL" "$TEMP_DIR"
# The JSON file should be inside the unzipped contents
JSON_FILE="$TEMP_DIR/modrinth.index.json"
if [ ! -f "$JSON_FILE" ]; then
echo "Error: modrinth.index.json not found in the .mrpack file."
exit 1
fi
# Fetch mod URLs and paths from the JSON
echo "Parsing modrinth.index.json..."
MOD_URLS=$(jq -r '.files[] | select(.env.server != "unsupported") | .downloads[0]' "$JSON_FILE")
MOD_PATHS=$(jq -r '.files[] | select(.env.server != "unsupported") | .path' "$JSON_FILE")
# Loop through each mod URL and download the corresponding mod file
echo "Updating mods..."
for i in $(seq 0 $(($(echo "$MOD_URLS" | wc -l) - 1))); do
MOD_URL=$(echo "$MOD_URLS" | sed -n "$((i + 1))p")
MOD_PATH=$(echo "$MOD_PATHS" | sed -n "$((i + 1))p")
MOD_SAVE_PATH="$MOD_PATH"
# Ensure the mods directory exists
mkdir -p "$(dirname "$MOD_SAVE_PATH")"
# Download the mod
download_file "$MOD_URL" "$MOD_SAVE_PATH"
done
}
# Main execution flow
echo "Starting mod update process..."
# Step 1: Backup existing mods
backup_mods
# Step 2: Update mods from Modrinth
update_server_mods
# Clean up temporary files
echo "Cleaning up temporary files..."
rm -rf "$TEMP_DIR"
echo "Mod update process completed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment