#
# \brief  Pistachio-specific test-environment supplements
# \author Norman Feske
# \date   2010-08-25
#
# This file is meant to be used as '--include' argument for 'tool/run'.
#


##
# Install files needed to boot via PXE
#
proc install_pxe_bootloader_to_run_dir { } {
	exec cp [genode_dir]/tool/boot/pulsar [run_dir]/boot/pulsar
	exec cp [genode_dir]/tool/boot/bender [run_dir]/boot/bender
}


##
# Read the location of the Pistachio user directory from 'etc/pistachio.conf'
#
proc pistachio_user_dir { } {
	global _pistachio_user_dir

	if {![info exists _pistachio_user_dir]} {
		if {[file exists etc/pistachio.conf]} {
			set _pistachio_user_dir [exec sed -n "/^PISTACHIO_USER_BUILD_DIR/s/^.*=\\s*//p" etc/pistachio.conf]
		} else {
			set _pistachio_user_dir "[pwd]/l4"
		}
	}
	return $_pistachio_user_dir
}


##
# Read the location of the Pistachio kernel directory from 'etc/pistachio.conf'
# or return a good heuristic
#
proc pistachio_kernel { } {
	global _pistachio_kernel

	if {![info exists _pistachio_kernel]} {
		if {[file exists etc/pistachio.conf]} {
			set _pistachio_kernel [exec sed -n "/^PISTACHIO_KERNEL/s/^.*=\\s*//p" etc/pistachio.conf]
			if {$_pistachio_kernel == ""} {
				set _pistachio_kernel [file dirname [file dirname [pistachio_user_dir]]]/kernel/build/x86-kernel
			}
		} else {
			set _pistachio_kernel "[pwd]/bin/kernel"
		}
	}
	return $_pistachio_kernel
}


##
# Return whether the kernel is provided from the outside
#
proc kernel_external { } {
	if {[pistachio_kernel] == "[pwd]/bin/kernel"} { return 0 }
	return 1
}


##################################
## Test framework API functions ##
##################################

proc create_boot_directory { } {
	exec rm -rf   [run_dir]
	exec mkdir -p [run_dir]/genode
	exec mkdir -p [run_dir]/pistachio
}


proc build_boot_image {binaries} {

	#
	# Collect contents of the ISO image
	#
	copy_and_strip_genode_binaries_to_run_dir $binaries

	if {![kernel_external] && ![file exists [pistachio_kernel]]} { build { kernel } }

	exec cp [pistachio_kernel] [run_dir]/pistachio/kernel
	exec cp [pistachio_user_dir]/serv/sigma0/sigma0 [run_dir]/pistachio
	exec cp [pistachio_user_dir]/util/kickstart/kickstart [run_dir]/pistachio

	install_iso_bootloader_to_run_dir

	#
	# Generate grub config file
	#
	# The core binary is part of the 'binaries' list but it must
	# appear right after 'sigma0' as boot module. Hence the special case.
	#
	set fh [open "[run_dir]/boot/grub/menu.lst" "WRONLY CREAT TRUNC"]
	puts $fh "timeout 0"
	puts $fh "default 0"
	puts $fh "\ntitle Genode on L4ka::Pistachio"
	puts $fh " kernel /pistachio/kickstart"
	puts $fh " module /pistachio/kernel"
	puts $fh " module /pistachio/sigma0"
	puts $fh " module /genode/core"
	puts $fh " module /genode/config"
	foreach binary $binaries {
		if {$binary != "core"} {
			puts $fh " module /genode/$binary" } }
	close $fh

	#
	# Install PXE bootloader pulsar
	#
	install_pxe_bootloader_to_run_dir

	create_iso_image_from_run_dir

	#
	# Generate pulsar config file
	#
	set fh [open "[run_dir]/config-52-54-00-12-34-56" "WRONLY CREAT TRUNC"]
	puts $fh " exec /boot/bender"
	puts $fh " load /pistachio/kickstart"
	puts $fh " load /pistachio/kernel"
	puts $fh " load /pistachio/sigma0"
	puts $fh " load /genode/core"
	puts $fh " load /genode/config"
	puts $fh " load /genode/config"
	foreach binary $binaries {
		if {$binary != "core"} {
			puts $fh " load /genode/$binary" } }
	close $fh

	#
	# Generate pulsar config file pointing to the config file above.
	#
	if {[info exists ::env(PXE_TFTP_DIR_BASE)] && [info exists ::env(PXE_TFTP_DIR_OFFSET)]} {
		exec ln -nfs "[pwd]" "$::env(PXE_TFTP_DIR_BASE)$::env(PXE_TFTP_DIR_OFFSET)"

		set tftp_base ""
		if {[get_cmd_switch --tftp-absolute]} {
			set tftp_base $::env(PXE_TFTP_DIR_BASE)
		}

		set fh [open "$::env(PXE_TFTP_DIR_BASE)$::env(PXE_TFTP_DIR_OFFSET)/config-00-00-00-00-00-00" "WRONLY CREAT TRUNC"]
		puts $fh " root $tftp_base$::env(PXE_TFTP_DIR_OFFSET)/[run_dir]"
		puts $fh " config config-52-54-00-12-34-56"
		close $fh
	}
}


proc run_genode_until {{wait_for_re forever} {timeout_value 0} {running_spawn_id -1}} {
	#
	# If a running_spawn_id is specified, wait for the expected output
	#
	if {$running_spawn_id != -1} {
		wait_for_output $wait_for_re $timeout_value $running_spawn_id
		return
	}

	#
	# Try to use one of the supported backends for running the scripts
	#
	if {[is_amt_available]} {
		spawn_amt $wait_for_re $timeout_value
		return
	}
	if {[is_qemu_available]} {
		spawn_qemu $wait_for_re $timeout_value
		return
	}

	global run_target
	puts stderr "Error: Can't execute automatically on target '$run_target'"
	exit -1
}