# Minecraft Server Manager: Bash Completion script
# ================================================
#
# This script provides tab completion support for the
# Minecraft Server Manager command.
#
# https://github.com/msmhq/msm
#


# Source, if it exists, the msm profile.d script
if [ -f "/etc/profile.d/msm.sh" ]; then
	source "/etc/profile.d/msm.sh"
fi

MSM="${MSM_SCRIPT:-/etc/init.d/msm}"


### Auto Completion Function

_msm() {
	source "$MSM"
	
	local base current options
	COMPREPLY=()
	
	current="${COMP_WORDS[$COMP_CWORD]}"

	manager_property SERVER_STORAGE_PATH
	
	if [[ $COMP_CWORD == 1 ]]; then
		if [ -d "$SETTINGS_SERVER_STORAGE_PATH" ]; then
			local servers="$(ls -1 "$SETTINGS_SERVER_STORAGE_PATH")"
		fi
		options="help start stop restart version update server jargroup all config $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 "$SETTINGS_SERVER_STORAGE_PATH" ]]; then
								options="$(ls -1 "$SETTINGS_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)
							manager_property JAR_STORAGE_PATH
							if [[ $COMP_CWORD == 3 && -d "$SETTINGS_JAR_STORAGE_PATH" ]]; then
								options="$(ls -1 "$SETTINGS_JAR_STORAGE_PATH")"
							fi
							;;
					esac
				fi
				;;
			start|config|help|update)
				# Do nothing, just don't execute the server logic
				;;
			*)
				# Server options

				if [[ "${COMP_WORDS[1]}" == "all" ]]; then
					local sid="-1"
				else
					server_get_id "${COMP_WORDS[1]}"
					local sid="$RETURN"
				fi
				

				local server_path="$SETTINGS_SERVER_STORAGE_PATH/${COMP_WORDS[1]}"	
				if [[ "${COMP_WORDS[1]}" == "all" ]] || [ -e "$server_path" ]; then
					if [[ $COMP_CWORD == 2 ]]; then
						options="start stop restart status connected worlds logroll backup jar whitelist blacklist operator gamemode kick say time toggledownfall give xp save cmd cmdlog console config"
					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
									if [[ "${COMP_WORDS[1]}" != "all" ]]; then
										case "${COMP_WORDS[3]}" in
											ram)
												if [[ $COMP_CWORD == 4 ]]; then
													server_property "$sid" WORLD_STORAGE_PATH
													if [ -d "${SERVER_WORLD_STORAGE_PATH[$sid]}" ]; then
														options="$(ls -1 "${SERVER_WORLD_STORAGE_PATH[$sid]}")"
													fi
												fi
												;;
											on)
												if [[ $COMP_CWORD == 4 ]]; then
													server_property "$sid" WORLD_STORAGE_INACTIVE_PATH
													if [ -d "${SERVER_WORLD_STORAGE_INACTIVE_PATH[$sid]}" ]; then
														options="$(ls -1 "${SERVER_WORLD_STORAGE_INACTIVE_PATH[$sid]}")"
													fi
												fi
												;;
											off)
												if [[ $COMP_CWORD == 4 ]]; then
													server_property "$sid" WORLD_STORAGE_PATH
													if [ -d "${SERVER_WORLD_STORAGE_PATH[$sid]}" ]; then
														options="$(ls -1 "${SERVER_WORLD_STORAGE_PATH[$sid]}")"
													fi
												fi
												;;
										esac
									fi
								fi
								;;
							jar)
								manager_property JAR_STORAGE_PATH
								if [[ $COMP_CWORD == 3 && -d "$SETTINGS_JAR_STORAGE_PATH" ]]; then
									options="$(ls -1 "$SETTINGS_JAR_STORAGE_PATH")"
								fi
								
								if [[ $COMP_CWORD == 4 && -d "$SETTINGS_JAR_STORAGE_PATH/${COMP_WORDS[3]}" ]]; then
									options="$(find "$SETTINGS_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
									if [[ "${COMP_WORDS[1]}" != "all" ]]; then
										case "${COMP_WORDS[3]}" in
											remove)
												if [[ $COMP_CWORD -ge 4 ]]; then
													server_property "$sid" WHITELIST_PATH
													if [ -f "${SERVER_WHITELIST_PATH[$sid]}" ]; then
														options="$(cat "${SERVER_WHITELIST_PATH[$sid]}")"
													fi
												fi
												;;
										esac
									fi
								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
												if [[ "${COMP_WORDS[1]}" != "all" ]]; then
													case "${COMP_WORDS[4]}" in
														remove)
															if [[ $COMP_CWORD -ge 5 ]]; then
																server_property "$sid" BANNED_PLAYERS_PATH
																if [ -f "${SERVER_BANNED_PLAYERS_PATH[$sid]}" ]; then
																	options="$(cat "${SERVER_BANNED_PLAYERS_PATH[$sid]}")"
																fi
															fi
															;;
													esac
												fi
											fi
											;;
										ip)
											if [[ $COMP_CWORD == 4 ]]; then
												options="add remove"
											else
												if [[ "${COMP_WORDS[1]}" != "all" ]]; then
													case "${COMP_WORDS[4]}" in
														remove)
															if [[ $COMP_CWORD -ge 5 ]]; then
																server_property "$sid" BANNED_PLAYERS_PATH
																if [ -f "${SERVER_BANNED_IPS_PATH[$sid]}" ]; then
																	options="$(cat "${SERVER_BANNED_IPS_PATH[$sid]}")"
																fi
															fi
															;;
													esac
												fi
											fi
											;;
									esac
								fi
								;;
							operator|op)
								if [[ $COMP_CWORD == 3 ]]; then
									options="add remove list"
								else
									if [[ "${COMP_WORDS[1]}" != "all" ]]; then
										case "${COMP_WORDS[3]}" in
											remove)
												if [[ $COMP_CWORD -ge 4 ]]; then
													server_property "$sid" OPS_PATH
													if [ -f "${SERVER_OPS_PATH[$sid]}" ]; then
														options="$(cat "${SERVER_OPS_PATH[$sid]}")"
													fi
												fi
												;;
										esac
									fi
								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
								;;
							config)
								if [[ $COMP_CWORD == 3 ]]; then
									if [[ "${COMP_WORDS[1]}" != "all" ]]; then
										server_property "$sid" SERVER_CONF
										options="$(more "${SERVER_CONF[$sid]}" | sed 's/=.*$//' | grep -v '#')";
									fi
								fi
								;;
						esac
								
					fi
				fi
				;;
		esac
	fi
	
	COMPREPLY=( $(compgen -W "${options}" -- ${current}) )
	return 0
}

complete -F _msm msm