#!/bin/sh
#
# debupdnot (C) 2002 Erik Greenwald <erik@smluc.org> <erik@math.smsu.edu>
#
# a script to notify the administrator when a debian (http://www.debian.org)
# machine has new or updated packages available for install.
#
# http://math.smsu.edu/~erik/software.php?id=
#
# This software is released under the "modified BSD" license.
#
# Configuration info follows the license
#
################### LICENSE #######################
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#    1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#    2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#    3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# 
########################################################
#################### CONFIGURATION  ####################
########################################################


		# these are the files to use. These defaults should be sane.
LOCKFILE=/tmp/.smartupgrade.lock
TMPFILE=/tmp/.smartupgrade
AVAIL=/tmp/.available
AVAIL_OLD="/tmp/.available-old"

SHOULD_SEND=0	# set this to 1 if you want to always receive notification,
		# even if no packages have been added or updated. 
RECIPIENT=root	# this is the email address of the administrator. If your
		# mail configuration is sane, 'root' should be sufficient.

	# set these to 0 if you don't want them included in the report
WANT_HOSTNAME=1
WANT_UNAME=1
WANT_DATE=1
WANT_UPTIME=1

VERBOSE=0

########################################################
################## CONFIGURATION ENDS ##################
########################################################

VERSION="1.1"

# parse the parameters.

for a in $*
do
	case ${a} in
	"-v" | "--verbose")
		VERBOSE=1
		;;
	"-h" | "--help")
cat << EOF
debupdnot $VERSION (C) 2002 Erik Greenwald <erik@smluc.org>
Usage:
	$0 [-hvV] [--help] [--version] [--verbose]

	-h, --help		Print this help information and exit.
	-V, --version		Print program version and exit.
	-v, --verbose		Verbose reporting. (Disables mail)
	-m, --mail		Force email to send
	-n, --nomail		Force email to only send on new/updates
	-r, --remove		Remove the temp files. (including lock)

Report bugs to erik@smluc.org	
EOF
		exit
		;;
	"-V" | "--version")
cat << EOF
debupdnot $VERSION (C) 2002 Erik Greenwald <erik@smluc.org>
EOF
		exit
		;;
	"-m" | "--mail")
		SHOULD_SEND=1
		;;
	"-n" | "--nomail")
		SHOULD_SEND=0
		;;
	"-r" | "--remove")
		rm -f $TMPFILE $LOCKFILE $AVAIL $AVAILOLD
		exit
		;;
	*)
		echo "Unrecognized parm '$a', try --help"
		exit
		;;
esac
done


# check to see if we're locked

if [ $VERBOSE = 1 ]
then 
	echo "Running verbose, no e-mail will be sent!"
	echo "Checking the lockfile"
fi

if [ -e $LOCKFILE ]
then
	if [ $VERBOSE = 1 ]
	then 
		echo "debupdnot failed: locked" 
	else
		echo "debupdnot failed: locked" | mail -s 'Automated update failed' $RECIPIENT
	fi
	exit
fi

# a little locking to make sure we don't have the script trip over itself
# if the script fails for some reason, the administrator will start getting
# lock errors, and should use the -r or --remove parm. Debugging and a
# problem report sent to erik@smluc.org or erik@math.smsu.edu would be nice :)
touch $LOCKFILE
chmod 200 $LOCKFILE

touch $TMPFILE && chmod 600 $TMPFILE

# a little bit of information about this machine.
if [ $WANT_HOSTNAME == 1 ];then (echo -n "Hostname: "; hostname) > $TMPFILE ; fi
if [ $WANT_UNAME == 1 ];then (echo -n "Uname -a: "; uname -a) >> $TMPFILE ; fi
if [ $WANT_DATE == 1 ];then (echo -n "Date:     "; date)     >> $TMPFILE ; fi
if [ $WANT_UPTIME == 1 ];then (echo -n "Uptime:   "; uptime)   >> $TMPFILE ; fi

# update the dpkg database
if [ $VERBOSE = 1 ]
then
	apt-get -q update || exit
else
	apt-get -q update 2>&1 > /dev/null || exit
fi

# see what files can be upgraded, and add the upgrade notification line
if [ $VERBOSE = 1 ] ; then echo "Checking for updated packages" ; fi
UPDATES=`apt-get -qsu dselect-upgrade | grep '^Inst' | cut -d \  -f 2 | sort`
if [ ! "$UPDATES" == "" ]
then
	SHOULD_SEND=1
	echo -en "\nUpdates: " >> $TMPFILE
	echo $UPDATES >> $TMPFILE
fi

# see what packages are new and if the new package line should be added
if [ $VERBOSE = 1 ] ; then echo "Checking for new packages" ; fi
grep '^Package: ' /var/lib/dpkg/available-old |sort|uniq> $AVAIL_OLD
grep '^Package: ' /var/lib/dpkg/available |sort|uniq> $AVAIL

NEWPKG=`diff -u $AVAIL_OLD $AVAIL | grep '^+Package: ' | cut -d \  -f 2`
if [ ! "$NEWPKG" == "" ]
then
	SHOULD_SEND=1
	echo -ne '\nNew packages: ' >> $TMPFILE
	echo  >> $TMPFILE
fi	

if [ $VERBOSE = 1 ]
then
	cat $TMPFILE
	echo "Cleaning up"
else
# send the mail
	if [ $SHOULD_SEND == 1 ]
	then
		cat $TMPFILE | mail -s 'Automated upgrade notice' $RECIPIENT
	fi
fi

rm $TMPFILE $LOCKFILE $AVAIL $AVAILOLD

################## END SCRIPT ######################3
