#!/bin/sh

# sbopkglint test, must be sourced by sbopkglint (not run standalone).

# PKG, PRGNAM, VERSION, ARCH are set by sbopkglint. also the current
# directory is the root of the installed package tree.

#######################################################################
# check contents of /usr/share/terminfo, if present.
#
# 0. /usr/share/terminfo may only contain directories (not files). This
#    gets checked in 05-basic_sanity, not here.
# 1. directories under /usr/share/terminfo must have one-character names,
#    /a-zA-Z0-9/.
# 2. directories under /usr/share/terminfo mode 0755.
# 3. /usr/share/terminfo/*/ must contain only files (no subdirs).
# 4. files must go in the subdir for the first letter of their name.
# 5. files must be valid compiled terminfo entries.
# 6. files must be mode 0644 or 0444.
# 7. all files and dirs must be owned by root:root.
#
# Note: although I've never seen it done, it's perfectly legit for a
# terminfo file to have a space in its name. So that's not checked
# for here.

if [ -d "usr/share/terminfo" ]; then
	find -L "usr/share/terminfo" -type f > .tfiles.$$
	# 1:
	find -L "usr/share/terminfo" -type d -mindepth 1 -name '??*' > .baddirs.$$
	# 3:
	find -L "usr/share/terminfo" -type d -mindepth 2 >> .baddirs.$$
	# 2:
	find -L "usr/share/terminfo" -type d -a \! -perm 755 > .badperms.$$
	# 6:
	find -L "usr/share/terminfo" -type f -a \! \( -perm 644 -o -perm 444 \) >> .badperms.$$
	# 7:
	find -L "usr/share/terminfo" \! -user root -o \! -group root >> .badperms.$$
	# 5:
	xargs -d "\n" file -L --mime-type < .tfiles.$$ | grep -v 'application/x-terminfo' > .badmime.$$
	# 4:
	egrep -v 'usr/share/terminfo/([a-zA-Z0-9])/\1[^/]*$' .tfiles.$$ > .wrongdirs.$$

	if [ -s .badperms.$$ ]; then
		warn "bad terminfo ownership (must be root:root) and/or permissions (755 for dirs, 644 or 444 for files):"
		sort -u .badperms.$$ | xargs -d "\n" ls -ld
	fi

	if [ -s .baddirs.$$ ]; then
		warn "invalid terminfo directory structure:"
		sort -u .baddirs.$$ | xargs -d "\n" ls -ld
	fi

	if [ -s .wrongdirs.$$ ]; then
		warn "terminfo entries in wrong dir(s):"
		xargs -d "\n" ls -l < .wrongdirs.$$
	fi

	if [ -s .badmime.$$ ]; then
		warn "/usr/share/terminfo has invalid terminfo file(s):"
		cat .badmime.$$
	fi

	rm -f .badperms.$$ .baddirs.$$ .tfiles.$$ .wrongdirs.$$ .badmime.$$
fi
