2019-07-04 00:34:09 +02:00
|
|
|
#!/bin/bash
|
2019-07-06 16:17:03 +02:00
|
|
|
set -eou pipefail
|
2019-07-04 00:34:09 +02:00
|
|
|
|
|
|
|
FACTORIO_VERSION=$1
|
|
|
|
MOD_DIR=$2
|
|
|
|
USERNAME=$3
|
|
|
|
TOKEN=$4
|
|
|
|
|
|
|
|
MOD_BASE_URL="https://mods.factorio.com"
|
|
|
|
|
|
|
|
print_step()
|
|
|
|
{
|
|
|
|
echo "$1"
|
|
|
|
}
|
|
|
|
|
2019-07-05 09:15:18 +02:00
|
|
|
print_success()
|
2019-07-04 00:34:09 +02:00
|
|
|
{
|
|
|
|
echo "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
print_failure()
|
|
|
|
{
|
|
|
|
echo "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
update_mod()
|
|
|
|
{
|
2019-07-08 17:05:18 +02:00
|
|
|
MOD_NAME="${1// /%20}"
|
2019-07-04 00:34:09 +02:00
|
|
|
|
|
|
|
print_step "Checking for update of mod $MOD_NAME..."
|
|
|
|
|
|
|
|
MOD_INFO_URL="$MOD_BASE_URL/api/mods/$MOD_NAME"
|
|
|
|
MOD_INFO_JSON=$(curl --silent "$MOD_INFO_URL")
|
|
|
|
|
2020-04-05 11:07:18 +02:00
|
|
|
if ! echo "$MOD_INFO_JSON" | jq -e .name >/dev/null; then
|
|
|
|
print_success " Custom mod not on $MOD_BASE_URL, skipped."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2019-07-04 00:34:09 +02:00
|
|
|
MOD_INFO=$(echo "$MOD_INFO_JSON" | jq -j --arg version "$FACTORIO_VERSION" ".releases|reverse|map(select(.info_json.factorio_version as \$mod_version | \$version | startswith(\$mod_version)))[0]|.file_name, \";\", .download_url, \";\", .sha1")
|
|
|
|
|
|
|
|
MOD_FILENAME=$(echo "$MOD_INFO" | cut -f1 -d";")
|
|
|
|
MOD_URL=$(echo "$MOD_INFO" | cut -f2 -d";")
|
|
|
|
MOD_SHA1=$(echo "$MOD_INFO" | cut -f3 -d";")
|
|
|
|
|
|
|
|
if [[ $MOD_FILENAME == null ]]; then
|
|
|
|
print_failure " Not compatible with version"
|
2020-04-05 11:09:01 +02:00
|
|
|
return 0
|
2019-07-04 00:34:09 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f $MOD_DIR/$MOD_FILENAME ]]; then
|
|
|
|
print_success " Already up-to-date."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
print_step "Downloading..."
|
|
|
|
FULL_URL="$MOD_BASE_URL$MOD_URL?username=$USERNAME&token=$TOKEN"
|
|
|
|
HTTP_STATUS=$(curl --silent -L -w "%{http_code}" -o "$MOD_DIR/$MOD_FILENAME" "$FULL_URL")
|
|
|
|
|
|
|
|
if [[ $HTTP_STATUS != 200 ]]; then
|
|
|
|
print_failure " Download failed: Code $HTTP_STATUS."
|
2019-11-04 16:12:24 +01:00
|
|
|
rm -f "$MOD_DIR/$MOD_FILENAME"
|
2019-07-04 00:34:09 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f $MOD_DIR/$MOD_FILENAME ]]; then
|
|
|
|
print_failure " Downloaded file missing!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2019-07-06 16:41:40 +02:00
|
|
|
if ! [[ $(sha1sum "$MOD_DIR/$MOD_FILENAME") =~ $MOD_SHA1 ]]; then
|
2019-07-04 00:34:09 +02:00
|
|
|
print_failure " SHA1 mismatch!"
|
2019-11-04 16:12:24 +01:00
|
|
|
rm -f "$MOD_DIR/$MOD_FILENAME"
|
2019-07-04 00:34:09 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
print_success " Download complete."
|
|
|
|
|
|
|
|
for file in "$MOD_DIR/${MOD_NAME}_"*".zip"; do # wildcard does usually not work in quotes: https://unix.stackexchange.com/a/67761
|
|
|
|
if [[ $file != $MOD_DIR/$MOD_FILENAME ]]; then
|
|
|
|
print_success " Deleting old version: $file"
|
2019-11-04 16:12:24 +01:00
|
|
|
rm -f "$file"
|
2019-07-04 00:34:09 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ -f $MOD_DIR/mod-list.json ]]; then
|
|
|
|
jq -r ".mods|map(select(.enabled))|.[].name" "$MOD_DIR/mod-list.json" | while read -r mod; do
|
|
|
|
if [[ $mod != base ]]; then
|
|
|
|
update_mod "$mod"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|