#!/usr/bin/env zsh

use scwrypts/environment

#####################################################################

USAGE__options="
	--all   print the whole environment instead of individual environment variables
"

USAGE__args="
	  \$1      must be the name of the environment variable
	  \$2...   any additional arguments to forward to utils.environment.check
"

USAGE__description="
	Wrapper API for utils.environment.check to get environment variable values
	from non-zsh scwrypts environments. Value of the variable is written
	to stdout, and returns an error status if the variable is required
	and missing.

	------ utils.environment.check help ------

	$(utils.environment.check --help 2>&1 | sed 's/^/ > /g' | grep -v 'usage')

	------ utils.environment.check help ------
"

#####################################################################

MAIN() {
	local MODE=single-variable
	local ARGS=() ARGS_FORCE=true

	eval "${ZSHPARSEARGS}"

	case $MODE in
		( single-variable )
			utils.environment.check --print-value ${ARGS[@]}
			;;
		( all )
			scwrypts.environment.get-user-json
			;;
	esac
}

MAIN.parse() {
	local PARSED=0

	case $1 in
		( --all ) PARSED=1; MODE=all ;;
	esac

	return $PARSED
}


MAIN.parse.validate() {
	case $MODE in
		( single-variable )
			[[ "${#ARGS[@]}" -eq 0 ]] \
				&& echo.error "missing variable / lookup path to check"
			;;
		( all )
			[[ "${#ARGS[@]}" -gt 0 ]] \
				&& echo.error "unexpected variable name with '--all' flag"
			;;
	esac
}
