msm/bash_completion/msm
Marcus Whybrow 9447124b58 Added support for "all" server name. Fixes #16.
Using "all" executes a command for all servers.
2012-06-07 14:22:27 +01:00

262 lines
6.7 KiB
Plaintext

### Utility Functions
CONFIG="/etc/msm.conf"
# Loads variables
__init() {
source "$CONFIG"
}
# Loads server variables
# $1: The name of a server
__init_server() {
SERVER_PATH="$SERVER_STORAGE_PATH/$1"
SERVER_CONF="$SERVER_STORAGE_PATH/$1/$DEFAULT_SERVER_CONF"
SERVER_USER="$DEFAULT_SERVER_USER"
SCREEN_NAME="${DEFAULT_SCREEN_NAME//\{SERVER_NAME\}/${1}}"
WORLD_STORAGE_PATH="$SERVER_PATH/$DEFAULT_WORLD_STORAGE_PATH"
WORLD_STORAGE_INACTIVE_PATH="$SERVER_PATH/$DEFAULT_WORLD_STORAGE_INACTIVE_PATH"
WHITELIST="$SERVER_PATH/$DEFAULT_WHITELIST"
BANNED_PLAYERS="$SERVER_PATH/$DEFAULT_BANNED_PLAYERS"
BANNED_IPS="$SERVER_PATH/$DEFAULT_BANNED_IPS"
OPS="$SERVER_PATH/$DEFAULT_OPS"
JAR="$DEFAULT_JAR"
RAM="$DEFAULT_RAM"
INVOCATION="$DEFAULT_INVOCATION"
if [[ -f "$SERVER_CONF" ]]; then
local name value
while read line; do
# ignore comment lines
echo "$line" | grep "^#" >/dev/null 2>&1 && continue
# if not empty, get the name and value of the setting
if [ ! -z "$line" ]; then
name="$(echo $line | awk -F '=' '{print $1}')"
value="$(echo $line | awk -F '\"' '{print $2}')"
fi
case "$name" in
SERVER_USER) SERVER_USER="$value";;
SCREEN_NAME) SCREEN_NAME="$value";;
WORLD_STORAGE_PATH) WORLD_STORAGE_PATH="$SERVER_PATH/$value";;
WORLD_STORAGE_INACTIVE_PATH) WORLD_STORAGE_INACTIVE_PATH="$SERVER_PATH/$value";;
WHITELIST) WHITELIST="$SERVER_PATH/$value";;
BANNED_PLAYERS) BANNED_PLAYERS="$SERVER_PATH/$value";;
BANNED_IPS) BANNED_IPS="$SERVER_PATH/$value";;
OPS) OPS="$SERVER_PATH/$value";;
JAR) JAR="$SERVER_PATH/$value";;
RAM) RAM="$value";;
INVOCATION) INVOCATION="$value";;
esac
done < "$SERVER_CONF"
fi
}
### Auto Completion Function
_msm() {
__init
local base current options
COMPREPLY=()
current="${COMP_WORDS[$COMP_CWORD]}"
if [[ $COMP_CWORD == 1 ]]; then
if [ -d "$SERVER_STORAGE_PATH" ]; then
local servers="$(ls -1 "$SERVER_STORAGE_PATH")"
fi
options="help start stop restart version server jargroup all $servers"
else
case "${COMP_WORDS[1]}" in
stop|restart)
if [[ $COMP_CWORD == 2 ]]; then
options="now"
fi
;;
server)
if [[ $COMP_CWORD == 2 ]]; then
options="list create delete rename"
else
case "${COMP_WORDS[2]}" in
delete|rename)
if [[ $COMP_CWORD == 3 && -d "$SERVER_STORAGE_PATH" ]]; then
options="$(ls -1 "$SERVER_STORAGE_PATH")"
fi
;;
esac
fi
;;
jargroup)
if [[ $COMP_CWORD == 2 ]]; then
options="list create delete rename changeurl getlatest"
else
case "${COMP_WORDS[2]}" in
delete|rename|changeurl|getlatest)
if [[ $COMP_CWORD == 3 && -d "$JAR_STORAGE_PATH" ]]; then
options="$(ls -1 "$JAR_STORAGE_PATH")"
fi
;;
esac
fi
;;
*)
# Server options
local server_path="$SERVER_STORAGE_PATH/${COMP_WORDS[1]}"
if [[ "${COMP_WORDS[1]}" == "all" ]] || [ -e "$server_path" ]; then
# If the server exists
__init_server "${COMP_WORDS[1]}"
if [[ $COMP_CWORD == 2 ]]; then
options="start stop restart status connected worlds logroll backup jar whitelist blacklist operator gamemode kick say time toggledownfall save cmd cmdlog"
else
case "${COMP_WORDS[2]}" in
stop|restart)
if [[ $COMP_CWORD == 3 ]]; then
options="now"
fi
;;
worlds)
if [[ $COMP_CWORD == 3 ]]; then
options="list load ram todisk backup on off"
else
case "${COMP_WORDS[3]}" in
ram)
if [[ $COMP_CWORD == 4 ]]; then
if [ -d "$WORLD_STORAGE_PATH" ]; then
options="$(ls -1 "$WORLD_STORAGE_PATH")"
fi
fi
;;
on)
if [[ $COMP_CWORD == 4 ]]; then
if [ -d "$WORLD_STORAGE_INACTIVE_PATH" ]; then
options="$(ls -1 "$WORLD_STORAGE_INACTIVE_PATH")"
fi
fi
;;
off)
if [[ $COMP_CWORD == 4 ]]; then
if [ -d "$WORLD_STORAGE_PATH" ]; then
options="$(ls -1 "$WORLD_STORAGE_PATH")"
fi
fi
;;
esac
fi
;;
jar)
if [[ $COMP_CWORD == 3 && -d "$JAR_STORAGE_PATH" ]]; then
options="$(ls -1 "$JAR_STORAGE_PATH")"
fi
if [[ $COMP_CWORD == 4 && -d "$JAR_STORAGE_PATH/${COMP_WORDS[3]}" ]]; then
options="$(find "$JAR_STORAGE_PATH/${COMP_WORDS[3]}" -type f -name "*.jar" -exec basename {} \;)"
fi
;;
whitelist|wl)
if [[ $COMP_CWORD == 3 ]]; then
options="on off add remove list"
else
case "${COMP_WORDS[3]}" in
remove)
if [[ $COMP_CWORD == 4 ]]; then
if [ -f "$WHITELIST" ]; then
options="$(cat "$WHITELIST")"
fi
fi
;;
esac
fi
;;
blacklist|bl)
if [[ $COMP_CWORD == 3 ]]; then
options="player ip list"
else
case "${COMP_WORDS[3]}" in
player)
if [[ $COMP_CWORD == 4 ]]; then
options="add remove"
else
case "${COMP_WORDS[4]}" in
remove)
if [[ $COMP_CWORD == 5 ]]; then
if [ -f "$BANNED_PLAYERS" ]; then
options="$(cat "$BANNED_PLAYERS")"
fi
fi
;;
esac
fi
;;
ip)
if [[ $COMP_CWORD == 4 ]]; then
options="add remove"
else
case "${COMP_WORDS[4]}" in
remove)
if [[ $COMP_CWORD == 5 ]]; then
if [ -f "$BANNED_IPS" ]; then
options="$(cat "$BANNED_IPS")"
fi
fi
;;
esac
fi
;;
esac
fi
;;
operator|op)
if [[ $COMP_CWORD == 3 ]]; then
options="add remove list"
else
case "${COMP_WORDS[3]}" in
remove)
if [[ $COMP_CWORD == 4 ]]; then
if [ -f "$OPS" ]; then
options="$(cat "$OPS")"
fi
fi
;;
esac
fi
;;
gamemode|gm)
if [[ $COMP_CWORD == 3 ]]; then
options="survival creative"
fi
;;
time)
if [[ $COMP_CWORD == 3 ]]; then
options="set add"
fi
;;
save)
if [[ $COMP_CWORD == 3 ]]; then
options="on off all"
fi
;;
esac
fi
fi
;;
esac
fi
COMPREPLY=( $(compgen -W "${options}" -- ${current}) )
return 0
}
complete -F _msm msm