forked from Rycieos/factorio-leaflet-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorio-map-process.sh
executable file
·150 lines (121 loc) · 3.62 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
149
150
#!/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 ! [ -x "$(command -v jq)" ]; then
echo "ERROR: Requirement not satisfied - jq -"
exit 1
fi
if [[ REDUCE -eq 1 && !(-x "$(command -v rdfind)") ]]; then
echo "ERROR: Requirement not satisfied - rdfind -"
exit 1
fi
}
function parseFileName() {
NAME_NOEXT="${FILENAME%.*}"
echo "Target File: ${FILENAME}"
local IFS='\n'
infos=($(awk -F'[_.]' '{print $1; print $2}' <<< "${NAME_NOEXT}"))
if [[ ${#infos[@]} -ge 2 ]]; then
WORLDNAME="${infos[0]}"
echo "WORLD NAME $WORLDNAME"
DATESTR=${infos[1]}
echo "DATE STRING $DATESTR"
else
WORLDNAME=""
DATESTR=${infos[0]}
echo "DATE STRING $DATESTR"
fi
DESTPATH=$SERVER_BASE_PATH/$WORLDNAME/$MAP_TILES_PATH/$DATESTR/
}
function createNewWorld() {
## Test for existing world folder
if [[ ! -d $SERVER_BASE_PATH/$WORLDNAME ]]; then
## Create New world folder
mkdir -p $SERVER_BASE_PATH/$WORLDNAME/$MAP_TILES_PATH
cp example.html $SERVER_BASE_PATH/$WORLDNAME/index.html
fi
}
function processTiles() {
echo "DESTINATION $DESTPATH"
mkdir $DESTPATH
$FACTORIO_MAP_SCRIPT $TARFILE $DESTPATH
if [ $REDUCE -eq 1 ]; then
rdfind -makehardlinks true "${SERVER_BASE_PATH}/${WORLDNAME}/${MAP_TILES_PATH}"
fi
}
function addDatesJSON() {
DATEFILE="${SERVER_BASE_PATH}/${WORLDNAME}/dates.json"
if [[ -f $DATEFILE ]]; then
TMPFILE="$(mktemp)"
jq ". += [\"${DATESTR}\"]" "${DATEFILE}" > "${TMPFILE}" && cat "${TMPFILE}" > "${DATEFILE}"
rm "${TMPFILE}"
else
jq <<< "[\"${DATESTR}\"]" > "${DATEFILE}"
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"
echo "Using MAP Path $MAP_TILES_PATH"
shift 2
;;
-s|--server)
SERVER_BASE_PATH="$2"
echo "Using Server Path $SERVER_BASE_PATH"
shift 2
;;
*)
TARFILE=$1
echo "TARFILE $TARFILE"
if ! tar tf "$TARFILE" >/dev/null 2>&1; then
echo "ERROR: File is not a tar archive"
usage;
exit 0
fi
FILENAME=$(basename "$TARFILE")
shift
;;
esac
done
main;