diff --git a/tmux.sh b/tmux.sh index ce30375..0f759b0 100755 --- a/tmux.sh +++ b/tmux.sh @@ -1,21 +1,50 @@ #!/bin/bash -server="$1" -shift +# constants +code_clear=$'\x05\x15' +code_send=$'\r' -if ! [[ -e ./vars ]] ; then - echo 'Please create a "vars" file using "vars.example" as a template' +die () { + echo "$@" exit 1 -fi -source ./vars +} + +# directories +mcserver_root="${XDG_DATA_HOME:-${HOME}/.local/share}/mcserver" +instances_dir="$mcserver_root/instances" + +mkdir -p "$instances_dir" cmd="$1" shift -code_clear=$'\x05\x15' -code_send=$'\r' +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" ;; - *) tmux new-window -t $TMUX_SESSION: "./start.sh" || tmux new-session -s $TMUX_SESSION "./start.sh";; + start|*) tmux new-window -t $TMUX_SESSION: "./start.sh" || tmux new-session -s $TMUX_SESSION "./start.sh";; esac