#!/bin/bash

# rpm-sigchecker 1.0
# written 2002 Matthew Miller <mattdm@mattdm.org>
# released to the public domain

# takes a list of rpms on stdin. exits with 0 if all packages are properly
# signed; exits with the number of unsigned packages otherwise.

GPGUSERDIR=/root


function checkrpm()
{
	if [ ! -e $1 ]; then
		echo error: ${1##*/} does not seem to exist.
		return 1
	fi

	if [ ! -r $1 ]; then
		echo error: ${1##*/} is not readable.
		return 1
	fi
	
	if [ -n "$GPGUSERDIR" ]; then
		sigcheck=`HOME=$GPGUSERDIR rpm -K $1 2>&1`
	else
		sigcheck=`rpm -K $1 2>&1`
	fi

	case ${sigcheck##*:} in 
		*gpg* )
			return 0
			;;
		*GPG* )
			echo error: ${1##*/} has invalid or unknown GPG key.
			return 1
			;;
		*"rpmReadSignature failed"* )
			echo error: ${1##*/} read signature failed. \(Corrupt file?\)
			return 1
			;;
		* )
			echo error: ${1##*/} was not GPG signed.
			return 1
			;;
	esac
}


unsignedcount=0
while read p; do
	if [ -n "$p" ] ; then
		checkrpm $p
		unsignedcount=$(( $unsignedcount + $? ))
	fi
done

exit $unsignedcount

