#compdef synth

_synth () {

    local cmds findorigin portsdir
    local -a subcmds
    local context state line
    typeset -A val_args

    cmds=(
	'status:Dry-run: What "upgrade-system" or [ports] would build'
	'configure:Brings up interactive configuration menu'
	'upgrade-system:Incrementally and fully upgrade host system'
	'prepare-system:Upgrade Synth repository for host system'
	'rebuild-repository:Rebuilds local Synth repository on command'
	'purge-distfiles:Deletes obsolete source distribution files'
	'status-everything:Dry-run: Shows what "everything" would build'
	'everything:Builds entire ports tree and rebuilds repository'
	'version:Displays version, description and usage summary'
	'help:Displays 1-line summary of each command'
	'build:Incrementally build [ports] list, offer to install'
	'just-build:Incrementally build [ports] list'
	'install:Incrementally build [ports] list and install'
	'force:Unconditionally build [ports] list, offer to install'
	'test:Unconditionally build [ports] in Developer mode'
    )

    portsdir=${PORTSDIR:-/usr/ports}
    findorigin='port:port origin:_files -X "%Bports%b" -W "${portsdir}" -/'
    subcmds=(
	${findorigin}
	'file:path to file containing one port origin per line:_files'
    )

    # we are in second argument
    if ((CURRENT == 2)); then
	_describe "Synth commands" cmds

    # the main command has been typed, now we take care of the action
    elif ((CURRENT == 3)); then
	case $words[2] in
	    status|build|just-build|install|force|test)
		_alternative $subcmds
	esac
	return
    elif ((CURRENT > 3)); then
        if [ ! -d "${portsdir}/$words[3]" ]; then
           return;
        fi
	case $words[2] in
	    status|build|just-build|install|force|test)
		_alternative ${findorigin}
	esac
	return
    fi
}

_synth "$@"
