#!/bin/sh
#
# $Header: /home/kbackup/CVSROOT/KBackup/src/verify_src,v 1.21 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
#
#
#	subroutine for verifying existing archives
#	uses read_data() for input
#
#

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

Verify_src_loaded=YES

Verify_II()	# accepts option "auto" to avoid insert-media-prompt
{
	save_options
	backup_filesperarchive="???"
	backup_version="???"
	backup_date="???"
	backup_compression="???"
	backup_type="???"
	backup_archive_format="???"
	backup_multivolume="???"
	backup_donly="???"
	backup_use_multibuf="???"
	backup_multibuf_seq_info="???"
	backup_multibuf_blksize="???"
	backup_multibuf_nblocks="???"
	backup_parent="???"
	backup_numversion=0
	
	old_compression="$compression"
	
	if [ "$device_type" = "FLOPPY" ]
	then
		ErrorBox "Verify for disk archives not implemented."
		return
	fi

	if [ "$1" != "auto" ]
	then
		insert_media
		if [ $? != 0 ] 
		then
			return
		fi
	fi	

	auto_rewind
	
	if [ "$device_type" = "TAPE" ]
	then
		InfoBox "Reading header..." 
		read_data -raw $device >$TMP/$TMPFILE 2>$STDERR
		check_error -dev
		if [ $? != 0 ]
		then
			return
		fi
		retry=2
		while [ $retry -gt 0 ]
		do
			read firstline < $TMP/$TMPFILE 
			if [ $? = 0 ] && [ "$firstline" = "#KBackup" ]
			then
				retry=0
				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
			else
				if [ "$1" != "auto" ] || [ $retry -eq 1 ]
				then
					ErrorBox "No valid KBackup Archive Header."
					return
				else
					MsgBox \
"Cannot read archive header.
If this is a multivolume archive,
please insert the first volume!"
					retry=`expr $retry - 1`
				fi
			fi
		done # while retry	
		report "Verify: Archive header:     OK"
		
		dirstatus=
		archstatus=		
		$DIALOG   --title "Verify" --infobox "\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 MultiBuf: ................. $backup_use_multibuf\n\
  MultiBuf blocksize: ............. $backup_multibuf_blksize\n\
  MultiBuf number of blocks: ...... $backup_multibuf_nblocks\n\
\n\
Parent directory of backup: ....... $backup_parent\n\
Contents directory: ............... $dirstatus\n\
Archive status: ................... $archstatus" 20 75 

case $backup_compression in
			GZIP) ;;
			COMPRESS) ;;
			NONE) ;;
			PGP) ;;
			*)	beep
				$DIALOG   --title "Error" --msgbox "\nNo KBackup archive or\nunknown compression method." 7 40
				backup_compression="???"
				restore_options
				return ;;
		esac
		if [ "$backup_compression" != "???" ] ; then
			compression="$backup_compression"
			set_compress
			if [ "$backup_compression" = "PGP" ]
			then
				GetPGPPASS
			fi
		fi

			$DIALOG   --title "Verify" --infobox "\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 MultiBuf: ................. $backup_use_multibuf\n\
  MultiBuf blocksize: ............. $backup_multibuf_blksize\n\
  MultiBuf number of blocks: ...... $backup_multibuf_nblocks\n\
\n\
Parent directory of backup: ....... $backup_parent\n\
Contents directory: ............... $dirstatus\n\
Archive status: ................... $archstatus" 20 75 

(read_data | $uncompress )>/dev/null 2>$STDERR 3<$TMP/$PHRASEFILE </dev/null
		if check_compress_success
		then
			dirstatus=OK
			report "Verify: Contents directory: OK"
		else
			dirstatus=ERR
			report "Verify: Contents directory: Corrupted"
			beep
			$DIALOG   --title "Error" --yesno "\nContents directory corrupted.\nCheck archive contents anyway?" 8 40
			if [ $? != 0 ]
			then
				restore_options
				clear_PGP
				return
			fi
		fi
			$DIALOG   --title "Verify" --infobox "\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 MultiBuf: ................. $backup_use_multibuf\n\
  MultiBuf blocksize: ............. $backup_multibuf_blksize\n\
  MultiBuf number of blocks: ...... $backup_multibuf_nblocks\n\
\n\
Parent directory of backup: ....... $backup_parent\n\
Contents directory: ............... $dirstatus\n\
Archive status: ................... $archstatus" 20 75 

		if [ $backup_archive_format = TAR ]
		then
			(read_data | $uncompress | $TAR $xopt -t -f -) 2>$STDERR >/dev/null 3<$TMP/$PHRASEFILE </dev/null
		else	# AFIO
			xopt=
			if [ "$device_type" = "FLOPPY" ]
			then
				blocks=`echo $floppy_format|$SED '1,$ s/\(.*\)\/.*/\1k/g' `
				xopt="$xopt -F -s $blocks "
			fi
	
			if [ "$compression" = "GZIP" ]
			then
				xopt="$xopt -Z"
				num=`echo $compress_options | $SED '1,$ s/-\([0-9]\).*/\1/g'`
				case "$num" in
				"0"|"1"|"2"|"4"|"5"|"6"|"7"|"8"|"9")
					xopt="$xopt -G $num"
					;;
				esac
			fi
			
			reset
			clear
			( read_data | $AFIO -t $xopt - ) 2>$STDERR 3<$TMP/$PHRASEFILE
			if [ $? != 0 ]
			then
				ErrorBox "Archive is corrupted."	
				restore_options
				clear_PGP	
				return
			fi
		fi

		if [ $? -eq 0 ]
		then
			archstatus=OK
		else
			archstatus=Corrupted
		fi
		
		report "Verify: Archive status:     $archstatus"


$DIALOG   --title "Verify" --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 MultiBuf: ................. $backup_use_multibuf\n\
  MultiBuf blocksize: ............. $backup_multibuf_blksize\n\
  MultiBuf number of blocks: ...... $backup_multibuf_nblocks\n\
\n\
Parent directory of backup: ....... $backup_parent\n\
Contents directory: ............... $dirstatus\n\
Archive status: ................... $archstatus" 20 75 

else
		beep
		$DIALOG   --title "Error" --msgbox "\nThere's no verify for files." 7 40
	fi
	clear_PGP
	restore_options
}

