#!/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.

########################################################################
# checks file permissions and ownership in the package doc dir. files
# should all be mode 644, directories should be 755. everything should
# be owned by root:root. also checks for empty files or (possibly)
# install instructions.

# ideally, we'd require all files under the doc dir to be mode 0644.
# however, too many existing packages (including core Slackware
# ones) break that rule. so check for the minimum set of desired
# permissions: a doc file should be readable by all users (at least
# 444), and check that any +x file looks like some kind of script
# (perl/shell/python/etc).

# also, any PDF files found in the doc dir are checked with pdfinfo,
# to make sure they're really PDFs and don't contain errors that
# prevent them being displayed.

DOCDIR=usr/doc/$PRGNAM-$VERSION

# existence of the doc dir was already checked by a previous test,
# so just don't do anything if it's missing.

if [ -d "$DOCDIR" ]; then
	find_warnfiles "bad file perms (should be 644, or at least 444) in doc dir:" \
		$DOCDIR -mindepth 1 -type f -a \! -perm -444
	find_warnfiles "bad directory perms (should be 755) in doc dir:" \
		$DOCDIR -mindepth 1 -type d -a \! -perm 0755
	find_warnfiles "bad ownership (should be root:root) in doc dir:" \
		$DOCDIR -mindepth 1 \! \( -user root -a -group root \)
	find_warnfiles "empty files/dirs in doc dir:" \
		$DOCDIR -mindepth 1 -maxdepth 1 -empty

	# 20230616 bkw: this handles spaces in filenames, but it will choke on
	# embedded backslashes. doubt that will be a real problem.
	find $DOCDIR -type f -perm /111 -print | while read f; do
		head -1 "$f" | grep -q '^#!' && continue;   # script file, +x is OK
		ls -bld "$f" >> .bad.$$
	done

	[ -s .bad.$$ ] && warn "bad file perms (should be 644, or at least 444) in doc dir:" && cat .bad.$$
	rm -f .bad.$$

	if [ -z "$INSTALL_DOCS_OK" ]; then
		for i in $DOCDIR/INSTALL*; do
			[ -e "$i" ] || continue
			if grep -q 'generic installation instructions' $i || \
			   grep -q 'instructions are generic' $i
			then
				warn "generic install instructions found, useless, please remove: $i"
			else
				note "install instructions found, is this relevant for binary package users?: $i"
			fi
		done
	fi

	# PDF filenames often have spaces/punctuation, don't use find.
	# This is not ideal (only goes 3 levels deep), but it's easy.
	for i in $DOCDIR/*.pdf $DOCDIR/*/*.pdf $DOCDIR/*/*/*.pdf \
	         $DOCDIR/*.PDF $DOCDIR/*/*.PDF $DOCDIR/*/*/*.PDF
	do
		if [ -e "$i" ]; then
			# pdfinfo non-fatal errors don't result in non-zero exit status,
			# but generally they do mean un-renderable PDFs, so check its stderr
			# in addition to its exit status.
			pdfinfo "$i" &> .tmp.pdfinfo
			if [ "$?" != "0" ] ||  grep -q '^Syntax Error' .tmp.pdfinfo; then
				warn "broken PDF file: $i"
			fi
			rm -f .tmp.pdfinfo
		fi
	done
fi

# allow /usr/doc/HTML to exist, though we don't check its contents here.
# this is the standard location for KDE5 help files.
find_warnfiles "docs outside of $DOCDIR:" \
	usr/doc -mindepth 1 -maxdepth 1 \! -name $PRGNAM-$VERSION \! -name HTML
