#
# \brief  Example for hosting the genode.org website on Genode
# \author Norman Feske
# \date   2012-08-16
#
# This run script uses the lighttpd web server to host the genode.org website.
# When executed the first time, the genode.org website is downloaded to
# 'bin/genode_org/website'. When the web server is up, you may point your web
# browser to http://localhost:5555
#

if {[have_spec odroid_xu]} {
	puts "Run script does not support this platform."
	exit 0
}

set build_components {
	core init
	drivers/timer
	app/lighttpd
}

set use_usb_driver      [expr [have_spec omap4] || [have_spec arndale] || [have_spec rpi]]
set use_nic_driver      [expr !$use_usb_driver]

if {$use_usb_driver}    { set network_driver "usb_drv" }
if {$use_nic_driver}    { set network_driver "nic_drv" }

lappend_if $use_usb_driver      build_components drivers/usb
lappend_if $use_nic_driver      build_components drivers/nic
lappend_if [have_spec gpio]     build_components drivers/gpio

proc gpio_drv { } { if {[have_spec rpi] && [have_spec hw]}  { return hw_gpio_drv }
                    if {[have_spec rpi] && [have_spec foc]} { return foc_gpio_drv }
                    return gpio_drv }

source ${genode_dir}/repos/base/run/platform_drv.inc
append_platform_drv_build_components

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> <parent/> <any-child/> </any-service>
	</default-route>
	<default caps="100"/>
	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Timer"/></provides>
	</start>}

append_if [have_spec gpio] config "
	<start name=\"[gpio_drv]\">
		<resource name=\"RAM\" quantum=\"4M\"/>
		<provides><service name=\"Gpio\"/></provides>
		<config/>
	</start>"

append_if $use_usb_driver config {
	<start name="usb_drv">
		<resource name="RAM" quantum="12M"/>
		<provides>
			<service name="Nic"/>
		</provides>
		<config ehci="yes">
			<nic mac="02:00:00:00:01:01" />
		</config>
	</start>}

append_if $use_nic_driver config {
	<start name="nic_drv">
		<binary name="} [nic_drv_binary] {"/>
		<resource name="RAM" quantum="4M"/>
		<provides><service name="Nic"/></provides>
	</start>}

append_platform_drv_config

append config {
	<start name="lighttpd" caps="1000">
		<resource name="RAM" quantum="1G" />
		<config>
			<arg value="lighttpd" />
			<arg value="-f" />
			<arg value="/etc/lighttpd/lighttpd.conf" />
			<arg value="-D" />
			<vfs>
				<dir name="dev">
					<log/>
					<null/>
				</dir>
				<tar name="genode_org.tar"/>
			</vfs>
			<libc stdin="/dev/null" stdout="/dev/log" stderr="/dev/log"/>
		</config>
	</start>}

append config {
</config>}

install_config $config

#
# Create TAR archive containting the web-server configuration and the
# website content.
#

# lighttpd configuration
exec mkdir -p bin/genode_org/etc/lighttpd
set fd [open "bin/genode_org/etc/lighttpd/lighttpd.conf" w]
puts $fd {
server.port                    = 80
server.document-root           = "/website"
server.event-handler           = "select"
server.network-backend         = "write"
server.max-keep-alive-requests = 0
index-file.names               = ( "index", "index.html", "index.htm" )
mimetype.assign                = (
                                   ".png"  => "image/png",
                                   ".jpg"  => "image/jpeg",
                                   ".jpeg" => "image/jpeg",
                                   ".gif"  => "image/gif",
                                   ".css"  => "text/css",
                                   ".html" => "text/html",
                                   ".htm"  => "text/html",
                                   ""      => "text/html",
                                 ) }
close $fd

# mirror of genode.org website
if {![file exists bin/genode_org/website/index]} {
	puts "mirroring genode.org website to bin/genode_org/website..."
	exec mkdir -p bin/genode_org/website

	# ignore wget errors
	catch {
		exec wget -nH -Lrc -P bin/genode_org/website http://genode.org
	}
}

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

#
# Boot modules
#

# generic modules
set boot_modules {
	core init timer ld.lib.so
	libc.lib.so libm.lib.so posix.lib.so
	lwip.lib.so zlib.lib.so
	lighttpd genode_org.tar
}

# platform-specific modules
lappend_if $use_usb_driver           boot_modules usb_drv
lappend_if $use_nic_driver           boot_modules [nic_drv_binary]
lappend_if [have_spec gpio]          boot_modules [gpio_drv]

append_platform_drv_boot_modules

build_boot_image $boot_modules

append_if [have_spec x86]     qemu_args " -net nic,model=e1000 "
append_if [have_spec lan9118] qemu_args " -net nic,model=lan9118 "

append qemu_args " -net user -redir tcp:5555::80 "
append qemu_args " -nographic -serial mon:stdio "

run_genode_until forever
