dotfiles/bin/mcserv

108 lines
1.7 KiB
Text
Raw Normal View History

#!/bin/bash
2023-12-01 13:29:06 +01:00
exec_as() {
local user="$1"
shift
2023-12-01 13:29:06 +01:00
if [ "$(whoami)" != "$user" ]; then
exec sudo -u "$user" "$0" "$@"
else
2023-12-01 13:29:06 +01:00
cd
fi
}
2023-12-01 13:29:06 +01:00
exec_as minecraft "$@" || exit
bin=bin
PATH="${bin}:${PATH}"
EDITOR="${EDITOR:-nvim}"
alias ls='ls --color=auto'
shopt -s expand_aliases
print_error() {
2023-12-01 13:29:06 +01:00
>&2 printf '\033[31;1mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
}
public_usage() {
local name
name="$(basename "$0")"
cat << EOF
${name}: utilisation
2023-12-01 13:29:06 +01:00
${name} start|status|stop|watch|restart|edit <nom serveur>
${name} list|active|usage|shell
EOF
}
error_usage() {
print_error "Utilisation invalide."
>&2 public_usage
return 1
}
public_start() {
if [ -n "$1" ]; then
2023-12-01 13:29:06 +01:00
exec screen -S "$1" \
"$1"
else
error_usage
fi
}
public_status() {
if [ -n "$1" ]; then
printf '\033[1m%s\033[0m : ' "$1"
if screen -ls "$1" > /dev/null 2>&1; then
echo "en cours d'exécution"
else
echo "non en cours d'exécution"
fi
else
error_usage
fi
}
public_stop() {
if [ -n "$1" ]; then
2023-12-01 13:29:06 +01:00
exec screen -S "$1" -X stuff '^E^Ustop^M'
else
error_usage
fi
}
public_watch() {
2023-12-01 13:29:06 +01:00
exec screen -dr "$1"
}
public_active() {
2023-12-01 13:29:06 +01:00
exec screen -ls
}
public_list() {
echo "Serveurs disponibles :"
2023-12-01 13:29:06 +01:00
exec ls "$bin"
}
2023-07-10 15:30:53 +02:00
public_restart() {
set -e
stop "$@"
start "$@"
set +e
}
2023-12-01 13:29:06 +01:00
public_edit() {
exec "$EDITOR" "${bin}/${1}"
}
public_shell() {
exec bash -i
}
verb="$1"
shift
2023-12-01 13:29:06 +01:00
if [ -z "$verb" ]; then
public_active
elif declare -F | cut -d \ -f 3 | grep '^public_' | sed 's/^public_//' | grep -q "^${verb}$"; then
"public_${verb}" "$@"
else
error_usage
fi