summaryrefslogtreecommitdiff
path: root/games-server/bedrock-server/files/bedrock-server.initd-r2
blob: 1ff60aa7816fa95c630cc6ec70e9bd7bc7647ce7 (plain)
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
#!/sbin/openrc-run
# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

if [ "${SVCNAME}" = "bedrock-server" ]; then
	instance="main"
else
	instance="${SVCNAME#*.}"
fi

bedrock_command="/opt/bin/bedrock-server"
bedrock_path="/var/lib/bedrock-server"
bedrock_path_data="/opt/bedrock-server"
bedrock_path_instance="${bedrock_path}/${instance}"
dtach_tmpfile="$(mktemp -u)"
name="Minecraft Bedrock server (${instance})"
pidfile="/run/bedrock-server.${instance}.pid"
start_stop_daemon_args="--chdir ${bedrock_path_instance}"

description_attach="Attaches to the session (interactive console) of the Minecraft Bedrock server"
extra_started_commands="attach"

command="/usr/bin/dtach"
command_background="true"
command_args="-N ${dtach_tmpfile} ${bedrock_command}"
command_group="bedrock"
command_user="bedrock"

depend() {
	use net
}

start_pre() {
	checkpath -d -o "${command_user}:${command_group}" -q "${bedrock_path}" "${bedrock_path_instance}"

	local bedrock_configs=( "permissions.json" "server.properties" "whitelist.json" )
	for bedrock_config in ${bedrock_configs[@]}; do
		if [ ! -f "${bedrock_path_instance}/${bedrock_config}" ]; then
			cp "${bedrock_path_data}/${bedrock_config}" "${bedrock_path_instance}"
			checkpath -f -o "${command_user}:${command_group}" -q "${bedrock_path_instance}/${bedrock_config}"
		fi
	done

	local bedrock_ressources=( "behavior_packs" "definitions" "resource_packs" "structures" )
	for bedrock_ressource in ${bedrock_ressources[@]}; do
		if [ ! -L "${bedrock_path_instance}/${bedrock_ressource}" ]; then
			ln -sf "${bedrock_path_data}/${bedrock_ressource}" "${bedrock_path_instance}"
		fi
	done
}

attach() {
	pidnumber="$(cat ${pidfile})"
	dtach_tmpfile="$(cat /proc/${pidnumber}/cmdline | tr '\0' ' ' | awk '{print $3}')"

	if [ -S "${dtach_tmpfile}" ]; then
		eval "${command}" -a "${dtach_tmpfile}" "${DTACH_OPTS}"
	else
		eerror "The determined socket file for dtach could not be found!"
		eerror "Did the process crash?"
	fi
}