#!/bin/sh -
#
#	@(#)daily	8.2 (Berkeley) 1/25/94
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
host=`hostname -s`
bak=/var/backups

echo ""
echo "Removing scratch and junk files:"

if [ -d /var/rwho ] ; then
    cd /var/rwho && {
    find . ! -name . -mtime +7 -exec rm -f -- {} \; ; }
fi

# Clean up junk files.
#find / \( ! -fstype local -o -fstype rdonly \) -a -prune -o \
#       \( -name '[#,]*' -o -name '.#*' -o -name a.out -o -name '*.core' \
#	  -o -name '*.CKP' -o -name '.emacs_[0-9]*' \) \
#	       -a -atime +3 -exec rm -f -- {} \;

# Clean up NFS turds.  May be useful on NFS servers.
#if [ "${host}" != "localhost" ]; then
#    find / -name .nfs\* -mtime +7 -exec rm -f -- {} \; -o -fstype nfs -prune
#fi

if [ -d /tmp ]; then
    cd /tmp && {
    find . -fstype local -type f -atime +3 -ctime +3 -exec rm -f -- {} \;
    find -d . -fstype local ! -name . -type d -mtime +1 -exec rmdir -- {} \; \
	>/dev/null 2>&1; }
fi

# remove system messages older than 21 days
msgs -c

if [ -f /var/account/acct ] ; then
    if [ -x /usr/bin/gzip ]; then gzext=".gz"; else gzext=""; fi
    echo ""
    echo "Gathering accounting statistics:"
    cd /var/account
    if [ -f "acct.2${gzext}" ] ; then mv -f "acct.2${gzext}" "acct.3${gzext}" ; fi
    if [ -f "acct.1${gzext}" ] ; then mv -f "acct.1${gzext}" "acct.2${gzext}" ; fi
    if [ -f "acct.0${gzext}" ] ; then mv -f "acct.0${gzext}" "acct.1${gzext}" ; fi
    cp -pf acct acct.0
    if [ -x /usr/bin/gzip ]; then gzip -9 acct.0; fi
    sa -s > /dev/null
fi

if [ -d /var/db/netinfo ]; then
    echo ""
    echo "Backing up NetInfo data"
    cd /var/db/netinfo
    for domain in *.nidb; do
	domain=$(basename $domain .nidb)
	server=`nicl -t localhost/$domain -statistics | grep tag | awk '{print $3}'`
	if [ $server = master ] ; then nidump -r / -t localhost/$domain > $bak/$domain.nidump; fi
    done
fi

echo ""
echo "Checking subsystem status:"

echo ""
echo "disks:"
df -k -l
echo ""
dump W

if [ -d /var/spool/mqueue ]; then
	echo ""
	echo "mail:"
	mailq
fi

echo ""
echo "network:"
netstat -i

echo ""
if [ -d /var/rwho ] ; then
	ruptime
fi

echo ""
echo -n "Rotating log files:"
cd /var/log
for i in system.log; do
    if [ -f "${i}" ]; then
	echo -n " ${i}"
	if [ -x /usr/bin/gzip ]; then gzext=".gz"; else gzext=""; fi
	if [ -f "${i}.6${gzext}" ]; then mv -f "${i}.6${gzext}" "${i}.7${gzext}"; fi
	if [ -f "${i}.5${gzext}" ]; then mv -f "${i}.5${gzext}" "${i}.6${gzext}"; fi
	if [ -f "${i}.4${gzext}" ]; then mv -f "${i}.4${gzext}" "${i}.5${gzext}"; fi
	if [ -f "${i}.3${gzext}" ]; then mv -f "${i}.3${gzext}" "${i}.4${gzext}"; fi
	if [ -f "${i}.2${gzext}" ]; then mv -f "${i}.2${gzext}" "${i}.3${gzext}"; fi
	if [ -f "${i}.1${gzext}" ]; then mv -f "${i}.1${gzext}" "${i}.2${gzext}"; fi
	if [ -f "${i}.0${gzext}" ]; then mv -f "${i}.0${gzext}" "${i}.1${gzext}"; fi
        if [ -f "${i}" ]; then
              touch "${i}.$$" && chmod 640 "${i}.$$" && chown root:admin "${i}.$$"
              mv -f "${i}" "${i}.0" && mv "${i}.$$" "${i}" && if [ -x /usr/bin/gzip ]; then 
		gzip -9 "${i}.0"; fi
        fi
    fi
done
if [ -f /var/run/syslog.pid ]; then kill -HUP $(cat /var/run/syslog.pid | head -1); fi
echo ""

if [ -d /var/log/httpd ]; then
    echo ""
    echo -n "Cleaning web server log files:"
    cd /var/log/httpd && \
	find . -type f -name '*_log*' -mtime +7 -exec rm -f -- {} \; >/dev/null 2>&1;
fi

if [ -f /etc/daily.local ]; then
    echo ""
    echo "Running daily.local:"
    sh /etc/daily.local
fi

if [ -f /etc/security ]; then
    echo ""
    echo "Running security:"
    sh /etc/security 2>&1 | sendmail root
fi
