# vim: ft=zsh noet
if [[ $1 == info ]]; then
	fsb_infer_gentoo_settings

	FSB_INFO_BASE=$FSB_BUILD_DIR/gentoo-${STAGE_GENTOO_PROFILE//\//_}
	FSB_INFO_BASE+=-$STAGE_GENTOO_ARCH
	[[ $STAGE_GENTOO_ARCH != $STAGE_GENTOO_MACHINE ]] && \
		FSB_INFO_BASE+=-$STAGE_GENTOO_MACHINE
	FSB_INFO_BASE+=-$STAGE_GENTOO_BRANCH
	FSB_INFO_BASE+=-p$STAGE_PORTAGE_TS
	FSB_INFO_BASE+=-bsp$STAGE_OVERLAY_REVNO[ccx-bootstrap]
	FSB_INFO_DIR=%s-conf
	FSB_INFO_DEPEND=gentoo_autobuild_stage

	return
elif [[ $1 != build ]]; then
	die "unimplemented action: $*"
fi

local dir profile profile_link accept_keywords profile_expected

case $STAGE_GENTOO_BRANCH in
	(stable)
		accept_keywords=$STAGE_GENTOO_ARCH;;
	(testing)
		accept_keywords="~$STAGE_GENTOO_ARCH";;
	(*)
		die "STAGE_GENTOO_BRANCH can be either stable or testing, not $STAGE_GENTOO_ARCH";;
esac

cp $FSB_TEMPLATE_DIR/chroot.sh $build_dir/ || die

case $STAGE_GENTOO_ARCH in
	(x86) echo 'SETARCH=i386' >> $build_dir/chroot.conf || die;;
	(amd64) echo 'SETARCH=x86_64' >> $build_dir/chroot.conf || die;;
esac

cat >>$build_dir/chroot.conf <<EOF
PORTDIR=${(q)FSB_PORTAGE_SNAPSHOTS}/${STAGE_PORTAGE_TS}
EOF

if ((${VAR_TMP_TMPFS:-0})); then
	print -r 'mkdir -p var/tmp' >>$build_dir/chroot.conf || die
	if (($+TMPFS_SIZE)); then
		print -r "trymount -t tmpfs -o size=$TMPFS_SIZE tmpfs var/tmp" >>$build_dir/chroot.conf || die
	else
		print -r "trymount -t tmpfs tmpfs var/tmp" >>$build_dir/chroot.conf || die
	fi
fi

mkdir -p $build_dir/usr/portage/gentoo || die

for dir in $dir in $build_dir/etc/portage/package.{mask,keywords,use}; do
	if [[ -f $dir ]]; then
		mv $dir $dir.orig || die
		mkdir $dir || die
		mv $dir.orig $dir/orig || die
	fi
done

mv $build_dir/etc/portage/make.conf{,.orig} || die
cat >>$build_dir/etc/portage/make.conf <<EOF
ACCEPT_KEYWORDS="${accept_keywords}"

EMERGE_DEFAULT_OPTS="--verbose --quiet-build=y --keep-going"
MAKEOPTS="${MAKEOPTS:--j$(cat /proc/cpuinfo | grep '^processor' | wc -l)}"

PORTDIR="/usr/portage/gentoo"
PORTDIR_OVERLAY="/var/portage/local"
PKGDIR="/var/portage/packages"
DISTDIR="/var/portage/distfiles"
PORT_LOGDIR="/var/log/portage"

PORTAGE_ELOG_CLASSES="log info warn error"
PORTAGE_ELOG_SYSTEM="save_summary save"
EOF

mkdir -p $build_dir/var/portage/{distfiles,packages,local/{metadata,profiles/custom}} || die
echo local >$build_dir/var/portage/local/profiles/repo_name || die
echo 'masters = gentoo' >$build_dir/var/portage/local/metadata/layout.conf || die

profile_link="$(readlink $build_dir/etc/portage/make.profile)" || die "could not read $build_dir/etc/portage/make.profile"
[[ -n $profile_link ]] || die "$build_dir/etc/portage/make.profile is invalid"
profile_expected=../usr/portage/gentoo/profiles/$STAGE_GENTOO_PROFILE
[[ $profile_link != $profile_expected ]] || \
	die $'mismatched profile\nexpected: '${(qqq)profile_expected}$'\ngot: '${(qqq)profile_link}
mv $build_dir/etc/portage/make.profile{,.orig} || die

#profile=/usr/portage/gentoo/${${profile_link#*/portage}#/gentoo}
profile=/etc/portage/$profile_link
ln -s ../../var/portage/local/profiles/custom $build_dir/etc/portage/make.profile || die
print -r - $profile >>$build_dir/var/portage/local/profiles/custom/parent || die
fsb_gentoo_overlay_add ccx-bootstrap
fsb_gentoo_profile_add -o ccx-bootstrap arch/$STAGE_GENTOO_MACHINE

local ovr
for ovr in musl; do
	if [[ -e $build_dir/var/portage/$ovr ]]; then
		sed -i "s|PORTDIR_OVERLAY=[\"']|&/var/portage/$ovr |" $build_dir/etc/portage/make.conf || die
	fi
done

if (($+EATMYDATA)); then
	mkdir -p $build_dir/etc/portage/package.keywords || die "could not create directory: $build_dir/etc/portage/package.keywords"
	echo 'sys-fs/libeatmydata' >>$build_dir/etc/portage/package.keywords/fsb || die "could not unmask sys-fs/libeatmydata"
	fsb_emerge -n libeatmydata
fi
# vim: ft=zsh noet