1
0
Fork 0
forked from ahurac/dotfiles
ahuarc-dotfiles/bin/mcserv

101 lines
1.5 KiB
Text
Raw Normal View History

#!/bin/bash
exec_user() {
local user="$1"
shift
if [ "$USER" != "$user" ]; then
exec sudo -u "$user" "$0" "$@"
else
return 0
fi
}
exec_user minecraft "$@"
cd ~/bin || exit
print_error() {
>&2 printf '\033[31;1m%s\033[0m \033[1m%s\033[0m\n' ERROR: "$*"
}
public_usage() {
local name
name="$(basename "$0")"
cat << EOF
${name}: utilisation
2023-07-10 15:30:53 +02:00
${name} start|status|stop|watch|restart NOM_SERVEUR
2023-07-04 20:31:59 +02:00
${name} list|active|usage
EOF
}
error_usage() {
print_error "Utilisation invalide."
>&2 public_usage
return 1
}
public_start() {
if [ -n "$1" ]; then
screen -d -m -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
screen -S "$1" -X stuff '^E^Ustop^M'
else
error_usage
fi
}
public_watch() {
screen -dr "$1"
}
public_active() {
screen -ls
}
public_list() {
echo "Serveurs disponibles :"
ls --color=auto
}
2023-07-10 15:30:53 +02:00
public_restart() {
set -e
stop "$@"
start "$@"
set +e
}
verb="$1"
shift
case "$verb" in
2023-07-10 15:30:53 +02:00
start|status|stop|watch|active|list|usage|restart)
"public_${verb}" "$@"
;;
"")
public_active
2023-07-04 20:29:09 +02:00
;;
*)
error_usage
;;
esac