if {[have_spec foc] || [have_spec linux] ||
    [have_spec rpi3] ||
    [expr [have_spec imx53] && [have_spec trustzone]]} {
	puts "Run script is not supported on this platform."
	exit 0
}

set on_hardware [expr ![have_include power_on/qemu]]

create_boot_directory

import_from_depot [depot_user]/src/[base_src] \
                  [depot_user]/pkg/[drivers_nic_pkg] \
                  [depot_user]/src/init \
                  [depot_user]/src/nic_bridge

build { app/ping }

proc dst_ip { } {
	if {![have_include power_on/qemu]} {
		return "10.0.0.2"
	} else {
		return "10.0.2.2"
	}
}

append 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"/>
	</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>

	<start name="drivers" caps="1000">
		<resource name="RAM" quantum="32M" constrain_phys="yes"/>
		<binary name="init"/>
		<route>
			<service name="ROM" label="config"> <parent label="drivers.config"/> </service>
			<service name="Timer"> <child name="timer"/> </service>
			<any-service> <parent/> </any-service>
		</route>
		<provides> <service name="Nic"/> </provides>
	</start>

	<start name="nic_bridge" caps="200">
		<resource name="RAM" quantum="10M"/>
		<provides><service name="Nic"/></provides>
		<config mac="02:02:02:02:03:00">
			<policy label_prefix="ping_1"/>
			<policy label_prefix="ping_2"/>
		</config>
		<route>
		<service name="Nic"> <child name="drivers"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>}

append_if $on_hardware config {
	<start name="ping_2">
		<binary name="ping"/>
		<resource name="RAM" quantum="8M"/>
		<config period_sec="1"
		        verbose="no"
		        count="1"
		        protocol="udp"
		        dst_ip="} [dst_ip] {"
		        dst_port="12345"/>
		<route>
			<service name="Nic"> <child name="nic_bridge"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>}

append config {
	<start name="ping_1">
		<binary name="ping"/>
		<resource name="RAM" quantum="8M"/>
		<config dst_ip="} [dst_ip] {"
		        period_sec="1"
		        verbose="no"
		        count="3"/>
		<route>
			<service name="Nic"> <child name="nic_bridge"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
</config>}

install_config $config
build_boot_image { ping }

proc qemu_nic_model {} {
	if [have_spec x86]         { return e1000 }
	if [have_spec lan9118]     { return lan9118 }
	if [have_spec zynq]        { return cadence_gem }
	return nic_model_missing
}

append qemu_args " -nographic "
append qemu_args " -netdev user,id=net0 "
append qemu_args " -net nic,model=[qemu_nic_model],netdev=net0 "

set done_string ".*\"ping_1\" exited with exit value 0.*\n"

append_if $on_hardware done_string ".*ping_2\] From [dst_ip] Destination Unreachable.*\n"

run_genode_until $done_string 30
