#!/bin/sh

# backup.sh - Script for automatic backup to disk of all source files
#
# arg1 is the source directory.
# arg2 is the destination directory.
# arg3 is the user to be mailed notification (optional).

# Normally this script is used by another script, which is executed by
# cron.  See /p7/dj/devel/src/tools/backup-dj.sh for an example.

# fad Wed Sep 21 1988 created.
# fad Tue Jun 26 1990 adapted for WAIS project.
# $Header: /tmp_mnt/am/public/Wais/RCS/backup.sh,v 1.4 90/07/05 10:53:14 fad Exp $

if test $# -lt 2
  then
    echo "usage: `basename $0` <sourcedir> <destdir> [<username>]"
    exit 1
  fi

SOURCEDIR=$1
DESTDIR=$2
if test $# = 3
  then
    USERS=$3
  fi

# Find out if -p option exists for cp, use it if possible (not in ULTRIX!)
touch /tmp/testcp$$
cp -p /tmp/testcp$$ /tmp/testcp1$$ 2>/dev/null
if test $? = 0
  then
    CP="cp -p"
  else
    CP=cp
  fi
rm -f /tmp/testcp*

cd $SOURCEDIR

# First, be sure directory structure is in place
if test ! -d $DESTDIR
  then
    echo Creating new backup directory $DESTDIR...
    mkdir $DESTDIR
  fi
echo Backup of $SOURCEDIR to $DESTDIR started `date` | \
		tee -a $DESTDIR/backup-log

for i in `find . -type d -print | sort`
  do
    if test ! -d $DESTDIR/$i
      then
        echo Creating backup directory $DESTDIR/$i... | \
		tee -a $DESTDIR/backup-log
        mkdir $DESTDIR/$i
      fi
  done

# Find out if a previous backup-date file exists
if test ! -f $DESTDIR/backup-date
  then
    echo $DESTDIR/backup-date not found, backing up all files in $SOURCEDIR!\
		 | tee -a $DESTDIR/backup-log
    NEWER_SPEC=
  else
    NEWER_SPEC="-newer $DESTDIR/backup-date"
  fi


# Do the copying.  Add filespecs here for filetypes to backup.
for i in `find . \( 		-name '*config*' -o \
				-name '*param*' -o \
				-name '*meet*' -o \
				-name '*status*' -o \
				-name '*.90????' -o \
				-name README -o \
				-name '*.spec' -o \
				-name '*.hqx' -o \
				-name Makefile -o \
				-name '*.c' -o \
				-name '*.h' -o \
				-name '*.sh' -o \
				-name '*.1' -o \
				-name '*.doc*' -o \
				-name '*,v' \) \
			$NEWER_SPEC \
			-print`
  do
    echo "$i --> $DESTDIR/$i" | tee -a $DESTDIR/backup-log
    rm -f $DESTDIR/$i
    $CP $i $DESTDIR/$i
  done

date > $DESTDIR/backup-date
echo Backup of $SOURCEDIR to $DESTDIR finished `date` | \
		tee -a $DESTDIR/backup-log
echo " " >> $DESTDIR/backup-log

if test $# = 3
  then
    echo See $DESTDIR/backup-log for details. | \
	/usr/ucb/mail -s "Backup of $SOURCEDIR to $DESTDIR complete." $USERS
  fi
