#                 __
#    ____ _____  |  | _____
#   /    \\__  \ |  | \__  \
#  |   |  \/ __ \|  |__/ __ \_
#  |___|  (____  /____(____  /
#       \/     \/          \/
#
# Copyright (C) 2021, 2022 Blake Lee
#
# This file is part of nala
#
# nala is 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.
#
# nala is 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 nala.  If not, see <https://www.gnu.org/licenses/>.

_nala()
{
	local sourcesdir="/etc/apt/sources.list.d"
	local cur prev words cword split
	_init_completion -s || return

	local command i

	local GLOBAL_OPTIONS='
		-h --help
		--debug
		--verbose
		--version
		--license
		--update
    '

	local DPKG_OPTIONS='
		--no-aptlist
		--noninteractive
		--noninteractive-full
		--confold
		--confnew
		--confdef
		--confmiss
		--confask
	'

    case $prev in
        --help | --version | --license | -!(-*)[chslSbopitT])
            return
            ;;
    esac

    $split && return

	local arg
	_get_first_arg
    if [[ -z $arg ]]; then
        if [[ $cur == -* ]]; then
            COMPREPLY=( $( compgen -W "$GLOBAL_OPTIONS" -- "$cur") )
        else
			COMPREPLY=( $( compgen -W 'show install remove purge update upgrade fetch history clean moo' -- "$cur") )
		fi
    else
		local args
		_count_args "" "-!(-*)[chslSbopitTdM]"
		# supported options per command
		if [[ "$cur" == -* ]]; then
			case $arg in
				install)
					COMPREPLY=( $( compgen -W '
						-y --assume-yes
						-d --download-only
						--raw-dpkg
						--no-autoremove
						'"$DPKG_OPTIONS""$GLOBAL_OPTIONS" -- "$cur" ) )
					return 0
					;;
				remove|purge)
					COMPREPLY=( $( compgen -W '
						-y --assume-yes
						-d --download-only
						--raw-dpkg
						--no-autoremove
						'"$GLOBAL_OPTIONS" -- "$cur" ) )
					return 0
					;;
				update|upgrade)
					COMPREPLY=( $( compgen -W '
						--no-full
						-y --assume-yes
						-d --download-only
						--no-update
						--raw-dpkg
						--no-autoremove
						'"$DPKG_OPTIONS""$GLOBAL_OPTIONS" -- "$cur" ) )
					return 0
					;;
				fetch)
					COMPREPLY=( $( compgen -W '
						--fetches
						--debian
						--ubuntu
						--country
						--foss'"$GLOBAL_OPTIONS" -- "$cur" ) )
					return 0
					;;
				show)
					COMPREPLY=( $( compgen -W '
						-a --all-versions'"$GLOBAL_OPTIONS" -- "$cur" ) )
					return 0
					;;
				history|clean)
					COMPREPLY=( $( compgen -W "$GLOBAL_OPTIONS" -- "$cur" ) )
					return 0
					;;
				moo)
					COMPREPLY=( $( compgen -W '
						' -- "$cur" ) )
					return 0
					;;
			esac
		else
			local args
			_count_args "" "-!(-*)[chslSbopitTdM]"
			case $arg in
				install)
					COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
						2> /dev/null ) )
					if [[ "$cur" == ./* || "$cur" == /* ]]; then
						_filedir "deb"
					fi
					return 0
					;;
				remove)
					if [[ -f /etc/debian_version ]]; then
						# Debian system
						COMPREPLY=( $( \
							_xfunc dpkg _comp_dpkg_installed_packages $cur ) )
					else
						# assume RPM based
						_xfunc rpm _rpm_installed_packages
					fi
					return 0
					;;
				history)
					case $args in
						2)
							COMPREPLY=( $( compgen -W 'info undo redo clear' \
								-- "$cur" ) )
							return 0
							;;
						3)
							return 0
							;;
					esac
					;;
				show|list)
					COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
						2> /dev/null ) )
					return 0
					;;
					moo)
					COMPREPLY=( $( compgen -W 'moo' \
						-- "$cur" ) )
					return 0
					;;
			esac
		fi
    fi

} &&
complete -F _nala nala

# vim:set ft=sh ts=4 sw=4 noet:
