#
# \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 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

create_boot_directory

append config {
	<config>
		<parent-provides>
			<service name="ROM"/>
			<service name="LOG"/>
			<service name="RM"/>
			<service name="CPU"/>
			<service name="PD"/>
			<service name="IRQ"/>
			<service name="IO_PORT"/>
			<service name="IO_MEM"/>
		</parent-provides>
		<default-route>
			<any-service> <any-child/> <parent/> </any-service>
		</default-route>
		<default caps="100"/>
		<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">
			<resource name="RAM" quantum="1M"/>
			<provides>
				<service name="Uart"/>
				<service name="Terminal"/>
			</provides>
			<config>
				<policy label_prefix="terminal_mux" uart="1" detect_size="yes"/>
			</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" />
				<vfs>
					<dir name="subsystems">
						<inline name="noux.subsystem">
							<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="screen" />

										<!-- 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>
						</inline>
						<inline name="ram_eater.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>
						</inline>
						<inline name="signal.subsystem">
							<subsystem name="signal" help="signalling test program">
								<resource name="RAM" quantum="5M" />
								<binary name="test-signal" />
							</subsystem>
						</inline>
					</dir>
				</vfs>
			</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 posix.lib.so
	libc.lib.so vfs.lib.so libm.lib.so libc_noux.lib.so libc_terminal.lib.so ncurses.lib.so
	vim.tar uart_drv
}

set fiasco_serial_esc_arg ""

build_boot_image $boot_modules

append qemu_args " -nographic "

#
# On all 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 qemu_args " -serial file:kdb.log "
append qemu_args " -serial mon:stdio"

run_genode_until forever