#!/usr/bin/env zsh
use scwrypts/virtualenv/update
use scwrypts/virtualenv/delete
#####################################################################

USAGE__options='
	-t, --type    type of virtual environment to refresh
	'

USAGE__description='
	drops, recreates, and updates the virtual environment used
	by scwrypts in the appropriate group
	'

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

MAIN() {
	local ENV_GROUP ENV_TYPE

	local ERRORS=0 _S
	while [[ $# -gt 0 ]]
	do
		_S=1
		case $1 in
			( -t | --type  )
				[ $2 ] && ((_S+=1)) \
					|| echo.error "missing env type" \
					|| break

				ENV_TYPE="$2"
				;;
		esac
		shift $_S
	done

	[ $ENV_TYPE ] || ENV_TYPE=$(\
		echo ${SCWRYPTS_VIRTUALENV__AVAILABLE_VIRTUALENVS[@]} \
			| sed 's/ \+/\n/' \
			| utils.fzf 'select an environment to refresh' \
		)
	[ $ENV_TYPE ] || echo.error "no environment type selected"

	utils.check-errors --fail

	##########################################
	: \
		&& scwrypts.virtualenv.delete "$ENV_TYPE" \
		&& scwrypts.virtualenv.update "$ENV_TYPE" \
		;
}
