If wget couldn't download a file from the provided url, the script would continue anyway and jq would write over modrinth.index.json with an empty file
72 lines
1.7 KiB
Bash
Executable file
72 lines
1.7 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# User input error handling
|
|
if [[ $# -eq 0 ]]; then
|
|
echo "$0: A mod url is required."
|
|
exit 4
|
|
fi
|
|
|
|
# Do everything relative to this files path
|
|
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
|
|
cd "$parent_path"
|
|
|
|
# Paths
|
|
mods_dir="/tmp/"
|
|
|
|
# Options parsing
|
|
mod_url="$1"
|
|
out_file="${2:-$parent_path/modrinth.index.json}"
|
|
|
|
if [ -n "$v" ]; then
|
|
echo "Verbose: $v, Mod url: $mod_url, Output: $out_file"
|
|
fi
|
|
|
|
# https://stackoverflow.com/questions/6250698/how-to-decode-url-encoded-string-in-shell#37840948
|
|
function urldecode() {
|
|
: "${*//+/ }"
|
|
echo -e "${_//%/\\x}"
|
|
}
|
|
|
|
# Get file name from url trimmed by slash and url decoded
|
|
mod_file="$(urldecode ${mod_url##*/})"
|
|
trap "rm \"${mods_dir}/${mod_file}\"" HUP INT TERM EXIT
|
|
echo "Downloading file..."
|
|
wget -N --no-verbose --directory-prefix "$mods_dir" "$mod_url"
|
|
|
|
# Exit if download failed
|
|
if [ $? != "0" ]; then
|
|
echo "$0: Could not download mod from provided URL"
|
|
trap "" HUP INT TERM EXIT # Unset trap as there's no file to delete
|
|
exit 1
|
|
fi
|
|
|
|
# Collect data required by modrinth.index.json
|
|
echo "Processing file info"
|
|
mod_path="${mods_dir}/${mod_file}"
|
|
|
|
modrinth_size="$(du --bytes "$mod_path" | cut -f -1)"
|
|
modrinth_sha1="$(sha1sum "$mod_path" | cut -d " " -f 1)"
|
|
modrinth_sha512="$(sha512sum "$mod_path" | cut -d " " -f -1)"
|
|
|
|
# Writing to file
|
|
echo "Writing to file"
|
|
jq ".files += [{ \
|
|
\"downloads\": [
|
|
\"${mod_url}\"
|
|
],
|
|
\"env\": {
|
|
\"client\": \"required\",
|
|
\"server\": \"required\"
|
|
},
|
|
\"fileSize\": ${modrinth_size},
|
|
\"hashes\": {
|
|
\"sha1\": \"${modrinth_sha1}\",
|
|
\"sha512\": \"${modrinth_sha512}\"
|
|
},
|
|
\"path\": \"mods/${mod_file}\"
|
|
}]" "$out_file" > "$out_file.tmp"
|
|
|
|
mv "$out_file.tmp" "$out_file"
|
|
|
|
[ $? -eq 0 ] && echo "Successfully appended mod to ${out_file}"
|