#!/bin/bash
# $Header: /cvs/WebShield/wsrc/files/mgmt/Attic/prune_extra_rules,v 1.2 2004/02/02 14:19:14 bwhittak Exp $
#
# (C) Copyright Networks Associates Techonolgy Inc. 2003
#
# Called by the UI to add an extra.rule file obtained from user.
#
if [ -f $NETAWSS/.profile.vars ]; then
    . $NETAWSS/.profile.vars
else
    . /var/NAIENV/.profile.vars
fi
[[ "$PATH" == "$wsPATH":* || -z "$wsPATH" ]] || PATH=$wsPATH:$PATH

shopt -s extglob
shopt -s nullglob
. spam-functions

[[ $# -gt 0 ]] || exit 1
newdir=$1	# directory we're getting the new RPMs from
shift
rpms=( "$newdir"/*.rpm )
[[ ${#rpms[@]} -gt 0 ]] || exit 0	# nothing to do

olist=$(shopt -u nullglob; ls -d /etc/mail/spamassassin/*.cf 2>/dev/null)
for f in "${rpms[@]}"; do
    # good name?
    [[ $f == *.rpm && $f != *[$IFS\<\>\'\"]* && $(ls $f) == "$f" ]] ||
	{ rm -rf "$f"; continue; }
    # basic check for well formed RPM
    out=$(rpm -K --nogpg --nogpg "$f" 2>&1) || { rm -rf "$f"; continue; }
    # check it has only legal files
    list=$(rpm -qlp "$f" 2>&1)
    out=$(echo "$list" | grep -v '/etc/mail/spamassassin/[^/]*\.cf' 2>&1)
    [[ -z "$out" ]] || { rm -rf "$f"; continue; }
    # make sure none of them are installed already
    out=$( echo "$olist" | fgrep -x "$list" )
    [[ -z "$out" ]] || { rm -rf "$f"; continue; }
    olist=$olist$'\n'$list	# add to list of files installed
    # check it's got no scripts
    out=$(rpm -q --triggers --scripts -p "$f" 2>&1)
    [[ -z "$out" ]] || { rm -rf "$f"; continue; }
done
exit 0
