:
##########################################################################
# Title      :	transfer - transfer backup file to "safe" destination
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1994-02-03
# Requires   :	icat, sendfile
# SCCS-Id.   :	@(#) transfer	1.5 04/03/03
##########################################################################

PN=`basename "$0"`
VER='1.5'

recipient=my.address@domain.com
limit=1048576				# 1 MB

msg ()	  { echo >&2 "$PN: $*"; }
fatal ()  { msg "$@"; exit 1; }
absdir () { cd "$@" && pwd; }

if [ $# -lt 1 ] || [ ! -r "$1" ]
then
    echo >&2 "usage: $PN file [file ...]"
    exit 1
fi

: ${BACKUPDIR:=${RHOME:-$HOME}/tmp}

[ -d "$BACKUPDIR" -a -r "$BACKUPDIR" -a \
	-w "$BACKUPDIR" -a -x "$BACKUPDIR" ] || {
    echo "$PN: cannot copy to directory $BACKUPDIR"
    exit 1
}
BACKUPDIR=`absdir "$BACKUPDIR"`

: ${DISKSPOOL:=$HOME/disk}

# Move files separately
for backup
do
    Dir=`dirname "$backup"`
    Base=`basename "$backup"`
    if [ X"$Base" = X"" ]
    then
	msg "could not determine basename of $backup - ignored"
	continue
    fi

    if [ -d "$DISKSPOOL" -a -w "$DISKSPOOL" ]
    then
	ln "$backup" "$DISKSPOOL/$Base" >/dev/null 2>&1 ||
	    cp "$backup" "$DISKSPOOL/$Base"
    fi

    if [ X`absdir "$Dir"` != X"$BACKUPDIR" ]
    then
	bytes=`wc -c < "$backup"`
	if [ ${bytes:-0} -le $limit ]
	then
	    title="Automated backup `date \"+%Y-%m-%d %H:%M\"`"
	    {
	    	cat <<-EOT
		$title

		EOT

		# Try to provide a listing of the file contents
		case "$backup" in
		    *.cpio) 	  cpio -itv < "$backup" 2>/dev/null;;
		    *.tar) 	  tar tvf - < "$backup" 2>/dev/null;;
		    *.cpio.[!.]*) icat "$backup" | cpio -itv 2>/dev/null;;
		    *.tar.[!.]*)  icat "$backup" | tar tvf - 2>/dev/null;;
		    *) echo "File $backup:" `ls -ld "$backup"`;;
		esac
	    } |
	    sendfile -s "$title" "$recipient" "$backup"
	    mv "$backup" "$BACKUPDIR/$Base" ||
	    	msg "could not move file $backup - ignored"
	else
	    echo >&2 "NOTE: larger than limit $limit: $Base"
	fi
    fi
done
