165 lines
3.6 KiB
Bash
Executable File
165 lines
3.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# kf2-srv is a command line tool for managing a set of Killing Floor 2 servers.
|
|
# Copyright (C) 2019, 2020 GenZmeY
|
|
# mailto: genzmey@gmail.com
|
|
#
|
|
# This file is part of kf2-srv.
|
|
#
|
|
# kf2-srv is free software: you can redistribute it and/or modify
|
|
# 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/>.
|
|
|
|
readonly ScriptFullname=$(readlink -e "$0")
|
|
readonly ScriptName=$(basename $0)
|
|
readonly ScriptVersion=$(rpm -q --queryformat '%{VERSION}' "$ScriptName")
|
|
|
|
readonly GrpDir=":DEFINE_PREFIX:/share/kf2-srv/cmdgrp"
|
|
readonly LibDir=":DEFINE_PREFIX:/share/kf2-srv/lib"
|
|
|
|
function include () # $1: Lib
|
|
{
|
|
if ! echo "$INC_LIBS" | grep -Foq "$1"; then
|
|
source "$1"
|
|
export INC_LIBS="$INC_LIBS:$1"
|
|
fi
|
|
}
|
|
|
|
function is_help () # $1: Arg
|
|
{
|
|
echo "$1" | grep -Piqo '^(-h|--help|help)$'
|
|
return $?
|
|
}
|
|
|
|
function is_version () # $1: Arg
|
|
{
|
|
echo "$1" | grep -Piqo '^(-v|--version|version)$'
|
|
return $?
|
|
}
|
|
|
|
function function_exists () # $1: function name
|
|
{
|
|
type "$1" &> /dev/null
|
|
return $?
|
|
}
|
|
|
|
function indent () # $1: Level
|
|
{
|
|
local Tab=' '
|
|
for ((i=0; i<$1; i++))
|
|
do
|
|
echo -n "$Tab"
|
|
done
|
|
}
|
|
|
|
function groups_list ()
|
|
{
|
|
find "$GrpDir" \
|
|
-mindepth 1 \
|
|
-maxdepth 1 \
|
|
-type d \
|
|
-printf "%f\n" | \
|
|
sort
|
|
}
|
|
|
|
function commands_list () # $1: Command group
|
|
{
|
|
find "$GrpDir/$1" \
|
|
-mindepth 1 \
|
|
-maxdepth 1 \
|
|
-type f \
|
|
-printf "%f\n" | \
|
|
sort
|
|
}
|
|
|
|
function group_info () # $1: Command group
|
|
{
|
|
local Command
|
|
for Command in $(commands_list $1)
|
|
do
|
|
local CommandPathName="$GrpDir/$1/$Command"
|
|
( # subshell
|
|
source "$CommandPathName"
|
|
if function_exists "cmd_usage"; then
|
|
echo "$(indent 1)${ScriptName}${KF2POSTFIX} $1 $Command $(cmd_usage)"
|
|
else
|
|
echo "$(indent 1)${ScriptName}${KF2POSTFIX} $1 $Command"
|
|
fi
|
|
if function_exists "cmd_info"; then
|
|
cmd_info | sed -r "s|^|$(indent 2)|g"
|
|
else
|
|
echo "$(indent 2)No information"
|
|
fi
|
|
)
|
|
done
|
|
}
|
|
|
|
function full_info ()
|
|
{
|
|
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)
|
|
do
|
|
group_info "$Group"
|
|
echo ""
|
|
done
|
|
echo "Use --help as an argument for information on a specific group or command"
|
|
}
|
|
|
|
include "$LibDir/globals.lib"
|
|
|
|
Group="$1"
|
|
Command="$2"
|
|
|
|
GroupPathname="$GrpDir/$Group"
|
|
CommandPathName="$GroupPathname/$Command"
|
|
|
|
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
|
|
else
|
|
echo "No help page for this command."
|
|
fi
|
|
else
|
|
if function_exists "cmd_main"; then
|
|
cmd_main "$@"
|
|
else
|
|
echo "No implementation for the command $Command"
|
|
fi
|
|
fi
|
|
else
|
|
echo "Command not found: $Command"
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "Command group not found: $Group"
|
|
exit 1
|
|
fi
|
|
|