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

$build_dir/chroot.sh /sbin/apk add --verbose s6 s6-rc s6-portable-utils s6-linux-utils rsync zsh || \
	die "Could not install alpine packages: s6 s6-rc"

fsapply $build_dir $FSB_TEMPLATE_DIR/s6-init.fslist || \
	die "fsapply failed"

#$build_dir/chroot.sh /root/s6-init/install || \
#	die "s6-linux-init-maker failed"

s6-linux-init-maker \
	-b /bin \
	-p /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
	$build_dir/etc/s6-linux-init || \
	die "s6-linux-init-maker failed"
	#-G "/sbin/getty 38400 tty2" \

local svc
for svc in $STAGE_ALPINE_S6RC_SERVICES; do
	fsapply $build_dir/etc/s6-rc/source $FSB_TEMPLATE_DIR/s6-rc.$svc.fslist || \
		die "fsapply failed"
	printf "%s\n" $svc >>$build_dir/etc/s6-rc/source/ok-all/contents
done

$build_dir/chroot.sh /etc/s6-rc/compile || \
	die "s6-rc-compile failed"