mcserver/mcserver

51 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
# constants
code_clear=$'\x05\x15'
code_send=$'\r'
die () {
echo "$@"
exit 1
}
# directories
mcserver_root="${XDG_DATA_HOME:-${HOME}/.local/share}/mcserver"
instances_dir="$mcserver_root/instances"
mkdir -p "$instances_dir"
cmd="$1"
shift
instance="$1"
shift
instance_dir="$instances_dir/$instance"
# input checks
if [[ -z "${cmd:+x}" ]] ; then
die "Please specify one of start, stop, or command."
fi
if [[ -z "${instance:+x}" ]] ; then
die "Please specify an instance."
fi
if ! [[ -d "$instance_dir" ]] ; then
die "Instance \`$instance\` does not exist. Please create it first."
fi
cd "$instance_dir"
if ! [[ -e ./vars ]] ; then
die 'Please create a "vars" file using "vars.example" as a template.'
fi
source ./vars
case "$cmd" in
stop) tmux send-keys -t "$TMUX_SESSION" -l "$code_clear" 'stop' "$code_send" ;;
command) tmux send-keys -t "$TMUX_SESSION" -l "$code_clear" "$*" "$code_send" ;;
start|*) tmux new-window -t $TMUX_SESSION: "./start.sh" || tmux new-session -s $TMUX_SESSION "./start.sh";;
esac