forked from SenorPez/factorio-leaflet-maps
-
Notifications
You must be signed in to change notification settings - Fork 1
/
factorio-map-process.sh
executable file
·148 lines (116 loc) · 3.71 KB
/
factorio-map-process.sh
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
#!/usr/bin/env bash
set -euo pipefail
## Defaults
SERVER_BASE_PATH=/srv/factorio/maps
MAP_TILES_PATH=images
FACTORIO_MAP_SCRIPT="$(dirname "${BASH_SOURCE[0]}")/factoriomap.py"
TARFILE=
REDUCE=0
function usage() {
cat <<EOF
USAGE
factorio-map-process.sh [OPTIONS] tarfile
DESCRIPTION
This script will process a given tarfile into a set of images used in a leaflet map.
The default behavior will create a directory for the world maps if one does not exist and copy the example html page in as 'index.html'
The tar file will be parsed for a map name and a date, separated by underscores (_). Files should be of the format {WorldName}_YYYY-MM-DD.tar.
OPTIONS
-t | --tiles <images> Set the name of the directory to store the tile images in. Default value is
'images'
-s | --server <server path> Set the base path where the http server files will be stored. Default value is
'/srv/factorio/maps'
-r Flag to size-reduce the images folder with the rdfind tool
EOF
}
function testRequirements() {
if ! command -v jq >/dev/null; then
echo "ERROR: Requirement not satisfied - jq -" >&2
exit 1
fi
if [[ $REDUCE -eq 1 ]] && ! command -v rdfind >/dev/null; then
echo "ERROR: Requirement not satisfied - rdfind -" >&2
exit 1
fi
}
function parseFileName() {
printf 'Target File: "%s"\n' "$FILENAME"
local file_name=${FILENAME%.*}
date_string=${file_name##*_}
printf 'Date String: "%s"\n' "$date_string"
local world_name=${file_name%_*}
if [[ $date_string != $world_name ]]; then
printf 'World Name: "%s"\n' "$world_name"
SERVER_BASE_PATH="${SERVER_BASE_PATH}/${world_name}"
fi
DESTPATH="${SERVER_BASE_PATH}/${MAP_TILES_PATH}/${date_string}/"
}
function createNewWorld() {
## Test for existing world folder
if [[ ! -d ${SERVER_BASE_PATH}/${MAP_TILES_PATH} ]]; then
## Create New world folder
mkdir -p "${SERVER_BASE_PATH}/${MAP_TILES_PATH}"
cp example.html "${SERVER_BASE_PATH}/index.html"
fi
}
function processTiles() {
printf 'Destination: "%s"\n' "$DESTPATH"
mkdir "$DESTPATH"
"$FACTORIO_MAP_SCRIPT" "$TARFILE" "$DESTPATH"
if [[ $REDUCE -eq 1 ]]; then
rdfind -makehardlinks true "${SERVER_BASE_PATH}/${MAP_TILES_PATH}"
fi
}
function addDatesJSON() {
local date_file="${SERVER_BASE_PATH}/dates.json"
if [[ -f $date_file ]]; then
local temp_file="$(mktemp)"
jq ". += [\"${date_string}\"]" "$date_file" > "$temp_file" && cat "$temp_file" > "$date_file"
rm "${temp_file}"
else
jq '.' <<< "[\"${date_string}\"]" > "$date_file"
fi
}
function main() {
testRequirements
parseFileName
createNewWorld
processTiles
addDatesJSON
echo "Success"
exit 0
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
# Print Usage
usage
exit 0
;;
-r)
REDUCE=1
shift
;;
-t|--tiles)
MAP_TILES_PATH="${2%/}"
printf 'Using MAP Path: "%s"\n' "$MAP_TILES_PATH"
shift 2
;;
-s|--server)
SERVER_BASE_PATH="${2%/}"
printf 'Using Server Path: "%s"\n' "$SERVER_BASE_PATH"
shift 2
;;
*)
TARFILE=$1
printf 'Tarfile: "%s"\n' "$TARFILE"
if ! tar tf "$TARFILE" >/dev/null 2>&1; then
echo "ERROR: File is not a tar archive" >&2
usage
exit 2
fi
FILENAME=$(basename "$TARFILE")
shift
;;
esac
done
main