#!/bin/bash
# DocumentId: $Id: cron-apt,v 1.14 2002/04/24 20:15:24 ola Exp $
# Changes:
#	2002-04-07 Ola Lundqvist <opal@debian.org>
#		Added skipping of comment lines in action files.
#		Added exra config.d directory.
#		Rewrote the mail and log part to make it more flexible.
#		Christian Perrier <Christian.Perrier@onera.fr> was the
#		one that needed this flexibility.
#		Also added skipping of backup files (*~).
#	2002-03-05 Ola Lundqvist <opal@debian.org>
#		Made it complete so it can be released.
#	2002-03-03 Ola Lundqvist <opal@debian.org>
#		Wrote the beginning.
#	2002-04-24 Ola Lundqvist <opal@debian.org>
#		Added a default path. Thanks to Donovan Baarda
#		<abo@minkirri.apana.org.au> for pointing it out.

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

############################# arguments #######################################

if [ "$1" = "--help" ] ; then
    echo "USAGE: cron-apt [configfile]"
    exit 0
fi
CONFIG=$1
if [ -z "$CONFIG" ] ; then
    CONFIG=/etc/cron-apt/config
fi

############################## defaults #######################################
ACTIONDIR=/etc/cron-apt/action.d
ACTIONCONFDIR=/etc/cron-apt/config.d
ERROR=/var/log/cron-apt/error
TEMP=/var/log/cron-apt/temp
MAIL=/var/log/cron-apt/mail
LOG=/var/log/cron-apt/log
MAILTO=root
# error, always
MAILON=error
# error, never
EXITON=error
# verbose, error
DEBUG=verbose
# general options
OPTIONS=-q

# Read configuration.
if [ -f $CONFIG ] ; then
    . $CONFIG
else
    echo "The config file $CONFIG does not exist, using defaults."
fi

############################## functions ######################################

# Send the mail to the system administrator. Will only be sent if there is
# something to send. No arguments needed.
mailit() {
    if [ -f $MAIL ] ; then
	SUBJECT="CRON-APT completed on $(uname -n)"
	if [ -f $ERROR ] ; then
	    SUBJECT="CRON-APT error on $(uname -n)"
	fi
	mail -s "$SUBJECT" "$MAILTO" < $MAIL
	rm -f $MAIL
    fi
}

# Create file with RUN information. Line data should be added later.
# Arguments:
# 1: File to create
# 2: Action command (filename) (i.e $ACTIONF)
createinfo() {
    FILE="$1"
    ACTT="CRON-APT ACTION: $ACTIONF"
    ACT="$ACTT, $(date)"
    RUNT="CRON-APT RUN:"
    RUN="$RUNT $(date)"
    touch $FILE
    if ! grep "$RUNT" $FILE > /dev/null 2>&1 ; then
	echo "$RUN" >> $FILE
    fi
    if ! grep "$ACTT" $FILE > /dev/null 2>&1 ; then
	echo "$ACT" >> $FILE
    fi
}

############################### check #########################################

if [ -e $ERROR ] ; then
    echo "Error in last update. Fix the problem and remove $ERROR."
    exit
fi

############################### script ########################################
# Always run mailit before exit.

for ACTIONF in $(ls $ACTIONDIR) ; do
    if echo "$ACTIONF" | grep "~" ; then
	# Temporary file, skipping.
	:
    elif [ -f "$ACTIONDIR/$ACTIONF" ] ; then
	if [ -f "$ACTIONCONFDIR/$ACTIONF" ] ; then
	    . "$ACTIONCONFDIR/$ACTIONF"
	fi
	STATUS=$(cat "$ACTIONDIR/$ACTIONF" | \
	    sed -e "s/#.*$//;" | \
	    grep -v "^[[:space:]]*$" | {
		while read -e LINE ; do
		    echo "CRON-APT LINE: $LINE, $(date)" > $TEMP
		    if ! apt-get $OPTIONS $LINE >> $TEMP 2>&1 ; then
			# An error has occured.
			createinfo $ERROR $ACTIONF
			cat $TEMP >> $ERROR
			if [ "$MAILON" = "error" -o "$MAILON" = "always" ] ; then
			    createinfo $MAIL $ACTIONF
			    cat $TEMP >> $MAIL
			fi
			if [ "$DEBUG" = "error" -o "$DEBUG" = "verbose" ] ; then
			    createinfo $LOG $ACTIONF
			    cat $TEMP >> $LOG
			fi
			if [ "$EXITON" = "error" ] ; then
			    echo exit
			    exit
			fi
		    else
			# No error has occured.
			if [ "$MAILON" = "always" ] ; then
			    createinfo $MAIL $ACTIONF
			    cat $TEMP >> $MAIL
			fi
			if [ "$DEBUG" = "verbose" ] ; then
			    createinfo $LOG $ACTIONF
			    cat $TEMP >> $LOG
			fi
		    fi
		done
		exit
		})
	if echo $STATUS | grep exit > /dev/null 2>&1 ; then
	    mailit
	    exit
	fi
    fi
done

mailit
