#!/usr/local/bin/bash
#
# (c) 2006 Witold Rugowski
# http://nhw.pl/
#
# 
# Simple script to monitor changes in installed ports tree
#
# Stores current version in file $DATA, makes diff agains curent content
# of /var/db/pkg directory. Any diffs are send to chosen email address
#
# CUSTOMIZE:
#
# DATA and TMP locations. Remeber about possibility of race - dont run this
# as root or choose safe (not writable by others) TMP
#
# TO - email address which will receive raports
#
# OUTPUT:
#  port_1 --> port_2	port_1 has changed into port_2
#  --> port_1 		port_1 has been instaled
#  port_1 		port_1 has been removed
#

DATA=/root/ports.lst
TMP=/root/ports.lst.$$
TO=example@example.com

if [ ! -f $DATA ]; then
	ls /var/db/pkg > $DATA
	exit 0
fi

ls /var/db/pkg/ > $TMP

diff $DATA $TMP >& /dev/null

if [ $? -eq 1 ]; then
	HOST=`hostname`
	diff --old-line-format='%l' \
		--new-line-format=' --> %L' \
		--unchanged-line-format='' \
	$DATA $TMP | mail -s "[$HOST] Ports DB change!" $TO
	cat $TMP > $DATA
fi
	rm -f $TMP

