#!/bin/sh
#
# Monitor - an installation monitoring tool
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
if [ ! "$UID" = "0" ]; then
echo
echo "NOTICE: * $LOGNAME * You need to be 'root' to run this script."
echo "You could login: root"
echo "You could also try one of these: # sudo monitor"
echo "                                 # su -c monitor"
echo
exit 1
fi

logdir="/var/log/packages"
search="/"
exclude="/DOS /cdrom /initrd /mnt /root /proc /tmp /var /dev /home /usr/src /usr/spool /usr/local/src"
fstype="ext2"

if [ ! -d $logdir ]; then
mkdir -p $logdir
fi
    
# clean up $search/$exclude a bit

search_exclude_normalize()
{
    for var in search exclude
    do
	eval tmp=\"\$$var\"
	tmp=$(printf " %s " "$tmp" | tr -s " /" | sed "s/\/ / /g" | sed "s/  / \/ /g" | tr " " "\n" | sort -u | tr "\n" " ")
	eval $var=\"\$tmp\"
    done
}

# prepares the command line of find

setup_find_args()
{
    actionargs="-cnewer /tmp/instmon.$package -print"
    excludeargs=""
    if [ -n "$fstype" ]
    then
	for type in $fstype
	do
	    excludeargs="$excludeargs -not -fstype $type"
	done
	excludeargs="$excludeargs -prune -o"
    fi
    for path in $exclude
    do
	excludeargs="$excludeargs -path $path -prune -o"
    done
}

# records the current time (as the timestamp of a file)
record()
{
    printf "Recording timestamp ...\n" 1>&2
    : > /tmp/instmon.$package
    printf "done ...\n" 1>&2
}

# detects the changes made to the file system since the recorded time
compare()
{
    printf "Detecting changes ...\n" 1>&2
    rm -f /tmp/instmon.files.$$
    : > /tmp/instmon.files.$$
    for path in $search
    do
	find $path -path $path \( $actionargs , -true \) -o $excludeargs $actionargs >> /tmp/instmon.files.$$ 2> /dev/null
    done
    printf "Package Log: /var/log/packages/$package\n" 1>&2
    printf "To remove package: removepkg $package\n" 1>&2
    printf "done ...\n" 1>&2
}

# tidies /tmp and places the log in $logdir if possible
cleanup()
{
    rm -f /tmp/instmon.$package
    rm -f /tmp/$package
    sort -u /tmp/instmon.files.$$ > /tmp/$package
    rm -f /tmp/instmon.files.$$
    echo "PACKAGE NAME: $package  `date '+%a %b %e %Y' 2>/dev/null`" > /var/log/packages/$package 
    echo "./" >> $logdir/$package 
    cat /tmp/$package >> $logdir/$package 
    rm -f /tmp/$package 
}

# main program

    if [ "$1" = "" ]; then
    package=$(basename $(pwd))
    else 
    package="$1"
    fi
    
    printf "Monitoring package %s ...\n" "$package" 1>&2

if [ -e /tmp/instmon.$package ]
then
    search_exclude_normalize
    setup_find_args
    compare
    cleanup
else
    record
fi

exit 0
