#
# \brief  Example for using the terminal_mux server over UART
# \author Norman Feske
# \date   2013-02-20
#

#
# On Linux, we don't have a UART driver, on which this run script depends.
#
if {[have_spec linux]} { puts "Run script does not support Linux"; exit 0 }

set build_components {
	core init noux/minimal lib/libc_noux app/cli_monitor test/bomb test/signal
	test/resource_yield drivers/timer drivers/uart server/terminal_mux
	server/terminal_log noux-pkg/vim
}

build $build_components

exec tar cfv bin/vim.tar -h -C bin/vim .

create_boot_directory

append config {
	<config>
		<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="4M"/>
			<provides>
				<service name="Terminal"/>
			</provides>
			<route>
				<any-service> <child name="uart_drv" /> <parent/> <any-child /> </any-service>
			</route>
		</start>
		<start name="terminal_log">
			<resource name="RAM" quantum="2M"/>
			<provides>
				<service name="LOG"/>
			</provides>
			<route>
				<any-service> <child name="terminal_mux" /> <parent/> <any-child/> </any-service>
			</route>
		</start>
		<start name="cli_monitor">
			<resource name="RAM" quantum="100M"/>
			<config>
				<preservation name="RAM" quantum="16M" />
				<subsystem name="noux" help="VIM executed in the noux runtime">
					<resource name="RAM" quantum="16M" />
					<config>
						<fstab> <tar name="vim.tar" /> </fstab>
						<start name="/bin/vim">
							<env name="TERM" value="linux" />

							<!-- Deactivate the loading of plugins. Otherwise, vim will
							     attempt to use a sub shell for pattern matching -->
							<arg value="--noplugin" />

							<!-- Do not use swap file. Any attempt to create of would
							     fail because we are on a read-only file system -->
							<arg value="-n" />

							<!-- Use the nocompatible mode, which is much nicer than
							     the plain vi mode -->
							<arg value="-N" />

							<!-- Permanently display status bar -->
							<arg value="--cmd" />
							<arg value="set laststatus=2" />

							<!-- Enable highlighted search results -->
							<arg value="--cmd" />
							<arg value="set hls" />

						</start>
					</config>
				</subsystem>
				<subsystem name="signal" help="signalling test program">
					<resource name="RAM" quantum="5M" />
					<binary name="test-signal" />
				</subsystem>
				<subsystem name="ram_eater" help="resource-yield test program">
					<resource name="RAM" quantum="25M" />
					<binary name="test-resource_yield" />
					<config child="yes" expand="yes" period_ms="100" />
				</subsystem>
			</config>
			<route>
				<any-service> <child name="terminal_mux" /> <any-child/> <parent/> </any-service>
			</route>
		</start>
	</config>
}

install_config $config


#
# Boot modules
#

# generic modules
set boot_modules {
	core init timer ld.lib.so noux terminal_mux terminal_log
	test-signal cli_monitor test-resource_yield
	libc.lib.so libm.lib.so libc_noux.lib.so libc_terminal.lib.so ncurses.lib.so
	vim.tar
}

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 " -m 256 "
append qemu_args " -nographic "

#
# On Fiasco.OC, we use only one UART via the kernel debugger.
# On all other kernels, we write the core debug output to the kdb.log file,
# and use qemu's stdio as the UART used by terminal_mux.
#
append_if [expr ![have_spec foc]] qemu_args " -serial file:kdb.log "
append qemu_args " -serial mon:stdio"

run_genode_until forever

exec rm bin/vim.tar
