#!/bin/bash

# need system config info
if [ -f /etc/sysconfig/tape ]; then
	. /etc/sysconfig/tape
else
	echo "Sorry, no /etc/sysconfig/tape config file found."
	exit 1
fi

# need backup config info
if [ ! -f "$BACKUPTAB" ]; then
	echo "Sorry, no $BACKUPTAB config file found."
	exit 1
fi

# who am i?
HOSTNAME=`hostname`

# setup the tape drive based on given info from /etc/sysconfig/tape
tape_setup () {

	echo -e "Backup started at $DATE.\nLogging to $LOG" >&2
	echo "$DATE" > $LOG

	echo -n "Setting up tape ... " >&2
	echo "rewinding" >&2
	mt -f $DEV rewind >> $LOG 2>&1
	echo "                    setting block size to $BLOCKSIZE" >&2
	mt -f $DEV setblk $BLOCKSIZE >> $LOG 2>&1
	rm -f $LIST*
	cat $BACKUPTAB | grep -v "^#" | grep -v "^$" | while read i; do
		set $i
		rsh $1 rm -f $LOGROOT/incr*
	done
}

tape_done () {
	sleep $SLEEP
	BLOCK=$(mt -f $DEV tell)
	DATE=$(date)
	echo -e "$DATE\n$BLOCK\nBackup completed." >> $LOG
	echo -e "Backup complete at $DATE.\nLogged to $LOG" >&2

	echo "Compressing $LOG in background ..."
	gzip -9 $LOG &

	echo "$BLOCK"
}

tape_rewind () {
# some crap code to only rewind after all incrementals are
# done.  Needs to be re-worked.
#if [ `cat $COUNTER` -gt 3 ]; then
#	tape_rewind
#	rm -f $COUNTER
#fi
#
# mt -f /dev/nst0 offline

	echo "Rewinding tape ..."
	mt -f $DEV rewind
}

# $1 is the error condition
# $2 is the machine
# $3 is the dir it was backing up
mail_it () {

	[ $1 -ne 0 ] && \
                cat << EOF | mail -s "Backups Failed" "$ADMIN"

Hi, this is the backup program at $HOSTNAME.  Your backups failed
for:

        $2:$3

EOF

}

# $1 should be the machine name
# $2 should be the dir to back up
# $3 should be the exclude file
local_backup() {
	expr `cat $DOTCOUNT` + 1 > $DOTCOUNT
	EXCLUDE=""
        MACHINE="$1"
        BACKUP_DIR="$2"
        if [ "$3" != "" ]; then
                EXCLUDE="-X $3"
        fi 
        sleep $SLEEP
        date >> $LOG
        BLOCK=$(mt -f $DEV tell)
        echo -e "Backing up $MACHINE:$BACKUP_DIR ...\n$BLOCK" >> $LOG
        echo -e "Backing up $MACHINE:$BACKUP_DIR ...\n$BLOCK" >&2
        tar -cvlSp -g "$LIST-`cat $DOTCOUNT`" -b 64 -V "Date: `date` *** Volume: $MACHINE:$BACKUP_DIR" $EXCLUDE -f $DEV $BACKUP_DIR >> $LOG 2>&1
	mail_it $? $MACHINE $BACKUP_DIR
}


# $1 should be the machine name
# $2 should be the dir to back up
# $3 should be the exclude file
remote_backup() {
	expr `cat $DOTCOUNT` + 1 > $DOTCOUNT
	EXCLUDE=""
        MACHINE="$1"
        BACKUP_DIR="$2"
        if [ "$3" != "" ]; then
                EXCLUDE="-X $3"
        fi 
        sleep $SLEEP
        date >> $LOG
        BLOCK=$(mt -f $DEV tell)
        echo -e "Backing up $MACHINE:$BACKUP_DIR ...\n$BLOCK" >> $LOG
        echo -e "Backing up $MACHINE:$BACKUP_DIR ...\n$BLOCK" >&2
	rsh $MACHINE mkdir -p $LOGROOT < /dev/null
        rsh $MACHINE tar -cvlSp -g "\"$LIST-`cat $DOTCOUNT`\"" -b 64 -V "\"Date: `date` *** Volume: $MACHINE:$BACKUP_DIR\"" $EXCLUDE -f "\"$HOSTNAME:$DEV\"" $BACKUP_DIR < /dev/null >> $LOG 2>&1
	mail_it $? $MACHINE $BACKUP_DIR
}


full_backup () {
	echo 1 > $COUNTER
	echo 0 > $DOTCOUNT
	mkdir -p $LOGROOT/$DAY
	LOG=$LOGROOT/$DAY/full-backup-log-$SHORTDATE
	
	tape_setup

	# cycle through /etc/backuptab backing up from everything
	# that's there...	

	cat $BACKUPTAB | grep -v "^#" | grep -v "^$" | while read i; do
		set $i
		if [ $1 = $HOSTNAME -o $1 = "localhost" ]; then
			local_backup $1 $2 $3
		else 
			remote_backup $1 $2 $3
		fi
	done

	tape_done
}

incr_backup () {

	echo 0 > $DOTCOUNT
	mkdir -p $LOGROOT/$DAY
	LOG=$LOGROOT/$DAY/incr-backup-log-$SHORTDATE

        
	# cycle through /etc/backuptab backing up from everything
	# that's there...	

	cat $BACKUPTAB | grep -v "^#" | grep -v "^$" | while read i; do
		set $i
		if [ $1 = $HOSTNAME -o $1 = "localhost" ]; then
			local_backup $1 $2 $3
		else 
			remote_backup $1 $2 $3
		fi
	done

        tape_done
	expr `cat $COUNTER` + 1 > $COUNTER
}

test_con () {

	cat $BACKUPTAB | grep -v "^#" | grep -v "^$" | while read i; do
		set $i
		if [ ! $1 = $HOSTNAME -a ! $1 = "localhost" ]; then
			echo "Testing $1"
			rsh $1 "rsh $HOSTNAME date" < /dev/null > /dev/null
			if [ $? -ne 0 ]; then
				echo "Test failed!"
				exit 1
			else
				echo "Test passed."
			fi
		fi
	done

}

case "$1" in
	--full)
	full_backup
	;;
	--incremental)
	incr_backup
	;;
	--test)
	test_con
	;;
	*)
	# Need correct argument...
	echo "Usage: full-backup --incremental|--full|--test"
	exit 2
	;;
esac

# No need to rewind tape...full_backup will do that and incr_backup
# expects to be at the end of the tape anyway.
#
# tape_rewind

echo "Done."
