# vim: ft=zsh
if [[ $1 == info ]]; then
	if ! (($+STAGE_ALPINE_VIRTUALENV_NAME)); then
		die "STAGE_ALPINE_VIRTUALENV_NAME needs to be defined"
	fi
	if ! (($+STAGE_ALPINE_VIRTUALENV_PACKAGES)); then
		die "STAGE_ALPINE_VIRTUALENV_PACKAGES needs to be defined"
	fi
	FSB_INFO_DEPEND=alpine_packages
	FSB_INFO_BASE=%s
	FSB_INFO_DIR=%s-virtualenv
	return
elif [[ $1 != build ]]; then
	die "unimplemented action: $*"
fi

local virtualenv_dir=/usr/local/virtualenv/$STAGE_ALPINE_VIRTUALENV_NAME
$build_dir/chroot.sh /usr/bin/virtualenv --system-site-packages $virtualenv_dir || \
	die "Could not initialize virtualenv"

local -a packages
local pkg

for pkg in $STAGE_ALPINE_VIRTUALENV_PACKAGES; do
	case $pkg in
		(/*)
			cp $pkg $build_dir/tmp/ || die
			packages+=( /tmp/$pkg:t )
			;;
		(*) packages+=( $pkg );;
	esac
done
$build_dir/chroot.sh $virtualenv_dir/bin/pip install $packages || \
	die "Could not install python packages: ${(j: :)${(q@)STAGE_ALPINE_VIRTUALENV_PACKAGES}}"