#
# Build
#

if {[have_include power_on/qemu]} {
	puts "\nAbort, using Qemu is not recommended.\n"
	exit 0
}

set build_components {
	core init
	timer
	drivers/audio
	server/report_rom
	server/dynamic_rom
	app/audio_player
}

source ${genode_dir}/repos/base/run/platform_drv.inc
append_platform_drv_build_components

build $build_components

create_boot_directory

#
# Config
#

append config {
<config>
	<parent-provides>
		<service name="ROM"/>
		<service name="RAM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="CAP"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
		<service name="SIGNAL" />
	</parent-provides>
	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>
	<default caps="100"/>

	<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="4M"/>
		<provides> <service name="Report"/> <service name="ROM"/> </provides>
		<config verbose="yes">
			<rom>
				<policy label_prefix="" report="audio_player -> current_track"/>
			</rom>
		</config>
	</start>

	<start name="dynamic_rom">
		<resource name="RAM" quantum="4M"/>
		<provides><service name="ROM"/></provides>
		<config verbose="yes">
			<rom name="playlist">
				<inline description="initial playlist">
<playlist>
	<trackList>
		<track><location>foo.mp3</location></track>
		<track><location>foo.ogg</location></track>
		<track><location>foo.flac</location></track>
	</trackList>
</playlist>
				</inline>
				<sleep milliseconds="30000"/>
				<inline description="initial playlist">
<playlist>
	<trackList>
		<track><location>foo.ogg</location></track>
	</trackList>
</playlist>
				</inline>
				<sleep milliseconds="30000"/>
			</rom>
			<rom name="audio_player.config">
				<inline description="initial config">
<config ld_verbose="yes" state="playing" playlist_mode="repeat">
	<report progress="yes" interval="1" playlist="yes"/>
	<libc/>
	<vfs>
		<rom name="foo.mp3"/>
		<rom name="foo.flac"/>
		<rom name="foo.ogg"/>
	</vfs>
</config>
				</inline>
				<sleep milliseconds="10000"/>
				<inline description="select third track">
<config state="playing" playlist_mode="repeat" selected_track="3"/>
				</inline>
				<sleep milliseconds="5000"/>
				<inline description="select first track">
<config state="playing" playlist_mode="repeat" selected_track="1"/>
				</inline>
				<sleep milliseconds="5000"/>
				<inline description="select second track">
<config state="playing" playlist_mode="repeat" selected_track="2"/>
				</inline>
				<sleep milliseconds="5000"/>
			</rom>
		</config>
	</start>

	<start name="audio_drv">
		<binary name="} [audio_drv_binary] {"/>
		<resource name="RAM" quantum="8M"/>
		<provides> <service name="Audio_out"/> </provides>
		<!--
		<config alsa_device="hw:CARD=PCH,DEV=0"/>
		-->
	</start>

	<start name="audio_player" caps="150">
		<resource name="RAM" quantum="24M"/>
		<route>
			<service name="ROM" label="config">
				<child name="dynamic_rom" label="audio_player.config"/> </service>
			<service name="ROM" label="playlist"> <child name="dynamic_rom"/> </service>
			<service name="Report"> <child name="report_rom"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
</config>}

install_config $config

#
# Check audio files
#

if {[expr ![file exists bin/foo.flac] || ![file exists bin/foo.mp3] || ![file exists bin/foo.ogg]]} {
	puts ""
	puts "One or all audio files are missing. Please put 'foo.flac', 'foo.mp3' and"
	puts "'foo.ogg' into './bin' and execute this run script again. Note, that the"
	puts "duration of each of those files has to be below 10 seconds for this run"
	puts "script to work properly."
	puts ""
	exit 1
}

#
# Boot modules
#

append boot_modules {
	core init timer dynamic_rom report_rom } [audio_drv_binary] {
	ld.lib.so libc.lib.so vfs.lib.so libm.lib.so zlib.lib.so
	avcodec.lib.so avformat.lib.so avutil.lib.so
	avresample.lib.so
	audio_player
	foo.mp3 foo.flac foo.ogg
}

append_platform_drv_boot_modules

build_boot_image $boot_modules

append qemu_args "  -soundhw es1370 "

run_genode_until forever
