assert_spec linux

create_boot_directory

import_from_depot [depot_user]/src/[base_src] \
                  [depot_user]/src/report_rom \
                  [depot_user]/src/vfs \
                  [depot_user]/src/lx_fs \
                  [depot_user]/src/vfs_import \
                  [depot_user]/src/init

create_tar_from_depot_binaries [run_dir]/genode/depot.tar \
                               [depot_user]/pkg/chroot \
                               [depot_user]/pkg/system_shell \
                               [depot_user]/pkg/fonts_fs \
                               [depot_user]/pkg/wm \
                               [depot_user]/pkg/nano3d \
                               [depot_user]/pkg/window_layouter \
                               [depot_user]/pkg/motif_decorator \
                               [depot_user]/pkg/themed_decorator \
                               [depot_user]/pkg/sticks_blue_backdrop

if { [get_cmd_switch --autopilot] } {
	import_from_depot [depot_user]/src/depot_remove
} else {
	build { app/depot_remove }
}

install_config {
<config>
	<parent-provides>
		<service name="ROM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
	</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>

	<start name="report_rom">
		<resource name="RAM" quantum="100M"/>
		<provides> <service name="Report"/> <service name="ROM"/> </provides>
		<config verbose="yes"/>
	</start>

	<start name="lx_fs" ld="no">
		<resource name="RAM" quantum="100M"/>
		<provides> <service name="File_system"/> </provides>
		<config>
			<policy label="depot_remove -> " root="/depot" writeable="yes"/>
		</config>
	</start>

	<start name="depot_remove">
		<resource name="RAM" quantum="2M"/>
		<route>
			<service name="ROM" label="config"> <parent label="depot_remove_config"/> </service>
			<service name="File_system"> <child name="lx_fs"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>

</config>}

exec mkdir [run_dir]/genode/depot
exec tar xvf [run_dir]/genode/depot.tar -C [run_dir]/genode/depot
exec chmod -R +r [run_dir]/genode/depot

if { [get_cmd_switch --autopilot] } {
	build_boot_image {}
} else {
	build_boot_image [build_artifacts]
}

proc install_test_config { args } {
	set fd [open [run_dir]/genode/depot_remove_config w]
	puts $fd "[join $args {}]"
	close $fd
}

proc pkg_path { name } {
	return [depot_user]/pkg/$name/[_current_depot_archive_version pkg $name] }

proc collect_pkg_dependencies { path } {

	set fd [open [run_dir]/genode/depot/$path/archives r]
	set pkg_deps [split [string trim [read $fd]]]
	close $fd

	return $pkg_deps
}

proc build_depot_context { } {
	set context [dict create]
	dict set context archives_to_delete { }
	dict set context archives_to_keep   { }

	# collect every PKGs and dependencies in the keep list
	foreach dir [glob -type d "[run_dir]/genode/depot/[depot_user]/pkg/*"] {
		dict with context {
			set path [pkg_path [file tail $dir]]
			set dependencies [collect_pkg_dependencies $path]

			foreach dependency $dependencies {
				set idx [lsearch -exact $archives_to_keep $dependency]
				if { $idx == -1 } {
					lappend archives_to_keep $dependency
				}
			}
			lappend archives_to_keep $path
		}
	}

	return $context
}

proc mark_pkg_for_deletion { context name } {

	# collect pkg versioned path and its dependencies
	set path [pkg_path $name]
	set archives [collect_pkg_dependencies $path]
	lappend archives $path

	# compute archive(s) still referenced by another PKG that must be kept
	foreach dir [glob -type d "[run_dir]/genode/depot/[depot_user]/pkg/*"] {
		if { [file tail $dir] == $name } continue

		set pkg_path [pkg_path [file tail $dir]]
		set pkg_dependencies [collect_pkg_dependencies $pkg_path]

		foreach archive $archives {
			set idx [lsearch -exact $pkg_dependencies $archive]
			# remove the archive from deletion list if listed in other PKG dependencies
			if { $idx != -1 } {
				set archive_idx [lsearch -exact $archives $archive]
				set archives [lreplace $archives $archive_idx $archive_idx]
			}
		}
	}

	# remove archives to delete from keep list and put them into deletion list
	foreach archive $archives {
		dict with context {
			set idx [lsearch -exact $archives_to_keep $archive]
			if { $idx != -1 } {
				set archives_to_keep [lreplace $archives_to_keep $idx $idx]
			}
			set idx [lsearch -exact $archives_to_delete $archive]
			if { $idx == -1 } {
					lappend archives_to_delete $archive
			}
		}
	}

	return $context
}

proc mark_pkg_to_keep { context name } {

	# collect pkg versioned path and its dependencies
	set path [pkg_path $name]
	set dependencies [collect_pkg_dependencies $path]
	lappend dependencies $path

	# ensure archives are not into deletion list but in keep list
	foreach archive $dependencies {
		dict with context {
			set idx [lsearch -exact $archives_to_delete $archive]
			if { $idx != -1 } {
				set archives_to_delete [lreplace $archives_to_delete $idx $idx]
			}
			set idx [lsearch -exact $archives_to_keep $archive]
			if { $idx == -1 } {
				lappend archives_to_keep $archive
			}
		}
	}

	return $context
}

proc mark_all_for_deletion { context } {
	dict with context {
		set archives_to_delete [concat $archives_to_delete $archives_to_keep]
		set archives_to_keep { }
	}
	return $context
}

proc check_depot_state { context } {

	foreach archive [dict get $context archives_to_delete] {

		regexp [_depot_archive_versioned_path_pattern] $archive dummy archive_user archive_type archive_name

		if { $archive_type == "src" } {
			set archive $archive_user/bin/[depot_spec]/$archive_name/[_current_depot_archive_version src $archive_name]
		}

		if { [file isdirectory [run_dir]/genode/depot/$archive] } {
			puts "ERROR: $archive is present but should has been deleted."
			return 1
		}
	}

	foreach archive [dict get $context archives_to_keep] {

		regexp [_depot_archive_versioned_path_pattern] $archive dummy archive_user archive_type archive_name

		if { $archive_type == "src" } {
			set archive $archive_user/bin/[depot_spec]/$archive_name/[_current_depot_archive_version src $archive_name]
		}

		if { ![file isdirectory [run_dir]/genode/depot/$archive] } {
			puts "ERROR: $archive should still be there but it has been deleted."
			return 1
		}
	}

	return 0
}

puts "\n--------------- TEST 1 --- Delete nano3d"

set context [mark_pkg_for_deletion [build_depot_context] "nano3d"]

install_test_config {
<config arch="} [depot_spec] {" report="yes">
	<remove user="} [depot_user] {" pkg="nano3d"/>
	<vfs> <dir name="depot"> <fs/> </dir> </vfs>
</config>}

run_genode_until ".*</removed_archives>" 10

if { [check_depot_state $context] } {
	puts " TEST 1 --- Delete nano3d -- ERROR"
	exit 1
}

puts " TEST 1 --- Delete nano3d -- SUCCESS"
puts "\n--------------- TEST 2 --- Delete non existing archive"

set context [build_depot_context]

install_test_config {
<config arch="} [depot_spec] {" report="yes">
	<remove user="} [depot_user] {" pkg="nano3d"/>
	<vfs> <dir name="depot"> <fs/> </dir> </vfs>
</config>}

run_genode_until ".*<removed_archives/>" 10

if { [check_depot_state $context] } {
	puts " TEST 2 --- Delete non existing archive -- ERROR"
	exit 1
}

puts " TEST 2 --- Delete non existing archive -- SUCCESS"

puts "\n--------------- TEST 3 --- Delete a PKG archive with deps to keep"

set context [mark_pkg_for_deletion [build_depot_context] "fonts_fs"]

install_test_config {
<config arch="} [depot_spec] {" report="yes">
	<remove user="} [depot_user] {" pkg="fonts_fs"/>
	<vfs> <dir name="depot"> <fs/> </dir> </vfs>
</config>}

run_genode_until ".*</removed_archives>" 10

if { [check_depot_state $context] } {
	puts " TEST 3 --- Delete a PKG archive with deps to keep --- ERROR"
	exit 1
}

puts " TEST 3 --- Delete a PKG archive with deps to keep --- SUCCESS"

puts "\n--------------- TEST 4 --- Remove all, keep themed_decorator and chroot PKGS"

set context [mark_all_for_deletion [build_depot_context]]
set context [mark_pkg_to_keep $context "themed_decorator"]
set context [mark_pkg_to_keep $context "chroot"]

install_test_config {
<config arch="} [depot_spec] {" report="yes">
	<remove-all>
		<keep user="} [depot_user] {" pkg="themed_decorator"/>
		<keep user="} [depot_user] {" pkg="chroot"/>
	</remove-all>
	<vfs> <dir name="depot"> <fs/> </dir> </vfs>
</config>}

run_genode_until ".*</removed_archives>" 10

if { [check_depot_state $context] } {
	puts " TEST 4 --- Remove all, keep themed_decorator and chroot PKGS --- ERROR"
	exit 1
}

puts " TEST 4 --- Remove all, keep themed_decorator and chroot PKGS --- SUCCESS"

puts "\n--------------- TEST 5 --- Remove all --- SUCCESS"

set context [mark_all_for_deletion [build_depot_context]]

install_test_config {
<config arch="} [depot_spec] {" report="yes">
	<remove-all/>
	<vfs> <dir name="depot"> <fs/> </dir> </vfs>
</config>}

run_genode_until ".*</removed_archives>" 10

if { [check_depot_state $context] } {
	puts " TEST 5 --- Remove all --- ERROR"
	exit 1
}

puts " TEST 5 --- Remove all --- SUCCESS"