#!/bin/sh -ue
echo "* ${0##*/}: Started"
echo "This test is to ensure that the 'Enhances' field includes all relevent packages"
echo "** env"
env
status=PASS

echo "** installing dependencies"
if [ ! -x /usr/bin/command-not-found ]; then
		# also ensure mawk and sed are installed
		apt-get update
		apt-get install --yes mawk sed command-not-found
fi

echo "** creating binaries so that many tests run"

MADE=""
for binary in \
		amd biff cron crontab fingerd in.fingerd gpm hdparm \
		inetd in.identd inetdconf init killall lsdopreload \
		lsof mail mingetty named in.pop2d in.pop3d \
		pstree rpcinfo rlogind in.rshd slogin sendmail sshd syslogd \
		tcpd tcpdump telnetd timed traceroute \
		ifconfig ps ip strings in.rexedcs; do
		for path in /bin/$binary /sbin/$binary /etc/$binary.conf; do
				if [ ! -e "$path" ]; then
						echo "Making $path"
						cat > "$path" <<EOF
#!/bin/sh
echo "Ensuring $binary exists for chkrootkit test"
exit 0
EOF
						chmod +x "$path"
						MADE="$MADE $path"
				else
						echo "$path: exists"
				fi
		done
done
echo "Done"

echo "** updating command-not-found cache"
echo "*** apt-file update"
apt-file update
echo "*** update-command-not-found"
update-command-not-found
echo "*** check it works"
if command-not-found chromium 2>&1 |tee |grep 'apt install chromium'; then
		echo "Seems OK"
else
		echo >&2 "command-not-found not working?"
fi
echo "** Running chkrootkit to generate a list of tests"
chkrootkit > /tmp/log 2>&1  || ls -l /usr/sbin/chkrootkit
awk '/`[[:alnum:]]+'\'\.\.\.'+/{print $2}' /tmp/log

enhances=" $(awk '/^Enhances:/{shown=1;next}
                 /^Description:/{shown=0}
                  {if (shown==1) {
                    gsub(","," ")
                    printf("%s ",$0) # \n -> space
                  }}' ./debian/control) "


echo "* Starting tests of 'Enhances' field"
echo "Enhances: <$enhances>"
for test in $(awk '/`[[:alnum:]]+'\'\.\.\.'+/{print $2}' /tmp/log | sed 's/[`'\''.]//g'); do
		pkgs="$(command-not-found --ignore-installed "$test" 2>&1 \
														 | command grep 'apt install' \
														 | command grep -v '<deb name>' \
														 | awk '{print $3 }')"
		case "$test" in
				aliens|asp|bindshell|chkutmp|inetdconf|lkm|ldsopreload|rexedcs|scalper|slapper|sniffer|w55808|wted|z2)
				## these tests that do not look for binaries of the same
				## name as the tets, and so do not enhance a debian
				## package
				;;
				fingerd|mail|traceroute|rlogind|rshd|write)
				## enhances added by hand as command-not-found does not locate the relevent package
				;;
				amd|identd|pop2|pop3|rexedcs|timed)
				## these binaries dont seem to be packaged in debian
				;;
				*)
						## other tests look at binaries, so they enhance any
						## debian package providing that binary
						if [ -z "$pkgs" ]; then
								echo "** FAIL: unexpectedly failed to find any packages providing $test. Update test and README.source"
								status=FAIL
						else
								for deb in $pkgs; do
										case "$enhances" in
												*" $deb "*)
														echo "** PASS: $test would run if $deb is installed and the package has Enhances: $deb"
														;;
												*)
														echo "** FAIL: package should say Enhances: $deb (for test $test)"
														cat ./debian/control || ls -l ./debian
														status="FAIL"
														;;
										esac
								done
						fi
		esac
done
echo "* ${0##*/}: DONE: $status"
[ -n "$MADE" ] && rm -f $MADE
if [ "$status" = "FAIL" ]; then
		echo >&2 "FAIL"
		exit 1
else
		echo "PASS"
		exit 0
fi
