#!/bin/sh
#
# $Header: /home/kbackup/CVSROOT/KBackup/src/restore_src,v 1.24 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
#
#
# subroutines doing all restore operation,
# get loaded by Restore() on demand
#

Restore_src_loaded=YES

if [ "$ReadData_src_loaded" != "YES" ]
then
	. $SRCDIR/$READDATA_SRC
fi

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

Restore_II()
{
	save_options

	if [ "$restore_write_log" = "YES" ]
	then
		rest_logfile="$TMP/$TARLOGFILE"
	else
		rest_logfile=/dev/null
	fi
	
	if [ "$scheduled" != "YES" ]
	then
		insert_media
		if [ $? != 0 ]
		then
			return
		fi
	fi

	auto_rewind
	
	if [ "$device_type" = "BLOCKDEV" ]
	then
		if [ -f "$MOUNTPOINT/$BLOCKDEV_HEADER" ]
		then
			dirfile="$MOUNTPOINT/$BLOCKDEV_DIRECTORY"
			header="$MOUNTPOINT/$BLOCKDEV_HEADER"
			archive="$MOUNTPOINT/$BLOCKDEV_ARCHIVE"
		else
			dirfile="$MOUNTPOINT/$arch_filename.dir"
			header="$MOUNTPOINT/$arch_filename.hdr"
			archive="$MOUNTPOINT/$arch_filename.arc"
		fi
		get_blockdev READ
	else if [ "$device_type" = "FILE" ]
		then
			dirfile="$arch_filename.dir"
			header="$arch_filename.hdr"
			archive="$arch_filename.arc"
		else
			dirfile="$device"
			header="$device"
			archive="$device"
		fi
	fi
	
	if [ "$scheduled" = "YES" ]
	then
		echo "Starting scheduled restore at `date`:" >$TMP/$MSGFILE
	else
		Restore_part_I
		if [ $? != 0 ]
		then
			restore_options
			return
		fi
	fi

	InfoBox "Filtering list..."

	# 	- remove all afio specific information from logfile:
	# 	- remove one possible remark from tar:
	# 	- remove leading slashes:
	#
	#	extract filename from:
	#	^file size uid gid wday month day hh:mm:ss year perm$
	#	this should handle filenames with ANY characters properly
	$SED "\
1,$	s/ -- okay$//g
1,$	s/\.z -- ([0-9][0-9]*\%)$//g
1,$	s/^tar: .* is the archive; not dumped$//g
1,1	s/^Generating logfile...$//g
1,$	s/^Archive size: .*$//g
1,$	s/^[0-9+]* records in$//g
1,$	s/^[0-9+]* records out$//g
1,$	s/^\/\(.*\)$/\1/g
1,$	s/^\\(.*\) [0-9][0-9]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* ..:..:.. [^ ][^ ]* [^ ][^ ]*$/\1/g
1,$	s/^\.\///g
" <$TMP/$DIRLISTFILE >$TMP/$TARLISTFILE
	check_error

	InfoBox "Restoring files..."

	if [ $compression = PGP ]
	then
		GetPGPPASS
	fi

	cd "$restore_dir"
	if [ "$backup_archive_format" = "TAR" ]
	then
		Tar_Restore

	else		# AFIO
		Afio_Restore
	fi
	clear_PGP
	if [ "$restore_write_log" = "YES" ]
	then
		logfile="$TMP/RESTORED-`date +'%y-%m-%d--%X'`"
		if [ $scheduled = NO ]
		then
			cat $rest_logfile | $compress >$logfile
			beep
			$DIALOG   --title $logfile --textbox $rest_logfile 23 75
		else
			cat $TMP/$MSGFILE $rest_logfile | $compress >$logfile
			rm -rf $datadir/$schedulename
			rm -f $TMP/$MSGFILE
		fi
	fi
	restore_options
	release_blockdev
}

Restore_part_I()
{
	old_compression="$compression"
	backup_numversion=0

	if [ "$device_type" != "FLOPPY" ]
	then
		InfoBox "Reading header..."
		read_data -raw $header >$TMP/$TMPFILE 2>$STDERR
		check_error -dev
		if [ $? != 0 ]
		then
			return 1
		else
			if read firstline < $TMP/$TMPFILE && test "$firstline" = "#KBackup" 
			then
				backup_use_multibuf="???"
				. $TMP/$TMPFILE
				compression="$backup_compression"
				if [ "$backup_use_multibuf" = "YES" ]
				then
					use_multibuf="$backup_use_multibuf"
					multibuf_blksize="$backup_multibuf_blksize"
					multibuf_nblocks="$backup_multibuf_nblocks"
					multibuf_seq_info="$backup_multibuf_seq_info"
				else
					use_multibuf=NO
				fi

       					$DIALOG   --title "Archive header information" --msgbox "\n\
Archive created by KBackup Version: $backup_version\n\
  files per archive: .............. $backup_filesperarchive\n\
Creation date: .................... $backup_date\n\
Backup type: ...................... $backup_type\n\
Archive format: ................... $backup_archive_format\n\
Compression method: ............... $backup_compression\n\
Directories-only flag: ............ $backup_donly\n\
using \"old\" MultiBuf: ........... $backup_use_multibuf\n\
  blocksize: ...................... $backup_multibuf_blksize\n\
  number of blocks: ............... $backup_multibuf_nblocks\n\
  using sequence information: ..... $backup_multibuf_seq_info\n\
Parent directory of backup: ....... $backup_parent\n" 19 75

			else
				ErrorBox "Cannot find a KBackup archive header!"
				return 1
			fi
		fi
	fi

	MsgBox \
"You are now prompted to choose
the relative root directory."

		if [ "$backup_parent" = "" ]
	then
		backup_parent=/
	fi
	
	choose_dir "$backup_parent"
	restore_dir="$choose_dir_result"

	set_compress

	restore_all=NO
	while :
	do
	$DIALOG   --menu "Please choose:" 11 75 4\
		A	"Restore all files from archive" \
		C	"Initialise editable list of files from contents directory"\
		L	"Initialise editable list of files from logfile"\
		"<"	"Return to main menu" 2>$TMP/$DIALOGFILE 
	if [ $? = 0 ]
	then
		case `cat $TMP/$DIALOGFILE` in
		A)	InfoBox "Reading directory..." 
			if [ "$device_type" != "FLOPPY" ]
			then
				read_data $dirfile >$TMP/$TMPFILE  2>$STDERR
				$uncompress <$TMP/$TMPFILE >$TMP/$DIRLISTFILE 2>$STDERR 3<$TMP/$PHRASEFILE
				check_compress_success 
				if [ $? != 0 ]
				then
					beep
				$DIALOG   --title "Error" --msgbox "
Reading/Decompressing contents
directory failed.

Error message:
`cat $STDERR`
" 12 40
					return 1
				fi
			fi
			restore_all=YES
			return 0
			;;
		C)	if [ "$device_type" = "FLOPPY" ]
			then
				ErrorBox "Floppy archives do not have a contents directory!"
				return 1
			fi
			InfoBox "Reading contents directory..." 
			read_data $dirfile >$TMP/$TMPFILE 2>$STDERR
			check_error 
			if [ $? != 0 ] ; then return 1; fi
			$uncompress <$TMP/$TMPFILE >$TMP/$DIRLISTFILE 2>$STDERR 3<$TMP/$PHRASEFILE
			check_compress_success 
			if [ $? != 0 ]
			then
				beep
				$DIALOG   --title "Error" --msgbox "
Reading/Decompressing contents
directory failed.

Error message:
`cat $STDERR`
" 12 40
				return 1
			fi
			check_error
			if [ $? != 0 ] ; then return 1; fi
			clear
			reset
			$EDITOR $TMP/$DIRLISTFILE 2>$STDERR
			check_error
			if [ $? != 0 ] ; then return 1; fi
			$DIALOG --title " Restore " --yesno "Continue with restore?" 7 32 
			if [ $? = 0 ]
			then
				return 0
			else
				return 1
			fi
			;;
		L)	find $datadir/$configname -name "*Log*" -printf "%f %Tx\n" | sort >$TMP/$TMPFILE
			lines=`wc -l <$TMP/$TMPFILE`
			if [ "$lines" = "" -o "$lines" = "0" ]
			then
				MsgBox "Could not find any logfiles."
				return 1	# error
			fi
			if [ "$lines" -gt 15 ] ; then lines=15 ; fi
			$DIALOG   --menu "Choose a logfile:" 22 60 $lines \
				`cat $TMP/$TMPFILE` 2>$TMP/$DIALOGFILE
			check_error
			
			( uncompress_logfile $datadir/$configname/`cat $TMP/$DIALOGFILE` && cat $TMP/$TMPFILE >$TMP/$DIRLISTFILE ) 2>$STDERR 3<$TMP/$PHRASEFILE
			check_error
			if [ $? != 0 ] ; then return 1; fi
			clear
			reset
			$EDITOR $TMP/$DIRLISTFILE 2>$STDERR
			check_error 
			if [ $? != 0 ] ; then return 1; fi
			$DIALOG --title " Restore " --yesno "Continue with restore?" 7 32 
			if [ $? != 0 ]
			then
				return 1
			fi
			if [ "$device_type" != "FLOPPY" ]
			then
				InfoBox "Skipping contents directory..." 
				read_data $dirfile >/dev/null 2>$STDERR
				check_error
			fi
			return 0
			;;
		"<")	compression="$old_compression"
			return 1
			;;
		esac
	else
		compression="$old_compression"
		return 1
	fi
	done
	return 0
}

Tar_Restore()
{
	xopt="$STD_TAROPT_REST"
	
	if [ $restore_all = NO ]
	then
		xopt="$xopt --files-from $TMP/$TARLISTFILE"
	else
		files=
	fi

	if [ $followtarops = YES ]
	then
		clear
		reset
		xopt="$xopt -v -v"
		echo "( read_data $archive \
		| $TAR $tar_user_opts $xopt --extract $tarcompress -f - )" >$TMP/$TMPFILE
		. $TMP/$TMPFILE 2>&1 3<$TMP/$PHRASEFILE | tee $rest_logfile
		echo -e "\nPress ENTER to continue..."
		read
	else
		echo "( read_data $archive | $TAR $tar_user_opts $xopt --extract \
		$tarcompress -f - )" >$TMP/$TMPFILE
		. $TMP/$TMPFILE 2>&1 3<$TMP/$PHRASEFILE >$rest_logfile
	fi
}	

Afio_Restore()
{
	xopt="$STD_AFIOOPT_REST"

	afio_bug_save_programs

	if [ "$device_type" = "FLOPPY" ]
	then
		blocks=`echo $floppy_format|$SED '1,$ s/\(.*\)\/.*/\1k/g' `
		xopt="$xopt -F -s $blocks "
	fi
	
	if [ "$protect_newer" = "YES" ]
	then
		xopt="$xopt -n "
	fi

	if [ $compression != NONE ]
	then
		xopt="$xopt -Z"
		if [ "$HasAFIO242" != "YES" ]
		then
			if [ $compression != GZIP ]
			then
				mkdir $TMP/$BINDIR
				cp $TMP/$COMPRESSFILE  $TMP/$BINDIR/gzip
				oldpath="$PATH"
				export PATH="$TMP/$BINDIR:$PATH"
			fi
		else	# new afio
			xopt="$xopt -P \"$compress_program\""
			if [ -n "$decompress_options" ]
			then
				for i in $decompress_options
				do
					xopt="$xopt -Q \"$i\" "
				done
				if [ "$compression" = PGP ]
				then
					xopt="$xopt -Q -f "
				fi
			else
				xopt="$xopt -Q -d -Q -c "
			fi
		fi
	fi

	if [ $restore_all = YES ]
	then
		if [ $followtarops = YES ]
		then
			clear
			reset
			echo "( read_data $archive | $AFIO -i $afio_user_opts $xopt -vz - )" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 3<$TMP/$PHRASEFILE | tee $rest_logfile 
			echo -en "\nPress RETURN key to continue..."
			read
		else
			echo "( read_data $archive | $AFIO -i $afio_user_opts $xopt -vz - ) ">$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 3<$TMP/$PHRASEFILE >$rest_logfile 
		fi
	else
		if [ $followtarops = YES ]
		then
			clear
			reset
			echo "( read_data $archive | $AFIO -i $afio_user_opts $xopt -vz -w $TMP/$TARLISTFILE - )" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 3<$TMP/$PHRASEFILE | tee $rest_logfile 
			echo -en "\nPress RETURN key to continue..."
			read
		else
			echo "( read_data $archive | $AFIO -i $afio_user_opts $xopt -w $TMP/$TARLISTFILE -vz - )" >$TMP/$TMPFILE
			. $TMP/$TMPFILE 2>&1 3<$TMP/$PHRASEFILE >$rest_logfile 
		fi
	fi
	if [ $compression != GZIP -a $compression != NONE ]
	then
		rm -rf $TMP/$BINDIR
		export PATH="$oldpath"
	fi
	afio_bug_restore_path
}
