#!/bin/sh
#
# $Header: /home/kbackup/CVSROOT/KBackup/src/backup_src,v 1.28 1997/09/19 19:49:25 kbackup Exp $
#
# This file is Copyright (C) 1995-1997 by Karsten Ballder (Ballueder@usa.net)
#
# It is part of the KBackup package, see the file COPYING for details.
# KBackup and all files included in this package are licensed and protected
# under the terms and conditions of the GNU General Public License Version 2.
#
#	If you want to contact the current maintainer of this software, please
#	send e-mail to: KBackup@usa.net
#       Or visit the KBackup-Homepage at http://KBackup.home.ml.org/
#
# subroutines for writing backups:
#
#	Backup_II()	(entry point)
#	Tar_Backup()
#	Afio_Backup()
#
#	$Revision: 1.28 $

if [ "$WriteData_src_loaded" != "YES" ]
then
	. $SRCDIR/$WRITEDATA_SRC
fi

if [ "$Blockdev_src_loaded" != "YES" ]
then
	. $SRCDIR/$BLOCKDEV_SRC
fi

Backup_src_loaded=YES

Tar_Backup()
{
	taropt="$STD_TAROPT_BACK"

	if [ $followtarops = YES ]
	then
		reset
		clear
	fi
	
	if [ $verify_archive = YES -a "$device_type" = "FLOPPY" ]
	then
		taropt="$taropt -W"
	fi

	if [ $local = YES ]
	then
		taropt="$taropt -l"
	fi

	if [ "$device_type" = "FILE" ]
	then
		writeto="$arch_filename.arc"
	fi
	
	if [ "$device_type" = "BLOCKDEV" ]
	then
		writeto="$MOUNTPOINT/$arch_filename.arc"
	else if [ "$device_type" = "FILE" ]
		then
			writeto="$arch_filename.arc"
		else
			writeto="$device"
		fi
	fi

	if [ $followtarops = YES ]
	then
		echo "( $TAR $tar_user_opts $taropt --create . \
			--exclude-from $TMP/$EXCLUDE -v -f - \
			$tarcompress | write_data $writeto )" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 | tee $TMP/$TARLOGFILE 
	else
		echo "( $TAR i$tar_user_opts $taropt --create . \
			--exclude-from $TMP/$EXCLUDE -v -f -\
			$tarcompress |  write_data $writeto )" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>>$TMP/$TARLOGFILE 
	fi

	if [ $? != 0 ]
	then
		beep
		ErrorBox \
"Something went wrong while writing
archive.
Read logfile carefully."
abort=YES
#	else
#	BAD because of auto-verify and scheduling:
#		if [ $followtarops = YES ]
#		then
#			echo -en "\nPress RETURN key to continue..."
#			read
#		fi
	else
		abort=NO
	fi
}

Afio_Backup()
{
	afioopt="$STD_AFIOOPT_BACK"

	oldpath="$PATH"
	if [ $compression != NONE ]
	then
		afioopt="$afioopt -Z"
		if [ "$HasAFIO242" != YES ]
		then	# alway generate fake gzip for afio
			mkdir $TMP/$BINDIR
			cp $TMP/$COMPRESSFILE  $TMP/$BINDIR/gzip
			export PATH="$TMP/$BINDIR:$PATH"
		else
			afioopt="$afioopt -P \"$compress_program\""
			if [ -n "$compress_options" ]
			then
				for i in $compress_options
				do
					afioopt="$afioopt -Q \"$i\" "
				done
			fi
			if [ "$compression" = "PGP" ]
			then
				afioopt="$afioopt -U -Q -f -Q -e -Q \"$pgpkey\" "
			fi
		fi
	fi

	if [ $verify_archive = YES -a "$device_type" = "FLOPPY" ]
	then
		afioopt="$afioopt -K"
	fi

	if [ $followtarops = YES ]
	then
		clear; reset
	fi

	if [ "$device_type" = "BLOCKDEV" ]
	then
		writeto="$MOUNTPOINT/$arch_filename.arc"
	else if [ "$device_type" = "FILE" ]
		then
			writeto="$arch_filename.arc"
		else
			writeto="$device"
		fi
	fi

	if [ "$device_type" != "FLOPPY" ]
	then
		#	use multibuf:
		if [ $followtarops = YES ]
		then
			echo "( cat $TMP/$TARLISTFILE | \
			$AFIO -o -v $afio_user_opts $afioopt - | write_data $writeto )" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 | tee $TMP/$TARLOGFILE 
		else
			echo "( cat $TMP/$TARLISTFILE | \
			$AFIO -o -v $afio_user_opts $afioopt - | write_data $writeto )" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 >$TMP/$TARLOGFILE
		fi
	else	# floppy:
		echo "fdformat $device" >$TMP/$TMPFILE2
		chmod +x $TMP/$TMPFILE2
		blocks=`echo $floppy_format|$SED '1,$ s/\(.*\)\/.*/\1k/g' `
		floppy_opts="-F -s $blocks -R $TMP/$TMPFILE2 "
		if [ $followtarops = YES ]
		then
			echo "cat $TMP/$TARLISTFILE | \
			$AFIO -o -v $afioopt $floppy_opts $device" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 | tee $TMP/$TARLOGFILE
		else
			echo "cat $TMP/$TARLISTFILE | \
			$AFIO -o -v $afioopt $floppy_opts $device " >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 >$TMP/$TARLOGFILE 
		fi
	fi

	if [ $? != 0 ]
	then
		$DIALOG   --title "Error" --msgbox "
Something went wrong while writing
archive.
Read logfile carefully.
" 7 40
	abort=YES
#	else
#		if [ $followtarops = YES ]
#		then
#			echo -en "\nPress RETURN key to continue..."
#			read
#		fi
	else
		abort=NO
	fi
	if [ $compression != NONE ]
	then
		rm -rf $TMP/$BINDIR
		export PATH="$oldpath"
	fi
}

Backup_II()
{	
	report --init
	
	if [ "$scheduled" = "YES" ]
	then
		echo "Starting scheduled backup at `date`.\n\n" >$TMP/$MSGFILE
	else
		insert_media
		if [ $? != 0 ] ; then return; fi
	fi

	InfoBox "Creating list of files..."
	cd $parent
	build_dir_list

	if [ $scheduled = NO -a $manualedit = YES ] ; then
		$EDITOR $TMP/$DIRLISTFILE
	fi

	multibuf_options="$STD_MULTIBUF_OPT"
	if [ "$multibuf_seq_info" = "YES" ]
	then
		multibuf_options="-h "
	fi

	FILESPERARCHIVE="$FILESPERARCHIVE_STD"
	if [ "$multibuf_seq_info" = "YES" ]
	then
		FILESPERARCHIVE=`expr $FILESPERARCHIVE + 2`
	fi

	get_blockdev	# format and mount blockdev if needed
	if [ $? != 0 ]
	then
		return
	fi

	if [ "$device_type" != "FLOPPY" ]
	then
		if [ "$device_type" = "FILE" ]
		then
			dirfile="$arch_filename.dir"
			writeto="$arch_filename.hdr"
		else if [ "$device_type" = "BLOCKDEV" ]
			then	
				dirfile="$MOUNTPOINT/$arch_filename.dir"
				writeto="$MOUNTPOINT/$arch_filename.hdr"
			else
				dirfile="$device"
				writeto="$device"
			fi
		fi
		InfoBox "Writing header..."
		cat >$TMP/$TMPFILE <<_EOF_
#KBackup
#	Archive, written by KBackup Version $VERSION
#
#	KBackup is Copyright (C) 1995 by Karsten Ballder
#	For information on how to get a copy of it, refer to
#	http://www.uni-karlsruhe.de/~Karsten.Ballueder
#	or get it via anonymous ftp from: 
#	ftp://sunsite.unc.edu/pub/linux/system/Backup
#
_EOF_
		echo backup_version="$VERSION" >>$TMP/$TMPFILE
		echo backup_numversion=$NUMVERSION >>$TMP/$TMPFILE
		if [ "$incremental" = "YES" ] ; then
			echo backup_type=incremental >>$TMP/$TMPFILE
		else
			echo backup_type=full >>$TMP/$TMPFILE
		fi
		echo backup_filesperarchive="$FILESPERARCHIVE" >>$TMP/$TMPFILE
		echo backup_compression="$compression" >>$TMP/$TMPFILE
		echo backup_date=\"`date`\" >>$TMP/$TMPFILE
		echo backup_donly="$directoriesonly" >>$TMP/$TMPFILE
		echo backup_archive_format="$archive_format" >>$TMP/$TMPFILE
		echo backup_use_multibuf="$use_multibuf" >>$TMP/$TMPFILE
		echo backup_multibuf_blksize="$multibuf_blksize" >>$TMP/$TMPFILE
		echo backup_multibuf_nblocks="$multibuf_nblocks" >>$TMP/$TMPFILE
		echo backup_multibuf_seq_info="$multibuf_seq_info" >>$TMP/$TMPFILE
		echo backup_parent="$parent" >>$TMP/$TMPFILE
		echo >>$TMP/$TMPFILE
		( cat $TMP/$TMPFILE | $DD ibs=2048 conv=sync | write_data -raw $writeto ) 2>$STDERR
		check_error
		if [ $? != 0 ]
		then
			release_blockdev
			return
		fi
	else	# device is floppy!
		setfdprm $device $floppy_format 2>$STDERR
		check_error
	fi

	set_compress
	if [ "$device_type" != "FLOPPY" ]
	then
		InfoBox "Writing directory..."
		(cat $TMP/$DIRLISTFILE | $compress > $TMP/$TMPFILE2 ) 2>$STDERR
		check_error
		( $DD if="$TMP/$TMPFILE2" ibs=$syncto conv=sync | write_data $dirfile ) 2>$STDERR
		check_error
		if [ $? != 0 ]
		then
			release_blockdev
		fi
	fi

	InfoBox "Writing archive..." 

	datestamp=`cat "$datadir/$configname/DateStamp"` 2>/dev/null
	newdate=`date +"%D %T"`
	# set logfile name now to get date/time right:
	logfile="$datadir/$configname/Log`date +'%y-%m-%d--%X'`"
	report "Writing log to: $logfile"
	
	if [ "$archive_format" = "AFIO" ]
	then
		Afio_Backup
	else
		Tar_Backup
	fi
	if [ $? = 0 ]
	then
		echo $newdate  > "$datadir/$configname/DateStamp"
	fi
	
	release_blockdev
	
	InfoBox "Generating logfile..."
	if [ $scheduled = YES ]
	then
		cat $TMP/$MSGFILE $TMP/$TMPFILE $TMP/$TARLOGFILE > $logfile
	else
		cat $TMP/$TMPFILE $TMP/$TARLOGFILE > $logfile
	fi


	# FIXME: BLOCKDEV verify not implemented yet!
	
	if [ "$verify_archive" = "YES" -a "$device_type" != "FLOPPY" -a "$abort" != "YES" ]
	then
		InfoBox "Verifying archive..."
		case "$device_type" in
		"TAPE")		InfoBox "Rewinding tape..."
				my_mt rewind -f "$device"
				;;
		"BLOCKDEV")	InfoBox "Re-mounting medium..."
				get_blockdev
				;;
		esac
		if [ $? != 0 ] 
		then 
			return  
		fi
		InfoBox "Starting Verify..."
		Verify auto
	fi
	
	tmp=`$FGREP "records in" <$logfile` 
	if [ $? = 0 ]
	then
		tmp=`echo $tmp | sed "1,$ s/^\([0-9]*\)+.*$/\1/g"`
		if [ $? = 0 ]
		then
			tmp=`expr $tmp / 1024` 2>/dev/null
			if [ $? = 0 ]
			then
				tmp="Archive size: $tmp MB"
				report $tmp
				echo "$tmp" >>$logfile
			fi
		fi
	fi
	
	if [ $scheduled = NO ]
	then
		beep
		$DIALOG   --title $logfile --textbox $logfile 23 75
	fi
	rm -f $TMP/$MSGFILE
	InfoBox "Compressing logfile..."
	if [ "$compression" = NONE ]
	then
		$STD_COMPRESS $logfile 2>$STDERR
		check_error
	else
		(cat <$logfile | $compress >$TMP/$TMPFILE) 2>$STDERR
		check_error
		mv -f $TMP/$TMPFILE $logfile 2>$STDERR
		check_error
	fi
	report "LogFile: $logfile (compressed)"
	if [ "$CreateReport" = "YES" -a "$ReportCommand" != "" ]
	then
		InfoBox "Processing Report file..."
		report "Report is being fed into: $ReportCommand"
		cat $TMP/$REPORTFILE | $ReportCommand 
	fi
}















