This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-minecraft-server
executable file
·114 lines (97 loc) · 2.75 KB
/
create-minecraft-server
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
#!/bin/bash
#
# Simple script to create a minecraft server using a zip of server files
# for a specific server name.
# The new directory will contain the server files and belong to the minecraft
# group.
# Will also create the init script for a given server.
#
script_name=`basename $0`
server_name=$2
server_location=$3
server_dir=/home/minecraft/$server_name
escaped_server_dir=`echo $server_dir | sed s/\\\\\//\\\\\\\\\\\//g`
minecraft_user=`grep minecraft /etc/passwd | cut -d: -f1`
url_regex="^(http:|https:)\/\/.?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"
check_server_name() {
if [ -z "$server_name" ]; then
echo "Did not specify <server-name>."
show_usage
exit 1
fi
}
check_server_location() {
if [ -z "$server_location" ]; then
echo "Did not specify <server-zip-location>."
show_usage
exit 1
fi
}
create() {
check_server_name; check_server_location;
if [[ ! $server_location =~ $url_regex ]]; then
echo
echo "<server-zip-location> is not a URL"
echo
exit 1
fi
mkdir tmp
wget -O tmp/$server_name.zip $server_location
unzip tmp/$server_name.zip -d $server_dir
echo "(Requires sudo)"
sudo chgrp -R minecraft $server_name
sudo chmod -R g+w $server_name
rm -rf tmp
}
init_script() {
check_server_name;
# using fhd's System V init template for a starter script
template_url=https://raw.githubusercontent.com/fhd/init-script-template/master/template
mkdir tmp
wget -O tmp/init-template $template_url
# Run replacements for directory, command, and user for the init script
sed s/dir=\"\"/dir=\"$escaped_server_dir\"/ tmp/init-template > tmp/init-template2
sed s/cmd=\"\"/cmd=\"\"/ tmp/init-template2 > tmp/init-template3
sed s/user=\"\"/user=\"minecraft\"/ tmp/init-template3 > tmp/init-template4
sudo cp tmp/init-template4 /etc/init.d/$server_name
rm -rf tmp
echo
echo "****************************************"
echo "* Init script has been written *"
echo "* Use: sudo service $server_name start *"
echo "* for testing. *"
echo "****************************************"
echo
}
show_usage() {
echo
echo "Usage:"
echo " $script_name create <server-name> <server-zip-location>"
echo " $script_name init-script <server-name>"
echo
}
if [ -z "$minecraft_user" ]; then
echo
echo "********************************************************"
echo "* Cannot run script without minecraft user. *"
echo "* Please run 'create-minecraft-user' script first. *"
echo "********************************************************"
echo
# exit 2
fi
# Process command line options
case "$1" in
create)
create;
;;
init-script)
init_script;
;;
*)
show_usage;
;;
esac
echo
echo "Done."
echo
exit 0