source: scripts/usr/local/lib/7dtd/common.sh@ 18

Last change on this file since 18 was 17, checked in by alloc, 10 years ago

Version 4

File size: 4.3 KB
RevLine 
[17]1#!/bin/bash
2# Version 4
3
4# Provides common functions for 7dtd-scripts. Not intended to be run directly.
5
6# Check if the script is run as root (exit otherwise) and load global config
7checkRootLoadConf() {
8 if [ `id -u` -ne 0 ]; then
9 echo "This script has to be run as root!"
10 exit 10
11 fi
12 . /etc/7dtd.conf
13}
14
15# Get the config path for the given instance
16# Params:
17# 1: Instance name
18# Returns:
19# Config path for instance
20getInstancePath() {
21 echo $SDTD_BASE/instances/$1
22}
23
24# Check if the given instance name is an existing instance
25# Params:
26# 1: Instance name
27# Returns:
28# 0/1 instance not valid/valid
29isValidInstance() {
30 if [ -z $1 ]; then
31 echo 0
32 else
33 if [ ! -d $(getInstancePath $1) ]; then
34 echo 0
35 else
36 if [ ! -f $(getInstancePath $1)/config.xml ]; then
37 echo 0
38 else
39 echo 1
40 fi
41 fi
42 fi
43}
44
45# Check if the given instance is currently running
46# Params:
47# 1: Instance name
48# Returns:
49# 0 = not running
50# 1 = running
51isRunning() {
52 start-stop-daemon --status --pidfile $(getInstancePath $1)/7dtd.pid
53 if [ $? -eq 0 ]; then
54 echo 1
55 else
56 echo 0
57 fi
58}
59
60# Get list of defined instances
61# Returns:
62# List of instances
63getInstanceList() {
64 for IF in $SDTD_BASE/instances/*; do
65 I=`basename $IF`
66 if [ $(isValidInstance $I) -eq 1 ]; then
67 echo $I
68 fi
69 done
70}
71
72# Get the PID of the instance if it is running, 0 otherwise
73# Params:
74# 1: Instance name
75# Returns:
76# 0 if not running
77# PID otherwise
78getInstancePID() {
79 if [ $(isRunning $1) -eq 1 ]; then
80 cat $(getInstancePath $1)/7dtd.pid
81 else
82 echo 0
83 fi
84}
85
86# Get a single value from a serverconfig
87# Params:
88# 1: Instance name
89# 2: Property name
90# Returns:
91# Property value
92getConfigValue() {
93 CONF=$(getInstancePath $1)/config.xml
94 $XMLSTARLET sel -t -v "/ServerSettings/property[@name='$2']/@value" $CONF
95}
96
97# Update a single value in a serverconfig
98# Params:
99# 1: Instance name
100# 2: Property name
101# 3: New value
102setConfigValue() {
103 CONF=$(getInstancePath $1)/config.xml
104 $XMLSTARLET ed -L -u "/ServerSettings/property[@name='$2']/@value" -v "$3" $CONF
105}
106
107# Check if a given port range (baseport, baseport+1, baseport+2 each udp)
108# is already in use by any instance
109# Params:
110# 1: Baseport
111# Returns:
112# 0/1 not in use/in use
113checkGamePortUsed() {
114 PORTMIN=$1
115 PORTMAX=$(( $1 + 2 ))
116 for I in $(getInstanceList); do
117 CURPORTMIN=$(getConfigValue $I "ServerPort")
118 CURPORTMAX=$(( $CURPORTMIN + 2 ))
119 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then
120 echo 1
121 return
122 fi
123 done
124 echo 0
125}
126
127# Check if a given telnet port is already in use by any instance
128# Params:
129# 1: Port
130# Returns:
131# 0/1 not in use/in use
132checkTelnetPortUsed() {
133 for I in $(getInstanceList); do
134 CURENABLED=$(getConfigValue $I "TelnetEnabled")
135 CURPORT=$(getConfigValue $I "TelnetPort")
136 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
137 echo 1
138 return
139 fi
140 done
141 echo 0
142}
143
144# Send a single command to the telnet port
145# Params:
146# 1: Instance name
147# 2: Command
148# Returns:
149# String of telnet output
150telnetCommand() {
151 TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
152 TEL_PORT=$(getConfigValue $1 TelnetPort)
153 TEL_PASS=$(getConfigValue $1 TelnetPassword)
154 if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
155 echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT
156 else
157 echo "Telnet not enabled or no password set."
158 fi
159}
160
161# Get all hook files for the given hook-name
162# Params:
163# 1: Hook name
164# Returns:
165# Names of hook files
166getHooksFor() {
167 if [ -d $SDTD_BASE/hooks/$1 ]; then
168 for H in $SDTD_BASE/hooks/$1/*.sh; do
169 echo "$H"
170 done
171 fi
172}
173
174# Lowercase passed string
175# Params:
176# 1: String
177# Returns:
178# Lowercased string
179lowercase() {
180 echo "${1}" | tr "[:upper:]" "[:lower:]"
181}
182
183# Prepare passed string as part of camelcase, i.e. first char upper case, others
184# lowercase
185# Params:
186# 1: String
187# Returns:
188# Transformed string
189camelcasePrep() {
190 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
191}
192
193listCommands() {
194 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
195 | grep -v "Help$"\
196 | grep -v "Description$"\
197 | grep -v "Expects$"); do
198 CMD=$(lowercase "${C#sdtdCommand}")
199 printf "%s " "$CMD"
200 done
201}
202
203. /usr/local/lib/7dtd/help.sh
204. /usr/local/lib/7dtd/playerlog.sh
205for M in /usr/local/lib/7dtd/commands/*.sh; do
206 . $M
207done
208
Note: See TracBrowser for help on using the repository browser.