# Check whether ebuilds are not installing new, non-Gentoo-ey paths.
# This specific version overrides Gentoo's upstream QA check, so it won't be
# triggered by /usr/tqt and /usr/trinity. Also it does check the paths themself
# for conformity.

# QA check: validate Gentoo's filesystem layout policies
# Maintainer: Trinity Gentoo ebuilds team <team-gentoo@trinitydesktop.org>

gentoo_path_check_prefix() {
	# A helper function to check the given path against list of allowed paths.
	# Usage: gentoo_check_prefix <path> <allowed1> [allowed2] ...
	# The files in the path that haven't passed the check will be appended to
	# "bad_paths" array
	# ------------------------

	local path="$1"; shift
	local allowed_paths=( "$@" )
	# local -n bad_paths_ref="bad_paths"

	local x
	local check_dirs=( "${ED%/}/${path#/}"/* )
	for x in "${check_dirs[@]##*/}"; do
		if ! has "${x}" "${allowed_paths[@]}"; then
			bad_paths+=( "/${x}" )
		fi
	done
}

gentoo_path_check() {
	# allowed path definitions
	# ------------------------

	# directories common to /, /usr and other prefixes
	local allowed_common_dirs=(
		bin lib lib32 lib64 libx32 sbin
	)

	# toplevel directories which can be installed to by ebuilds
	# /home is not included as no ebuilds should install files there
	local allowed_paths_toplevel=(
		"${allowed_common_dirs[@]}"
		boot dev etc opt srv usr var
	)

	# directories in /usr which can be installed to by ebuilds
	# /usr/games is not included as it is banned nowadays
	local allowed_paths_usr=(
		"${allowed_common_dirs[@]}"
		include libexec share src
		# TDE-specific
		trinity tqt3
		# toolchain stuff
		"${CHOST}" "${CTARGET}"
	)

	# [TDE] directories in /usr/tqt3
	local allowed_paths_tqt=(
		"${allowed_common_dirs[@]}"
		etc include mkspecs phrasebooks plugins
		share sip templates translations
	)

	# [TDE] directories in /usr/trinity/14
	local allowed_paths_trinity=(
		"${allowed_common_dirs[@]}"
		env etc include share shutdown
	)

	# the logic
	# ---------
	local bad_paths=()

	local shopt_save=$(shopt -p nullglob)
	shopt -s nullglob

	# 1. check for unexpected top-level directories
	gentoo_path_check_prefix "/" "${allowed_paths_toplevel[@]}"

	# 2. check for unexpected /usr subdirectories
	gentoo_path_check_prefix "/usr" "${allowed_paths_usr[@]}"

	# 3. check for unexpected subdirectories in bin and sbin
	local bin_subdirs=( "${ED%/}"{,/usr}/{bin,sbin}/*/ )
	bad_paths+=( "${bin_subdirs[@]%/}" )

	# 4. check for unexpected /usr/share/doc subdirectories
	gentoo_path_check_prefix "/usr/share/doc" "${PF}"

	# 5. [TDE] check for directories in /usr/tqt3
	gentoo_path_check_prefix "/usr/tqt3" "${allowed_paths_tqt[@]}"

	# 6. [TDE] check for directories in /usr/trinity
	#    (as for now only a single subdirectory "14" is allowed)
	gentoo_path_check_prefix "/usr/trinity" "14"

	# 7. [TDE] check for directories in /usr/trinity/14
	gentoo_path_check_prefix "/usr/trinity/14" "${allowed_paths_trinity[@]}"

	${shopt_save}

	# report
	# ------
	if [[ -n ${bad_paths[@]} ]]; then
		eqawarn "QA Notice: The ebuild is installing to one or more unexpected directories:"
		eqawarn
		eqatag -v non-gentoo-paths "${bad_paths[@]#${D%/}}"
		eqawarn
		eqawarn "Please fix the ebuild to use correct FHS/Gentoo policy paths (with TDE extentions)."
	fi
}

gentoo_path_check
: # guarantee successful exit

# vim:ft=sh
