#!/bin/sh -
#
#	$NetBSD: weekly,v 1.29 2012/08/29 20:34:19 wiz Exp $
#	from: @(#)weekly	8.2 (Berkeley) 1/2/94
#

export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/libexec
umask 077

if [ -s /etc/weekly.conf ]; then
	. /etc/weekly.conf
fi

host="$(hostname)"
date="$(date)"
rcvar_manpage='weekly.conf(5)'

echo "To: ${MAILTO:-root}"
echo "Subject: $host weekly output for $date"
echo ""

if [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
else
	echo "Can't read /etc/rc.subr; aborting."
	exit 1;
fi

WEEKLYDIR=$(mktemp -d -t _weekly) || exit 1

trap "/bin/rm -rf $WEEKLYDIR ; exit 0" EXIT INT QUIT PIPE

if ! cd "$WEEKLYDIR"; then
	echo "Can not cd to $WEEKLYDIR".
	exit 1
fi

TMP=weekly.$$

#echo ""
#echo "Removing old .o files:"
#find /usr/src -name '*.o' -atime +21 -print -a -exec rm -f {} \;

# see if /usr/src exists and is local
# before looking there for checked-out files

#if [ -d /usr/src -a \
#  -n "$(find -f /usr/src ! -fstype local -prune -or -type d -print -prune)" ];
#then
#	echo ""
#	echo "Looking for checked out files:"
#
#	TDIR=$(mktemp -d -t _checkout) || exit 1
#	trap "/bin/rm -rf $TDIR ; exit 0" EXIT INT QUIT
#	for file in $(find -f /usr/src ! -fstype local -prune -or \
#	    -name 'p.*' -print | egrep 'SCCS/p\.'); do
#		owner=$(awk '{ print $3 }' $file)
#		echo "$owner	$file"
#		echo $file >> "$TDIR/$owner"
#	done | sed -e 's,SCCS/p.,,'
#	if test -n "$(ls $TDIR)"; then
#		for file in $TDIR/*; do
#			sed -e 's,SCCS/p.,,' $file | \
#			    Mail -s 'checked out files' ${file##*/}
#		done
#	fi
#fi

if checkyesno rebuild_locatedb; then
	echo ""
	if [ -f /var/db/locate.database ]; then
		echo "Rebuilding locate database:"
		chmod 644 /var/db/locate.database
		chown nobody:nobody /var/db/locate.database
		(cd / && nice -5 su -m nobody -c /usr/libexec/locate.updatedb 2>/dev/null)
		chown root:wheel /var/db/locate.database
	else
		echo "Not rebuilding locate database; no /var/db/locate.database"
	fi
fi

if checkyesno rebuild_whatisdb; then
	echo ""
	if [ -x /usr/libexec/makewhatis -a \( -f /etc/man.conf \
	     -o -d /usr/share/man \) ]; then
		echo "Rebuilding whatis databases:"
		nice -n 5 /usr/libexec/makewhatis -f
	else
		echo "Not rebuilding whatis databases"
	fi
fi

if checkyesno rebuild_mandb; then
	echo ""
	if [ -f /etc/man.conf -a -x /usr/sbin/makemandb ]; then
		echo "Rebuilding man page index:"
		(umask 022; nice -n 5 /usr/sbin/makemandb -f -Q)
	else
		echo "Not rebuilding man page index"
	fi
fi

if [ -f /etc/weekly.local ]; then
	. /etc/weekly.local > $TMP 2>&1
	if [ -s $TMP ] ; then
		printf "\nRunning /etc/weekly.local:\n"
		cat $TMP
	fi
	rm -f $TMP
fi
