# vim: ft=zsh noet
if [[ $1 == info ]]; then
	FSB_INFO_DEPEND=gentoo_flavor
	FSB_INFO_BASE=%s
	FSB_INFO_DIR=%s-buildpkg
	if ! (($+FSB_TARGET_NAME)); then
		FSB_INFO_SKIP=1
	fi
	return
elif [[ $1 != build ]]; then
	die "unimplemented action: $*"
fi

local pkgdir f
pkgdir=$FSB_BASE_DIR/packages/$FSB_TARGET_NAME
mkdir -p $pkgdir || die "Could not make directory: $pkgdir"
mkdir -p $build_dir/var/portage/packages \
	|| die "Could not make directory: $build_dir/var/portage/packages"
printf "%s\n" "trymount --bind ${(qqq)pkgdir} var/portage/packages" \
	>>$build_dir/chroot.conf \
	|| die "Could not write package mount information"
$build_dir/chroot.sh /bin/bash --login \
	-c 'eix-installed -= -a | xargs quickpkg --include-unmodified-config y' \
	|| die "Building binary packages failed"
if [[ -e $FSB_BASE_DIR/signify/key.sec ]]; then
	for f in $FSB_BASE_DIR/packages/$FSB_TARGET_NAME/**/(Packages|*.tbz2)(.); do
		signify -S -s $FSB_BASE_DIR/signify/key.sec -m $f -x $f.sig || die "Failed to sign: ${(qqq)f}"
	done
fi