forked from zopanix/docker_factorio_server
-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathupdate-mods.sh
More file actions
executable file
·94 lines (73 loc) · 2.15 KB
/
Copy pathupdate-mods.sh
File metadata and controls
executable file
·94 lines (73 loc) · 2.15 KB
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
#!/bin/sh -x
FACTORIO_VERSION=$1
MOD_DIR=$2
USERNAME=$3
TOKEN=$4
MOD_BASE_URL="https://mods.factorio.com"
print_step()
{
echo "$1"
}
print_success()
{
echo "$1"
}
print_failure()
{
echo "$1"
}
update_mod()
{
MOD_NAME="$1"
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")
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 [ -z "$MOD_URL" ]; then
return 1
fi
if [ "$MOD_FILENAME" = "null" ]; then
print_failure " Not compatible with version"
return 1
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" -ne 200 ]; then
print_failure " Download failed: Code $HTTP_STATUS."
rm "$MOD_DIR/$MOD_FILENAME"
return 1
fi
if [ ! -f "$MOD_DIR/$MOD_FILENAME" ]; then
print_failure " Downloaded file missing!"
return 1
fi
set -- "$(sha1sum "$MOD_DIR/$MOD_FILENAME")"
if [ "$1" != "$MOD_SHA1" ]; then
print_failure " SHA1 mismatch!"
rm "$MOD_DIR/$MOD_FILENAME"
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"
rm "$file"
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