2021-03-21 16:43:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
|
2022-03-10 22:16:18 +00:00
|
|
|
# shellcheck source=start-utils
|
|
|
|
. "${SCRIPTS:-/}start-utils"
|
2021-03-21 16:43:21 +00:00
|
|
|
handleDebugMode
|
|
|
|
|
2022-03-10 22:16:18 +00:00
|
|
|
: "${SPIGET_RESOURCES:=}"
|
|
|
|
: "${SPIGET_DOWNLOAD_TOLERANCE:=5}" # in minutes
|
2024-06-12 21:38:41 +00:00
|
|
|
: "${REMOVE_OLD_MODS:=false}"
|
|
|
|
: "${REMOVE_OLD_MODS_EXCLUDE:=}"
|
2022-07-30 02:28:04 +00:00
|
|
|
: "${REMOVE_OLD_MODS_INCLUDE:=*.jar,*-version.json}"
|
2024-06-12 21:38:41 +00:00
|
|
|
: "${REMOVE_OLD_MODS_DEPTH:=1} "
|
2022-03-10 22:16:18 +00:00
|
|
|
|
2022-03-18 03:07:34 +00:00
|
|
|
acceptArgs=(--accept application/zip --accept application/java-archive --accept application/octet-stream)
|
2021-03-21 16:43:21 +00:00
|
|
|
|
|
|
|
containsJars() {
|
|
|
|
file=${1?}
|
|
|
|
|
|
|
|
pat='\.jar$'
|
|
|
|
|
|
|
|
while read -r line; do
|
|
|
|
if [[ $line =~ $pat ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
2022-03-18 03:07:34 +00:00
|
|
|
done < <(unzip -l "$file" | tail -n +4)
|
2021-03-21 16:43:21 +00:00
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-12-20 01:24:02 +00:00
|
|
|
containsPlugin() {
|
|
|
|
file=${1?}
|
|
|
|
|
|
|
|
pat='plugin.yml$'
|
|
|
|
|
|
|
|
while read -r line; do
|
|
|
|
if [[ $line =~ $pat ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
2022-03-18 03:07:34 +00:00
|
|
|
done < <(unzip -l "$file" | tail -n +4)
|
2021-12-20 01:24:02 +00:00
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-03-21 16:43:21 +00:00
|
|
|
getResourceFromSpiget() {
|
|
|
|
resource=${1?}
|
|
|
|
|
|
|
|
log "Downloading resource ${resource} ..."
|
|
|
|
|
|
|
|
mkdir -p /data/plugins
|
2021-07-14 23:42:34 +00:00
|
|
|
|
2021-07-17 23:44:18 +00:00
|
|
|
versionfile="/data/plugins/.${resource}-version.json"
|
2022-05-01 22:56:54 +00:00
|
|
|
versionfileNew="${versionfile}.tmp"
|
2021-07-14 23:42:34 +00:00
|
|
|
|
2021-07-17 23:44:18 +00:00
|
|
|
if [ -f "$versionfile" ]; then
|
|
|
|
if [[ -n $(find "$versionfile" -mmin +${SPIGET_DOWNLOAD_TOLERANCE}) ]]; then
|
|
|
|
urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest"
|
2022-03-10 22:16:18 +00:00
|
|
|
if ! get -o "${versionfileNew}" "${urlVersion}"; then
|
2024-10-22 21:04:38 +00:00
|
|
|
logError "Failed to download resource version meta data '${resource}' from ${urlVersion}"
|
2021-07-17 23:44:18 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2022-10-22 20:04:28 +00:00
|
|
|
if isTrue "${REMOVE_OLD_MODS:-false}"; then
|
|
|
|
installedVersion=0.0.0
|
|
|
|
else
|
|
|
|
installedVersion=$(jq -r '.name' $versionfile)
|
|
|
|
fi
|
2021-07-18 02:11:21 +00:00
|
|
|
newVersion=$(jq -r '.name' $versionfileNew)
|
2021-07-17 23:44:18 +00:00
|
|
|
|
|
|
|
if [ "$installedVersion" = "$newVersion" ]; then
|
2022-11-13 22:48:30 +00:00
|
|
|
log "Resource '${resource}': installed version '${installedVersion}' already up to date"
|
2021-07-17 23:44:18 +00:00
|
|
|
mv "${versionfileNew}" "${versionfile}"
|
|
|
|
else
|
|
|
|
if downloadResourceFromSpiget "${resource}"; then
|
|
|
|
mv "${versionfileNew}" "${versionfile}"
|
|
|
|
fi
|
|
|
|
fi
|
2021-07-14 23:42:34 +00:00
|
|
|
else
|
2021-07-17 23:44:18 +00:00
|
|
|
log "resource '${resource}' not checked because version meta file newer than '${SPIGET_DOWNLOAD_TOLERANCE}' minutes"
|
2021-07-14 23:42:34 +00:00
|
|
|
fi
|
2021-07-17 23:44:18 +00:00
|
|
|
else
|
|
|
|
if downloadResourceFromSpiget "${resource}"; then
|
|
|
|
urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest"
|
2022-03-10 22:16:18 +00:00
|
|
|
if ! get -o "${versionfileNew}" "${urlVersion}"; then
|
2024-10-22 21:04:38 +00:00
|
|
|
logError "Failed to download resource version meta data '${resource}' from ${urlVersion}"
|
2021-07-17 23:44:18 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
mv "${versionfileNew}" "${versionfile}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadResourceFromSpiget() {
|
|
|
|
resource=${1?}
|
|
|
|
|
2022-05-01 22:56:54 +00:00
|
|
|
tempDir="/data/plugins/tmp-${resource}"
|
|
|
|
mkdir -p $tempDir
|
2022-03-10 22:16:18 +00:00
|
|
|
resourceUrl="https://api.spiget.org/v2/resources/${resource}"
|
2022-05-01 22:56:54 +00:00
|
|
|
if ! outfile=$(get_silent --output-filename -o $tempDir "${acceptArgs[@]}" "${resourceUrl}/download"); then
|
2024-10-22 21:04:38 +00:00
|
|
|
logError "Failed to download resource '${resource}' from ${resourceUrl}/download"
|
2022-03-10 22:16:18 +00:00
|
|
|
if externalUrl=$(get --json-path '$.file.externalUrl' "${resourceUrl}"); then
|
2024-10-22 21:04:38 +00:00
|
|
|
logError " Visit $externalUrl to pre-download the resource"
|
|
|
|
logError " instead of using SPIGET_RESOURCES"
|
2022-03-10 22:16:18 +00:00
|
|
|
fi
|
2022-03-15 02:07:06 +00:00
|
|
|
exit 1
|
2021-07-17 23:44:18 +00:00
|
|
|
fi
|
|
|
|
|
2022-03-15 02:07:06 +00:00
|
|
|
if ! fileType=$(get --json-path '.file.type' "${resourceUrl}"); then
|
2024-10-22 21:04:38 +00:00
|
|
|
logError "Failed to retrieve file type of resource $resource"
|
2022-03-15 02:07:06 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2024-04-07 03:00:50 +00:00
|
|
|
if [[ $fileType = .sk ]]; then
|
|
|
|
mkdir -p /data/plugins/Skript/scripts
|
|
|
|
mv "$outfile" /data/plugins/Skript/scripts
|
2021-12-20 01:24:02 +00:00
|
|
|
else
|
2024-04-07 03:00:50 +00:00
|
|
|
if containsPlugin "${outfile}"; then
|
|
|
|
log "Moving resource ${resource} into plugins"
|
|
|
|
mv "$outfile" /data/plugins
|
|
|
|
elif containsJars "${outfile}"; then
|
|
|
|
log "Extracting contents of resource ${resource} into plugins"
|
|
|
|
extract "$outfile" /data/plugins
|
|
|
|
else
|
2024-10-22 21:04:38 +00:00
|
|
|
logError "File for resource ${resource} has an unexpected file type: ${fileType}"
|
2024-04-07 03:00:50 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
2021-03-21 16:43:21 +00:00
|
|
|
fi
|
2022-05-01 22:56:54 +00:00
|
|
|
rm -rf "$tempDir"
|
2021-03-21 16:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [[ ${SPIGET_RESOURCES} ]]; then
|
2024-06-12 21:38:41 +00:00
|
|
|
if isTrue "${REMOVE_OLD_MODS}"; then
|
2021-04-28 21:29:47 +00:00
|
|
|
removeOldMods /data/plugins
|
2021-04-24 19:06:31 +00:00
|
|
|
REMOVE_OLD_MODS=false
|
|
|
|
fi
|
|
|
|
|
2021-03-21 16:43:21 +00:00
|
|
|
log "Getting plugins via Spiget"
|
2021-04-24 19:06:31 +00:00
|
|
|
IFS=',' read -r -a resources <<<"${SPIGET_RESOURCES}"
|
|
|
|
for resource in "${resources[@]}"; do
|
2021-03-21 16:43:21 +00:00
|
|
|
getResourceFromSpiget "${resource}"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2022-03-10 22:16:18 +00:00
|
|
|
exec "${SCRIPTS:-/}start-setupWorld" "$@"
|