# Open Infrastructure: compute-tools

# Copyright (C) 2014-2022 Daniel Baumann <daniel.baumann@open-infrastructure.net>
#
# SPDX-License-Identifier: GPL-3.0+
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

_container()
{
	local cur prev cmd opts
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"

	if [ ${COMP_CWORD} -gt 0 ]
	then
		cmd="${COMP_WORDS[1]}"
	fi

	if [ "${prev}" = "--" ]
	then
		compopt -o bashdefault
		COMPREPLY=( $(compgen -c -- $cur) )
		return 0
	fi

	case "${cmd}" in
		auto)
			opts="-f --force -s --start -t --stop"
			COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
			return 0
			;;

		build|b)
			case "${prev}" in
				-n|--name)
					opts="$(cd /etc/compute-tools/debconf 2>/dev/null && ls *.cfg */*.cfg 2>/dev/null | sed -e 's|.*/||g' -e 's|.cfg$||g')"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				-s|--script)
					opts="$(cd /usr/share/compute-tools/build-scripts && find -maxdepth 1 -not -type d -and -not -name 'default' -and -not -name 'debconf' -and -not -name '*.d' -printf '%P\n' | sort)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name -c --capability -d --drop-capability -s --script -v --verbose -b --bind --bind-ro"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		console)
			case "${prev}" in
				-n|--name)
					opts="$(container list -s -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		enter|run)
			case "${prev}" in
				-n|--name)
					opts="$(container list -s -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		get|g)
			case "${prev}" in
				-n|--name)
					opts="$(cd /etc/compute-tools/debconf 2>/dev/null && ls *.cfg */*.cfg 2>/dev/null | sed -e 's|.*/||g' -e 's|.cfg$||g')"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				-s|--script)
					opts="$(cd /usr/share/compute-tools/get-scripts && find -maxdepth 1 -not -type d -and -not -name 'default' -and -not -name 'debconf' -and -not -name '*.d' -printf '%P\n' | sort)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name -c --capability -d --drop-capability -s --script -v --verbose -b --bind --bind-ro"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		info)
			case "${prev}" in
				-n|--name)
					opts="$(container list -t -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="--status --os --ip"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		key)
			case "${prev}" in
				-a|--add)
					opts="$(cd /usr/share/compute-tools/keys 2>/dev/null && ls *.pub 2>/dev/null | sed -e 's|.*/||g' -e 's|.pub$||g')"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				-r|--remove)
					opts="$(gpg --homedir /etc/compute-tools/keys --list-keys | grep ^uid | sed -e 's|.*<||' -e 's|>||')"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-a --add -r --remove -l --list"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		limit)
			case "${prev}" in
				-n|--name)
					opts="$(container list -a -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name --blockio-device-weight --blockio-read-bandwidth -b --blockio-weight --blockio-write-bandwidth -c --cpu-quota --cpu-shares -m --memory-limit -t --tasks-max"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		list|ls)
			case "${prev}" in
				-h|--host)
					opts="true false $(hostname -f 2> /dev/null || hostname)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				-f|--format)
					opts="cli csv json nwdiag shell sh yaml xml"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-a --all -f --format -h --host -o --other -s --started -t --stopped"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		log)
			case "${prev}" in
				-d|--date)
					opts="$(if ls /var/log/compute-tools/container.log-*.gz > /dev/null 2>&1;
						then \
							zcat /var/log/compute-tools/container.log-*.gz | awk '{ print $1 }' | sort -u; \
						fi; \
						if [ -e /var/log/compute-tools/container.log ]; \
						then \
							awk '{ print $1 }' /var/log/compute-tools/container.log | sort -u; \
						fi) \
						today yesterday"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				-n|--name)
					opts="$(if ls /var/log/compute-tools/container.log-*.gz > /dev/null 2>&1;
						then \
							zgrep -E '(\-n|\-\-name) ' /var/log/compute-tools/container.log-*.gz | sed -e 's|.*-n ||' -e 's|.*--name ||' -e 's| .*$||' | sort -u; \
						fi; \
						if [ -e /var/log/compute-tools/container.log ]; \
						then \
							grep -E '(\-n|\-\-name) ' /var/log/compute-tools/container.log | sed -e 's|.*-n ||' -e 's|.*--name ||' -e 's| .*$||' | sort -u; \
						fi)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-d --date -n --name"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		move|mv)
			case "${prev}" in
				-o|--old)
					opts="$(container list -t -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-f --force -n --new -o --old"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		remove|rm)
			case "${prev}" in
				-n|--name)
					opts="$(container list -t -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name -f --force -i --interactive -k --kill"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		rebuild|rb)
			case "${prev}" in
				-n|--name)
					opts="$(container list -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name -f --force -v --verbose"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		restart|rs)
			case "${prev}" in
				-n|--name)
					opts="$(container list -s -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		start|s)
			case "${prev}" in
				-n|--name)
					opts="$(container list -t -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name -v --verbose"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		status|st)
			case "${prev}" in
				-n|--name)
					opts="$(container list -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		stop|t)
			case "${prev}" in
				-n|--name)
					opts="$(container list -s -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name -f --force"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		top)
			opts="-d --delay"
			COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
			return 0
			;;

		update|u)
			case "${prev}" in
				-n|--name)
					opts="$(container list -s -f shell)"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;

				*)
					opts="-n --name -f --full-upgrade -r --autoremove -p --purge --y --yes"
					COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
					return 0
					;;
			esac
			;;

		version|ver)
			return 0
			;;

		*)
			local commands=$(cd /usr/libexec/container 2>/dev/null && find * -type f -print)
			COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
			return 0
			;;
	esac

	compopt -o filenames
	COMPREPLY=( $(compgen -f -- $cur) )
	return 0
}

complete -F _container container cnt