-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-spiget
executable file
·151 lines (124 loc) · 4.04 KB
/
start-spiget
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# shellcheck source=start-utils
. "${SCRIPTS:-/}start-utils"
handleDebugMode
: "${SPIGET_RESOURCES:=}"
: "${SPIGET_DOWNLOAD_TOLERANCE:=5}" # in minutes
: "${REMOVE_OLD_MODS_INCLUDE:=*.jar,*-version.json}"
acceptArgs=(--accept application/zip --accept application/java-archive --accept application/octet-stream)
containsJars() {
file=${1?}
pat='\.jar$'
while read -r line; do
if [[ $line =~ $pat ]]; then
return 0
fi
done < <(unzip -l "$file" | tail -n +4)
return 1
}
containsPlugin() {
file=${1?}
pat='plugin.yml$'
while read -r line; do
if [[ $line =~ $pat ]]; then
return 0
fi
done < <(unzip -l "$file" | tail -n +4)
return 1
}
getResourceFromSpiget() {
resource=${1?}
log "Downloading resource ${resource} ..."
mkdir -p /data/plugins
versionfile="/data/plugins/.${resource}-version.json"
versionfileNew="${versionfile}.tmp"
if [ -f "$versionfile" ]; then
if [[ -n $(find "$versionfile" -mmin +${SPIGET_DOWNLOAD_TOLERANCE}) ]]; then
urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest"
if ! get -o "${versionfileNew}" "${urlVersion}"; then
log "ERROR failed to download resource version meta data '${resource}' from ${urlVersion}"
exit 2
fi
if isTrue "${REMOVE_OLD_MODS:-false}"; then
installedVersion=0.0.0
else
installedVersion=$(jq -r '.name' $versionfile)
fi
newVersion=$(jq -r '.name' $versionfileNew)
if [ "$installedVersion" = "$newVersion" ]; then
log "Resource '${resource}': installed version '${installedVersion}' already up to date"
mv "${versionfileNew}" "${versionfile}"
else
if downloadResourceFromSpiget "${resource}"; then
mv "${versionfileNew}" "${versionfile}"
fi
fi
else
log "resource '${resource}' not checked because version meta file newer than '${SPIGET_DOWNLOAD_TOLERANCE}' minutes"
fi
else
if downloadResourceFromSpiget "${resource}"; then
urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest"
if ! get -o "${versionfileNew}" "${urlVersion}"; then
log "ERROR failed to download resource version meta data '${resource}' from ${urlVersion}"
exit 2
fi
mv "${versionfileNew}" "${versionfile}"
fi
fi
}
downloadResourceFromSpiget() {
resource=${1?}
tempDir="/data/plugins/tmp-${resource}"
mkdir -p $tempDir
resourceUrl="https://api.spiget.org/v2/resources/${resource}"
if ! outfile=$(get_silent --output-filename -o $tempDir "${acceptArgs[@]}" "${resourceUrl}/download"); then
log "ERROR: failed to download resource '${resource}' from ${resourceUrl}/download"
if externalUrl=$(get --json-path '$.file.externalUrl' "${resourceUrl}"); then
log " Visit $externalUrl to pre-download the resource"
log " instead of using SPIGET_RESOURCES"
fi
exit 1
fi
if ! fileType=$(get --json-path '.file.type' "${resourceUrl}"); then
log "ERROR: failed to retrieve file type of resource $resource"
exit 1
fi
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
log "ERROR: file for resource ${resource} has an unexpected file type: ${fileType}"
exit 2
fi
rm -rf "$tempDir"
}
if [[ ${SPIGET_RESOURCES} ]]; then
if isTrue "${REMOVE_OLD_MODS:-false}"; then
removeOldMods /data/plugins
REMOVE_OLD_MODS=false
fi
log "Getting plugins via Spiget"
IFS=',' read -r -a resources <<<"${SPIGET_RESOURCES}"
for resource in "${resources[@]}"; do
getResourceFromSpiget "${resource}" &
done
FAIL=0
for job in `jobs -p`
do
wait $job || let "FAIL+=1"
done
if [ "$FAIL" == "0" ];
then
echo "Successfully downloaded all mods!"
else
echo "Mod download failed!"
exit 1
fi
fi
exec "${SCRIPTS:-/}start-setupWorld" "$@"