#!/bin/bash
typeset -a package_set

scan_profile() {
	local d parent
	d="$(realpath "$1")"
	[[ -r "$d/parent" ]] && while read parent; do
		case "$parent" in
			('#'*);; # ignore comments
			(/*) scan_profile "$ROOT$parent";;
			(*)  scan_profile "$d/$parent";;
		esac
	done < "$d/parent"
	[[ -r "$d/package.set" ]] && package_set+=( "$d/package.set" )
}

scan_profile "$ROOT/etc/portage/make.profile"

if [[ -n "$package_set" ]]; then
	[[ -d "$ROOT/etc/portage/sets" ]] || mkdir "$ROOT/etc/portage/sets"
	sort -u "${package_set[@]}" >"$ROOT/etc/portage/sets/profile"
fi