KF2-SRV/SOURCES/main/kf2-srv

237 lines
5.8 KiB
Plaintext
Raw Normal View History

2020-07-08 22:25:08 +00:00
#!/bin/bash
# kf2-srv is a command line tool for managing a set of Killing Floor 2 servers.
2020-07-08 22:52:17 +00:00
# Copyright (C) 2019, 2020 GenZmeY
2020-07-08 22:25:08 +00:00
# mailto: genzmey@gmail.com
#
2020-07-08 23:03:57 +00:00
# This file is part of kf2-srv.
#
# kf2-srv is free software: you can redistribute it and/or modify
2020-07-08 22:25:08 +00:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-07-12 23:35:29 +00:00
readonly ScriptFullname=$(readlink -e "$0")
2020-08-07 07:56:51 +00:00
readonly ScriptName=$(basename $0)
readonly ScriptVersion=$(rpm -q --queryformat '%{VERSION}' "$ScriptName")
2020-07-08 22:52:17 +00:00
2020-08-07 07:56:51 +00:00
readonly GrpDir=":DEFINE_PREFIX:/share/kf2-srv/cmdgrp"
readonly LibDir=":DEFINE_PREFIX:/share/kf2-srv/lib"
2020-07-08 22:59:33 +00:00
2020-08-08 00:40:13 +00:00
readonly InstallDir=":DEFINE_PREFIX:/games/kf2-srv${KF2POSTFIX}"
readonly AppBin="$InstallDir/Binaries/Win64/KFGameSteamServer.bin.x86_64"
readonly DefaultConfigDir="$InstallDir/KFGame/Config"
readonly DefaultDownloadDir="$InstallDir/Binaries/Win64/steamapps/workshop"
readonly DefaultCacheDir="$InstallDir/KFGame/Cache"
readonly DefaultLogDir="$InstallDir/KFGame/Logs"
readonly DownloadDir="/var/cache/kf2-srv/workshop"
readonly CacheDir="/var/cache/kf2-srv/cache"
readonly LogDir="/var/log/kf2-srv${KF2POSTFIX}"
readonly InstanceConfigDir="/etc/kf2-srv/instances${KF2POSTFIX}"
readonly InstanceConfigTemplate="/etc/kf2-srv/instance.conf.template"
readonly AppServerNum="232130"
readonly AppClientNum="232090"
readonly StrangeConstUID="17825793"
readonly ServerBotLogin="srvbot"
declare -a DiffNames
declare -a WaveNames
declare -A ModeNames
declare -A MutNames
2020-08-07 07:56:51 +00:00
function include () # $1: Lib
2020-07-08 22:25:08 +00:00
{
2020-08-07 07:56:51 +00:00
if ! echo "$INC_LIBS" | grep -Foq "$1"; then
source "$1"
export INC_LIBS="$INC_LIBS:$1"
2020-07-08 22:25:08 +00:00
fi
}
2020-08-07 12:25:10 +00:00
function run_as_steamuser () # $@: command
{
include "/etc/steamcmd/steamcmd.conf"
if [[ "$(whoami)" == "$SteamUser" ]]; then
shift 3; cmd_main "$@"
2020-08-08 00:40:13 +00:00
elif [[ -n $(groups "$(whoami)" | grep -Fo 'wheel') ]] || [[ "$(whoami)" == "root" ]]; then
export INC_LIBS=""
2020-08-07 12:25:10 +00:00
sudo -u "$SteamUser" "$@"
else
echo "You must be a $SteamUser, root or sudo-user to run this command."
fi
}
function run_as_root () # $@: command
{
if [[ "$(whoami)" == "root" ]]; then
shift 3; cmd_main "$@"
2020-08-07 12:25:10 +00:00
elif [[ -n $(groups "$(whoami)" | grep -Fo 'wheel') ]]; then
2020-08-08 00:40:13 +00:00
export INC_LIBS=""
2020-08-07 12:25:10 +00:00
sudo "$@"
else
echo "You must be root or sudo-user to run this command."
fi
}
2020-08-07 07:56:51 +00:00
function is_help () # $1: Arg
2020-07-08 22:25:08 +00:00
{
2020-08-07 07:56:51 +00:00
echo "$1" | grep -Piqo '^(-h|--help|help)$'
2020-07-08 22:25:08 +00:00
}
2020-08-13 02:44:02 +00:00
function is_usage () # $1: Arg
{
echo "$1" | grep -Piqo '^usage$'
}
2020-08-07 07:56:51 +00:00
function is_version () # $1: Arg
2020-07-08 22:52:17 +00:00
{
2020-08-07 07:56:51 +00:00
echo "$1" | grep -Piqo '^(-v|--version|version)$'
2020-07-08 22:52:17 +00:00
}
2020-08-08 03:14:17 +00:00
function server_exists ()
{
test -x "$AppBin"
}
2020-08-07 07:56:51 +00:00
function function_exists () # $1: function name
2020-07-08 22:25:08 +00:00
{
2020-08-07 07:56:51 +00:00
type "$1" &> /dev/null
2020-07-08 22:25:08 +00:00
}
2020-08-07 07:56:51 +00:00
function indent () # $1: Level
2020-07-08 22:25:08 +00:00
{
2020-08-07 07:56:51 +00:00
local Tab=' '
for ((i=0; i<$1; i++))
2020-07-08 22:52:17 +00:00
do
2020-08-07 07:56:51 +00:00
echo -n "$Tab"
2020-07-08 22:52:17 +00:00
done
2020-07-08 22:59:33 +00:00
}
2020-08-07 07:56:51 +00:00
function groups_list ()
2020-07-08 22:59:33 +00:00
{
2020-08-07 10:51:21 +00:00
find "$GrpDir" \
-mindepth 1 \
-maxdepth 1 \
-type d \
-printf "%f\n" | \
2020-08-07 07:56:51 +00:00
sort
2020-07-08 22:52:17 +00:00
}
2020-08-07 07:56:51 +00:00
function commands_list () # $1: Command group
2020-07-08 22:52:17 +00:00
{
2020-08-07 10:51:21 +00:00
find "$GrpDir/$1" \
-mindepth 1 \
-maxdepth 1 \
-type f \
-printf "%f\n" | \
2020-08-07 07:56:51 +00:00
sort
2020-07-08 22:56:38 +00:00
}
2020-08-07 07:56:51 +00:00
function group_info () # $1: Command group
2020-07-08 22:59:33 +00:00
{
2020-08-07 07:56:51 +00:00
local Command
for Command in $(commands_list $1)
2020-07-08 22:59:33 +00:00
do
2020-08-07 07:56:51 +00:00
local CommandPathName="$GrpDir/$1/$Command"
( # subshell
source "$CommandPathName"
if function_exists "cmd_usage"; then
echo "$(indent 1)${ScriptName}${KF2POSTFIX} $1 $Command $(cmd_usage)"
2020-07-08 22:52:17 +00:00
else
2020-08-07 07:56:51 +00:00
echo "$(indent 1)${ScriptName}${KF2POSTFIX} $1 $Command"
2020-07-08 23:02:30 +00:00
fi
2020-08-07 07:56:51 +00:00
if function_exists "cmd_info"; then
cmd_info | sed -r "s|^|$(indent 2)|g"
2020-07-08 22:59:33 +00:00
else
2020-08-07 07:56:51 +00:00
echo "$(indent 2)No information"
2020-07-08 22:59:33 +00:00
fi
2020-08-07 07:56:51 +00:00
)
2020-07-08 22:52:17 +00:00
done
}
2020-08-07 07:56:51 +00:00
function full_info ()
2020-07-08 22:52:17 +00:00
{
2020-08-07 07:56:51 +00:00
echo "${ScriptName}${KF2POSTFIX} v${ScriptVersion}"
echo "Command line tool for managing a set of Killing Floor 2 servers."
echo ""
echo "Usage: ${ScriptName}${KF2POSTFIX} <group> <command> [<args>]"
echo ""
for Group in $(groups_list)
2020-07-08 22:52:17 +00:00
do
2020-08-07 07:56:51 +00:00
group_info "$Group"
echo ""
2020-07-08 22:52:17 +00:00
done
2020-08-07 07:56:51 +00:00
echo "Use --help as an argument for information on a specific group or command"
2020-07-08 22:52:17 +00:00
}
2020-08-07 07:56:51 +00:00
Group="$1"
Command="$2"
2020-07-08 22:59:33 +00:00
2020-08-07 07:56:51 +00:00
GroupPathname="$GrpDir/$Group"
CommandPathName="$GroupPathname/$Command"
2020-07-08 22:59:33 +00:00
2020-08-07 07:56:51 +00:00
if [[ $# -eq 0 ]] || is_help "$1"; then
full_info
exit 0
elif is_version "$1"; then
echo "${ScriptName}${KF2POSTFIX} v${ScriptVersion}"
exit 0
elif [[ -d "$GroupPathname" ]]; then
shift
if [[ $# -eq 0 ]] || is_help "$1"; then
group_info "$Group"
exit 0
elif [[ -r "$CommandPathName" ]]; then
shift
source "$CommandPathName"
if is_help "$1"; then
if function_exists "cmd_help"; then
cmd_help
2020-07-08 22:59:33 +00:00
else
2020-08-07 07:56:51 +00:00
echo "No help page for this command."
2020-07-08 22:59:33 +00:00
fi
2020-08-13 02:44:02 +00:00
elif is_usage "$1"; then
if function_exists "cmd_usage"; then
cmd_usage
else
echo "No usage information for this command."
fi
2020-07-08 22:59:33 +00:00
else
2020-08-07 07:56:51 +00:00
if function_exists "cmd_main"; then
2020-08-08 03:14:17 +00:00
if function_exists "cmd_need_installed_server" && cmd_need_installed_server && ! server_exists; then
echo "You must install server first"
echo "Run \"${ScriptName}${KF2POSTFIX} game update\" to install it"
exit 1
elif function_exists "cmd_need_superuser" && cmd_need_superuser; then
2020-08-07 12:25:10 +00:00
run_as_root "${ScriptFullname}${KF2POSTFIX}" "$Group" "$Command" "$@"
2020-08-07 10:51:21 +00:00
elif function_exists "cmd_need_steamuser" && cmd_need_steamuser; then
2020-08-07 12:25:10 +00:00
run_as_steamuser "${ScriptFullname}${KF2POSTFIX}" "$Group" "$Command" "$@"
2020-08-07 10:51:21 +00:00
else
cmd_main "$@"
fi
2020-07-08 22:59:33 +00:00
else
2020-08-07 07:56:51 +00:00
echo "No implementation for the command $Command"
2020-07-08 22:59:33 +00:00
fi
fi
2020-08-07 07:56:51 +00:00
else
echo "Command not found: $Command"
exit 1
2020-07-08 22:59:33 +00:00
fi
2020-08-07 07:56:51 +00:00
else
echo "Command group not found: $Group"
exit 1
2020-07-08 22:56:38 +00:00
fi