forked from TapeWerm/MCscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcbe_update.sh
executable file
·75 lines (67 loc) · 1.9 KB
/
mcbe_update.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
#!/usr/bin/env bash
# Exit if error
set -e
syntax='Usage: mcbe_update.sh SERVER_DIR MINECRAFT_ZIP`'
case $1 in
--help|-h)
echo "$syntax"
echo Update Minecraft Bedrock Edition server keeping packs, worlds, JSON files, and PROPERTIES files. Other files will be removed. You can convert a Windows SERVER_DIR to Ubuntu and vice versa if you convert line endings.
echo
echo MINECRAFT_ZIP cannot be in SERVER_DIR. Remember to stop server before updating.
exit
;;
esac
if [ "$#" -lt 2 ]; then
>&2 echo Not enough arguments
>&2 echo "$syntax"
exit 1
elif [ "$#" -gt 2 ]; then
>&2 echo Too much arguments
>&2 echo "$syntax"
exit 1
fi
server_dir=$(realpath "$1")
new_dir=$server_dir.new
old_dir=$server_dir.old
minecraft_zip=$(realpath "$2")
if [ -n "$(find "$server_dir" -wholename "$minecraft_zip")" ]; then
>&2 echo MINECRAFT_ZIP cannot be in SERVER_DIR
exit 1
fi
# Test extracting $minecraft_zip partially quietly
unzip -tq "$minecraft_zip"
echo "Enter Y if you backed up and stopped the server you're updating"
read -r input
input=$(echo "$input" | tr '[:upper:]' '[:lower:]')
if [ "$input" != y ]; then
>&2 echo "$input != y"
exit 1
fi
rm -rf "$new_dir"
trap 'rm -rf "$new_dir"; echo fail > "$server_dir/version"' ERR
unzip -q "$minecraft_zip" -d "$new_dir"
cd "$new_dir"
# Trim off $minecraft_zip after last .zip
basename "${minecraft_zip%.zip}" > version
for pack_dir in *_packs; do
if [ -d "$server_dir/$pack_dir" ]; then
packs=$(ls "$server_dir/$pack_dir")
# Escape \ while reading line from $packs
echo "$packs" | while read -r pack; do
# Don't clobber 1st party packs
if [ ! -d "$pack_dir/$pack" ]; then
cp -r "$server_dir/$pack_dir/$pack" "$pack_dir/"
fi
done
fi
done
for file in worlds *.{json,properties}; do
if [ -e "$server_dir/$file" ]; then
cp -r "$server_dir/$file" .
fi
done
rm -rf "$old_dir"
trap '' SIGTERM
mv "$server_dir" "$old_dir"
mv "$new_dir" "$server_dir"
rm -rf "$old_dir"