if {![have_spec foc] || ![have_spec 32bit]} {
	puts "\nThe Noux GDB scenario is supported on 32-bit Fiasco.OC only\n"
	exit 0
}

source ${genode_dir}/ports/run/noux_gdb.inc

#
# Uncomment the following line when working on the GDB source code. Otherwise,
# the package may get recompiled, yet it does not get reinstalled into 'bin/'.
#
#exec rm -rf noux-pkg/[noux_gdb_pkg_name]/ bin/[noux_gdb_pkg_name]/

set build_components {
	core init drivers/timer noux lib/libc_noux
	drivers/uart
	server/terminal_mux server/terminal_crosslink
	server/terminal_log
	app/cli_monitor
	app/gdb_monitor
}
lappend build_components noux-pkg/[noux_gdb_pkg_name]

lappend build_components test/gdb_monitor
set gdb_target_binary_name test-gdb_monitor

build $build_components

# names of the binaries needed for the GDB monitor test
set gdb_target_binaries {
	ld.lib.so
	libc.lib.so
	libc_log.lib.so
}
lappend gdb_target_binaries ${gdb_target_binary_name}

create_gdb_tar
create_binary_and_source_tars "gdb_target" ${gdb_target_binaries}

create_boot_directory

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

		<start name="timer">
			<resource name="RAM" quantum="1M"/>
			<provides><service name="Timer"/></provides>
			<route> <any-service> <parent/> </any-service> </route>
		</start>

		<start name="uart_drv">}

# use kernel debugger as UART on Fiasco.OC
append_if [have_spec foc] config {
			<binary name="kdb_uart_drv"/>}

append config {
			<resource name="RAM" quantum="1M"/>
			<provides>
				<service name="Uart"/>
				<service name="Terminal"/>
			</provides>
			<config> }

# on Fiasco.OC the kdb_uart_drv is always UART 0
append_if [have_spec foc] config {
				<policy label="terminal_mux" uart="0" detect_size="yes"/> }

# on all other kernels, direct terminal_mux to UART 1 (Qemu stdio, see below)
append_if [expr ![have_spec foc]] config {
				<policy label="terminal_mux" uart="1" detect_size="yes"/> }

append config {
			</config>
			<route> <any-service> <parent/> <any-child/> </any-service> </route>
		</start>
		<start name="terminal_mux">
			<resource name="RAM" quantum="3M"/>
			<provides><service name="Terminal"/></provides>
			<route>
				<service name="Terminal"><child name="uart_drv"/></service>
				<any-service> <parent/> <any-child/> </any-service>
			</route>
			<config>
				<keyboard layout="de"/>
			</config>
		</start>
		<start name="terminal_log">
			<resource name="RAM" quantum="2M"/>
			<provides>
				<service name="LOG"/>
			</provides>
			<route>
				<any-service><child name="terminal_mux"/> <any-child/> <parent/> </any-service>
			</route>
		</start>
		<start name="cli_monitor">
			<resource name="RAM" quantum="3G"/>
			<config>
				<subsystem name="gdb_test" help="GDB test application"> }
append config "
					<binary name=\"${gdb_target_binary_name}\"/> "
append config {
					<resource name="RAM" quantum="50M"/>
				</subsystem>
			</config>
			<route>
				<service name="LOG"><child name="terminal_log"/></service>
				<service name="Terminal"><child name="terminal_mux"/></service>
				<any-service><parent/><any-child/></any-service>
			</route>
		</start>
	</config>
}

install_config $config


#
# Boot modules
#

exec cp ${genode_dir}/os/src/app/cli_monitor/gdb_command_config bin

# generic modules
set boot_modules {
	core init timer ld.lib.so noux terminal_mux terminal_crosslink
	libc.lib.so libm.lib.so libc_noux.lib.so ncurses.lib.so expat.lib.so
	libc_lock_pipe.lib.so libc_log.lib.so libc_terminal.lib.so
	cli_monitor	gdb_monitor terminal_log gdb.tar
	gdb_command_config
	gdb_target.tar
	gdb_target-src.tar
}
lappend boot_modules ${gdb_target_binary_name}

# platform-specific modules
lappend_if [expr ![have_spec foc]] boot_modules uart_drv
lappend_if [have_spec foc]         boot_modules kdb_uart_drv

set fiasco_serial_esc_arg ""

build_boot_image $boot_modules

append qemu_args " -nographic "

run_genode_until forever

exec rm bin/gdb.tar
