set genext2fs [installed_command genext2fs]
set e2fsck [installed_command e2fsck]

create_boot_directory

import_from_depot genodelabs/src/[base_src] \
                  genodelabs/src/init \
                  genodelabs/src/platform_drv \
                  genodelabs/src/report_rom \
                  genodelabs/src/acpi_drv \
                  genodelabs/src/ahci_drv \
                  genodelabs/src/vfs \
                  genodelabs/src/rump \
                  genodelabs/src/log_terminal \
                  genodelabs/src/noux \
                  genodelabs/src/libc \
                  genodelabs/src/posix \
                  genodelabs/src/bash \
                  genodelabs/src/coreutils

source ${genode_dir}/repos/base/run/platform_drv.inc
set config {
<config>
	<parent-provides>
		<service name="ROM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
		<service name="TRACE"/>
	</parent-provides>

	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>

	<default caps="100"/>

	<resource name="RAM" preserve="2M"/>

	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Timer"/></provides>
	</start> }

append_platform_drv_config

append config {
	<start name="report_rom">
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Report"/> <service name="ROM"/> </provides>
		<config verbose="yes"/>
	</start>

	<start name="ahci_drv">
		<resource name="RAM" quantum="10M" />
		<provides><service name="Block" /></provides>
		<config atapi="yes">
			<report ports="yes"/>
			<default-policy device="0" writeable="yes" />
		</config>
		<route>
			<service name="Report"> <child name="report_rom"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>

	<start name="vfs" caps="400">
		<resource name="RAM" quantum="100M" />
		<provides> <service name="File_system"/> </provides>
		<config>
			<vfs> <rump fs="ext2fs" ram="64M" writeable="yes"/> </vfs>
			<default-policy root="/" writeable="yes"/>
		</config>
	</start>

	<start name="log_terminal">
		<resource name="RAM" quantum="10M" />
		<provides> <service name="Terminal"/> </provides>
	</start>

	<start name="noux" caps="500">
		<binary name="noux"/>
		<resource name="RAM" quantum="100M" />
		<config>
			<fstab>
				<inline name=".bash_profile">
					rm /rw/build.conf
					sleep 2
					ls /rw
				</inline>
				<tar name="bash.tar" />
				<tar name="coreutils.tar" />
				<dir name="dev"> <zero/> <null/> </dir>
				<dir name="rw"> <fs/> </dir>
			</fstab>
			<start name="/bin/bash">
				<env name="TERM" value="screen" />
				<env name="PS1" value="fs:$PWD> " />
				<env name="HOME" value="/" />
				<arg value="--login"/>
			</start>
		</config>
	</start>
</config> }

install_config $config
append boot_modules { }
build_boot_image $boot_modules

#
# Build EXT2-file-system image
#
catch { exec $genext2fs -d etc -b 16384 bin/ext2.raw }

append qemu_args " -nographic -device ahci,id=ahci -boot d "
append qemu_args " -drive id=disk,file=bin/ext2.raw,format=raw,if=none -device ide-hd,drive=disk,bus=ahci.0 "

run_genode_until {\[init -> log_terminal\].*} 30

spawn $e2fsck -y bin/ext2.raw
set fsck_id $spawn_id

run_genode_until "840/16384 blocks" 5 $fsck_id

exec rm -f bin/ext2.raw