#compdef envstore
## vim:ft=zsh
## completion for envstore release-3
## Daniel Friesel <derf@derf.homelinux.org>
## https://derf.homelinux.org/~derf/dotfiles/completion/_envstore
## see also: http://git.cloudconnected.org/?p=envstore.git;a=summary

local -a options options_nofile

options=(
	'clear:forget stored parameters'
	'eval:generate shell code to set parameters'
	'list:list saved parameters in human-readable format'
	'rm:delete saved parameter'
	'save:save parameter'
)

options_nofile=(
	'save:save parameter'
)

function _saved_param () {
	_wanted parameter expl 'saved parameter' \
	compadd $(envstore eval | cut -d ' ' -f 2 | cut -d '=' -f 1)
}

if (( CURRENT == 2 )) {
	if [[ -r /tmp/envstore-$UID ]] {
		_describe command options
	} else {
		_describe command options_nofile
	}
} elif (( CURRENT == 3 )) {
	case ${words[2]} in
		e*)
			_arguments -s '2::option:(-e)'
		;;
		r*)
			_saved_param
		;;
		s*)
			_parameters -g '(scalar|integer)*export*'
		;;
		*)
			_message 'no more arguments'
	esac
} elif [[ $CURRENT == 4 && ${words[2]} == save ]] {
	_message 'value'
} else {
	_message 'no more arguments'
}
