# usage: call <name> [<arguments>] [-- <parameters>]
# where:
#   <name> is a dependency name as defined by dofile `default.<name>.do`
#   <arguments> are named arguments to pass to dependency in following forms:
#       foo=bar  -- sets argument foo to value "bar"
#       foo:     -- unifies argument foo with variable foo in the caller
#       foo:bar  -- unifies argument foo with variable bar in the caller
#   <parameters> are arbitrary positional arguments

local name
local -A prev_vars

prev_vars=( "${(kv)vars[@]}" )
vars=( )

name=$1
shift

# parse variable assignments
while (( $# )); do
	case $1 in
		(*=*)
			fresh ${1%%=*}
			setvar ${1%%=*} ${1#*=}
			;;
		(*:)
			if (($+prev_vars[${1%:}])); then
				vars[${1%:}]=$prev_vars[${1%:}]
			else
				fresh ${1%:}
				prev_vars[${1%:}]=$vars[${1%:}]
			fi
			;;
		(*:*)
			if (($+prev_vars[${1#*:}])); then
				vars[${1%:}]=$prev_vars[${1#*:}]
			else
				fresh ${1%:}
				prev_vars[${1%:}]=$vars[${1#*:}]
			fi
			;;
		(--)   shift; break;;
		(*)    die "$name: unrecognised argument: ${(qqq)1}";;
	esac
	shift
done

call.$name "$@" || die "$0: ${(qqq)name} returned error"

vars=( "${(kv)prev_vars[@]}" )

# vim: fileencoding=utf8 ft=zsh noet sw=4 ts=4 sts=4