#!/bin/sh
#
# $Id: GetApplyPatch.cron,v 1.9 2001/02/14 14:29:29 reggers Exp $
#
# This script is designed to determine outstanding patches (using CheckPatches)
# and apply all outstanding patches (using GetApplyPatch). The idea is to
# do this regularly from cron and mail the results. An example cron entry
# might be
#
# 1)   # Check patch level and fix at 3:22am the 6th and 21th of each month:
#      3 22 6,21 * * /usr/local/lib/GetApplyPatch.cron
#
# If you want to mail to someone other than the user root try this:
#
# 2)   # mail results to operator not "root"
#      3 22 6,21 * * ADMIN=operator /usr/local/lib/GetApplyPatch.cron
#
# If you have to contend with a local ftp proxy try this:
#
# 3)   # pass additional arguments to "CheckPatches" and "GetApplyPatch"
#      3 22 6,21 * * /usr/local/lib/GetApplyPatch.cron -u ftp@sunsolve.sun.com -s proxy.my.com
#
# If your ROOT isn't /usr/local, if you'd rather not work in /var/adm then:
#
# 4)   # Different location for package. Some like /opt
#      3 22 6,21 * * ROOT=/opt/patches VAR=/var/patches /usr/local/lib/GetApplyPatch.cron
#
# v1.6; Sean Boran suggestion -- if input is a tty then don't push to
#	a file and mail. Just run to the tty.
#
# BEWARE: I DO NOT RECOMMEND YOU DO THIS ON SENSITIVE BACKROOM SERVERS!!
#	YOU MIGHT DO THIS ON A TEST/DEVELOPMENT MACHINE FIRST. APPLYING
#	PATCHES WILLY NILLY ON PRODUCTION SERVERS *CAN* GET YOU INTO REAL
#	TROUBLE. SOME PATCHES REQUIRE REBOOTS, SHOULD BE APPLIED IN SINGLE
#	USER MODE, OR ETC. BE CAREFUL!!
#
#	AND DON'T SAY YOU HAVEN'T BEEN WARNED.
#
# BEWARE: THIS SCRIPT LEAVES FILES IN /var/adm!! (Cf. CheckPatches cron)
#
# Solaris patch reports are published around the 1st and the 15th of
# the month. It's probably prudent to wait at little before grabbing
# anything. You may be more anxious, I'm not. REQ.
#

ARGS="$*"				# if you use a proxy, see 3)
ADMIN=${ADMIN:-root}			# honor environment, see 2)
VAR=${VAR:-/var/adm}			# honor environment, see 4)
ROOT=${ROOT:-/usr/local}		# honor environment, see 4)
ETC=${ETC:-${ROOT}/etc}			# honor environment, see 4)

TMP=/tmp/PatchReport.$$

# Search path includes CheckPatches & GetApplyPatch tools found in $ETC

PATH=/usr/bin:/usr/sbin:$ETC; export PATH
RELEASE=`uname -srpn`;
SUBJECT="Patches Applied for $RELEASE";

# change to working directory (where we'll keep the patch report)

cd $VAR

# Call with '-f' to ensure new report is fetched.

if tty -s >/dev/null 2>&1;  then
	CheckPatches -f $ARGS | sort -u | GetApplyPatch $ARGS
else
	CheckPatches -f $ARGS | sort -u | GetApplyPatch $ARGS >$TMP 2>&1

	if [ -s $TMP ]; then
		mailx -s "$SUBJECT" $ADMIN < $TMP
	fi

	rm $TMP
fi
exit 0;

