2018-01-07 23:18:45 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function join_by() {
|
|
|
|
local d=$1
|
|
|
|
shift
|
|
|
|
echo -n "$1"
|
|
|
|
shift
|
|
|
|
printf "%s" "${@/#/$d}"
|
|
|
|
}
|
2020-07-18 23:31:29 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function isURL() {
|
2018-01-07 23:18:45 +00:00
|
|
|
local value=$1
|
|
|
|
|
2021-04-28 21:44:39 +00:00
|
|
|
if [[ ${value:0:8} == "https://" || ${value:0:7} == "http://" || ${value:0:6} == "ftp://" ]]; then
|
2018-01-07 23:18:45 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function isValidFileURL() {
|
|
|
|
suffix=${1:?Missing required suffix arg}
|
|
|
|
url=${2:?Missing required url arg}
|
|
|
|
|
|
|
|
[[ "$url" == http*://*.${suffix} || "$url" == http*://*.${suffix}\?* ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveEffectiveUrl() {
|
|
|
|
url="${1:?Missing required url argument}"
|
|
|
|
if ! curl -Ls -o /dev/null -w %{url_effective} "$url"; then
|
|
|
|
log "ERROR failed to resolve effective URL from $url"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilenameFromUrl() {
|
|
|
|
url="${1:?Missing required url argument}"
|
|
|
|
strippedOfQuery="${url%\?*}"
|
|
|
|
basename "$strippedOfQuery"
|
|
|
|
}
|
|
|
|
|
|
|
|
function isTrue() {
|
2018-01-07 23:18:45 +00:00
|
|
|
local value=${1,,}
|
|
|
|
|
|
|
|
result=
|
|
|
|
|
|
|
|
case ${value} in
|
2020-12-12 21:22:07 +00:00
|
|
|
true | on)
|
|
|
|
result=0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
result=1
|
|
|
|
;;
|
2018-01-07 23:18:45 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
return ${result}
|
2019-02-03 15:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function isDebugging() {
|
2021-03-21 16:43:21 +00:00
|
|
|
if isTrue "${DEBUG:-false}"; then
|
2019-02-03 15:49:59 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-03-21 16:43:21 +00:00
|
|
|
function handleDebugMode() {
|
|
|
|
if isDebugging; then
|
|
|
|
set -x
|
|
|
|
extraCurlArgs=(-v)
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function debug() {
|
2019-02-03 15:49:59 +00:00
|
|
|
if isDebugging; then
|
2020-03-06 15:52:17 +00:00
|
|
|
log "DEBUG: $*"
|
2019-02-03 15:49:59 +00:00
|
|
|
fi
|
2019-11-21 19:50:06 +00:00
|
|
|
}
|
2020-03-06 15:52:17 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function logn() {
|
2020-03-06 15:52:17 +00:00
|
|
|
echo -n "[init] $*"
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function log() {
|
2020-03-06 15:52:17 +00:00
|
|
|
echo "[init] $*"
|
|
|
|
}
|
2020-04-11 15:41:20 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function logAutopause() {
|
2020-05-23 14:15:10 +00:00
|
|
|
echo "[Autopause loop] $*"
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function logAutopauseAction() {
|
2020-05-23 14:15:10 +00:00
|
|
|
echo "[$(date -Iseconds)] [Autopause] $*"
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function normalizeMemSize() {
|
2020-04-11 15:41:20 +00:00
|
|
|
local scale=1
|
|
|
|
case ${1,,} in
|
2020-12-12 21:22:07 +00:00
|
|
|
*k)
|
|
|
|
scale=1024
|
|
|
|
;;
|
|
|
|
*m)
|
|
|
|
scale=1048576
|
|
|
|
;;
|
|
|
|
*g)
|
|
|
|
scale=1073741824
|
|
|
|
;;
|
2020-04-11 15:41:20 +00:00
|
|
|
esac
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
val=${1:0:-1}
|
|
|
|
echo $((val * scale))
|
2020-04-11 15:41:20 +00:00
|
|
|
}
|
2020-04-13 23:29:44 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
function versionLessThan() {
|
2020-04-13 23:29:44 +00:00
|
|
|
local activeParts
|
2020-12-12 21:22:07 +00:00
|
|
|
IFS=. read -ra activeParts <<<"${VANILLA_VERSION}"
|
2020-04-13 23:29:44 +00:00
|
|
|
|
|
|
|
local givenParts
|
2020-12-12 21:22:07 +00:00
|
|
|
IFS=. read -ra givenParts <<<"$1"
|
2020-04-13 23:29:44 +00:00
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
if ((${#activeParts[@]} < 2)); then
|
2020-04-18 02:15:44 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2020-12-12 21:22:07 +00:00
|
|
|
if ((${#activeParts[@]} == 2)); then
|
|
|
|
if ((activeParts[0] < givenParts[0])) ||
|
|
|
|
((activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1])); then
|
2020-05-20 13:17:58 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
2020-04-13 23:29:44 +00:00
|
|
|
else
|
2020-12-12 21:22:07 +00:00
|
|
|
if ((activeParts[0] < givenParts[0])) ||
|
|
|
|
((activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1])) ||
|
|
|
|
((activeParts[0] == givenParts[0] && activeParts[1] == givenParts[1] && activeParts[2] < givenParts[2])); then
|
2020-05-20 13:17:58 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
2020-04-13 23:29:44 +00:00
|
|
|
fi
|
2020-06-19 16:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
requireVar() {
|
|
|
|
if [ ! -v $1 ]; then
|
|
|
|
log "ERROR: $1 is required to be set"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-07-19 20:01:19 +00:00
|
|
|
if [ -z "${!1}" ]; then
|
|
|
|
log "ERROR: $1 is required to be set"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-06-19 16:31:56 +00:00
|
|
|
}
|
2020-07-26 18:19:45 +00:00
|
|
|
|
2021-02-09 02:42:54 +00:00
|
|
|
requireEnum() {
|
|
|
|
var=${1?}
|
|
|
|
shift
|
|
|
|
|
|
|
|
for allowed in $*; do
|
|
|
|
if [[ ${!var} = $allowed ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
log "ERROR: $var must be set to one of $@"
|
|
|
|
# exit 1
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:19:45 +00:00
|
|
|
function writeEula() {
|
2021-01-20 03:36:21 +00:00
|
|
|
if ! echo "# Generated via Docker
|
|
|
|
# $(date)
|
2020-07-26 18:19:45 +00:00
|
|
|
eula=${EULA,,}
|
2020-12-12 21:22:07 +00:00
|
|
|
" >/data/eula.txt; then
|
|
|
|
log "ERROR: unable to write eula to /data. Please make sure attached directory is writable by uid=${UID}"
|
|
|
|
exit 2
|
|
|
|
fi
|
2020-07-26 18:19:45 +00:00
|
|
|
}
|
2021-04-28 21:29:47 +00:00
|
|
|
|
|
|
|
function removeOldMods {
|
|
|
|
if [ -d "$1" ]; then
|
2021-05-15 17:39:48 +00:00
|
|
|
find "$1" -mindepth 1 -maxdepth ${REMOVE_OLD_MODS_DEPTH:-16} -wholename "${REMOVE_OLD_MODS_INCLUDE:-*}" -not -wholename "${REMOVE_OLD_MODS_EXCLUDE:-}" -delete
|
2021-04-28 21:29:47 +00:00
|
|
|
fi
|
|
|
|
}
|
2021-10-09 20:22:42 +00:00
|
|
|
|
|
|
|
function get() {
|
2021-10-10 14:53:52 +00:00
|
|
|
local flags=()
|
|
|
|
if isDebugging; then
|
|
|
|
flags+=("--debug")
|
|
|
|
fi
|
|
|
|
mc-image-helper "${flags[@]}" get "$@"
|
2021-10-09 20:22:42 +00:00
|
|
|
}
|