#!/bin/bash

if [[ $# -ne 1 ]];
then
  echo "Usage: $0 -r";
  exit 1;
fi

CONFFILE="";
if [ -f ~/.cvechecker.rc ];
then
  CONFFILE=~/.cvechecker.rc;
elif [ -f /usr/local/etc/cvechecker.conf ];
then
  CONFFILE=/usr/local/etc/cvechecker.conf;
elif [ -f /etc/cvechecker.conf ];
then
  CONFFILE=/etc/cvechecker.conf;
fi


if [ -f /etc/gentoo-release ];
then
	cd /var/db/pkg;
	for CAT in *;
	do
		cd ${CAT};
		for PKG in *;
		do
			echo "!!!! Checking ${PKG}";
			typeset PKGNAME=$(echo ${PKG} | sed -e 's:\(.*\)-[0-9][0-9]*.*:\1:g');
			typeset PKGVERSION=$(echo ${PKG} | sed -e 's:.*-\([0-9][0-9]*.*\):\1:g' | sed -e 's:-r[0-9]*$::g' | sed -e 's:\.:\\.:g');
			export IGNORESTRINGS="tmp/portage";
			qlist ${PKG} | egrep '(bin/|\.so)' | cvegenversdat ${PKGNAME} ${PKGVERSION} ${CONFFILE} breakon;
		done
		cd ..
	done
else
	echo "Could not deduce if this system is a supported distribution or platform.";
	echo "Currently supported platforms are: Gentoo."
	echo "If you wish to help expand the supported distributions or platforms, please";
	echo "do not hesitate to contact the author at sven.vermeulen@siphos.be.";
	exit 1;
fi
