#!/bin/sh
#
# $Header: /home/kbackup/CVSROOT/KBackup/src/subroutines,v 1.39 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
#
#
#################### MISCELLANEOUS SUBROUTINES ########################

# $Revision: 1.39 $


mysleep()
{
	tmp=""
	max=$waittime
	count=$max
	while [ $count -ge 1 ]
	do
		InfoBox -n "Waiting: $count..." 
		sleep 1
		count=`expr $count - 1`
	done
	InfoBox -n "Waiting: finished." 
}		

my_mt()
{
	if [ "$remote_mode" = YES ]
	then
		su "$remote_uid" -c "rsh $remote_host mt $*" 2>$STDERR
	else
		mt "$@" 2>$STDERR

	fi
	tmp=$?
		
	if [ $tmp != 0  -a "$3" = "eom" ]
	then
		mtst $1 $2 seod
		tmp=$?
	fi
	if [ $tmp != 0  -a "$3" = "eom" ]
	then
		mt_st $1 $2 seod
		tmp=$?
	fi
	check_error -dev
}



check_config_changed()
{
	if [ "$Configuration_changed" = "YES" ] && \
		$DIALOG --title " Configuration changed " --yesno "
You have changed KBackup's configuration
or option settings.

Do you want to save this configuration?
This would make it the default configuration
to load the next time that you start KBackup.
" 11 50
	then
		Write_Config
	fi
	Configuration_changed=NO
}

set_multibuf_options()
{
	last_command=set_multibuf_options
	multibuf_options="$STD_MULTIBUF_OPT"

	rm -f "$TMP/$MVOLFLAG" 2>/dev/null

	if [ "$multibuf_nblocks" = "" ]
	then
		multibuf_nblocks=1
	fi

	if [ "$multibuf_blocksize" = "" ] || [ $multibuf_blocksize -lt 1024 ]
	then
		multibuf_blocksize=1024
	fi

	if [ "$multibuf_seq_info" = "YES" ]
	then
		multibuf_options="$multibuf_options -h"	# add header
	fi
	if [ "$double_buffering" = "YES" ]
	then
		multibuf_options="$multibuf_options -m $ddbufsize"
	fi
	echo -e "#!/bin/sh\nMOUNTPOINT=\"$MOUNTPOINT\"\ndevice=\"$device\"\nblockdev_fs_type=\"$blockdev_fs_type\"\n" >$TMP/$MBUF_SCRIPT1
	cp $TMP/$MBUF_SCRIPT1 $TMP/$MBUF_SCRIPT2

	if [ "$device_type" = "BLOCKDEV" ]
	then
		multibuf_options="$multibuf_options -F " # write endmarker as file
		if [ "$1" = "WRITE" ]
		then
			if [ "$TTYMODE" = "YES" ]
			then
				echo -e "( $MBUF_BLOCKDEV_WCMD1_TTY ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT1
				echo -e "( $blockdev_mkfs && $MBUF_BLOCKDEV_WCMD2_TTY ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT2
			else
				echo -e "( $MBUF_BLOCKDEV_WCMD1 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT1
				echo -e "( $blockdev_mkfs && $MBUF_BLOCKDEV_WCMD2 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT2
			fi
		else
			if [ "$TTYMODE" = "YES" ]
			then
				echo -e "( $MBUF_BLOCKDEV_RCMD1_TTY ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT1
				echo -e "( $MBUF_BLOCKDEV_RCMD2_TTY ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT2
				echo -e "( $MBUF_BLOCKDEV_RCMDR_TTY ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPTR
			else
				echo -e "( $MBUF_BLOCKDEV_RCMD1 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT1
				echo -e "( $MBUF_BLOCKDEV_RCMD2 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT2
				echo -e "( $MBUF_BLOCKDEV_RCMDR ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPTR
			fi
			multibuf_options="$multibuf_options -R $TMP/$MBUF_SCRIPTR "
			chmod +x $TMP/$MBUF_SCRIPTR
		fi
		multibuf_options="$multibuf_options -c $TMP/$MBUF_SCRIPT1 "
		multibuf_options="$multibuf_options -C $TMP/$MBUF_SCRIPT2 "
	else
		if [ "$device_type" = "TAPE" ]
		then
			if [ "$TTYMODE" = "YES" ]
			then
				echo -e "( $MBUF_TAPE_CMD1_TTY ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT1
				echo -e "( $MBUF_TAPE_CMD2_TTY ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT2
			else
				echo -e "( $MBUF_TAPE_CMD1 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT1
				echo -e "( $MBUF_TAPE_CMD2 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT2
			fi
			multibuf_options="$multibuf_options -c $TMP/$MBUF_SCRIPT1 "
			multibuf_options="$multibuf_options -C $TMP/$MBUF_SCRIPT2 "
		fi
	fi

	if [ "$device_type" = "FILE" ]
	then
		multibuf_options="$multibuf_options -F " # write endmarker as file
		multibuf_options="$multibuf_options -R false "
		multibuf_options="$multibuf_options -c false "
		multibuf_options="$multibuf_options -C false "
	fi

	

	if [ "$multibuf_cmd1" != "" -a "$multibuf_cmd1" != "DEFAULT" ]
	then
		echo -e "#!/bin/sh\nMOUNTPOINT=\"$MOUNTPOINT\"\ndevice=\"$device\"\nblockdev_fs_type=\"$blockdev_fs_type\"\n" >$TMP/$MBUF_SCRIPT1
		cp $TMP/$MBUF_SCRIPT1 $TMP/$MBUF_SCRIPT2
		echo -e "( $multibuf_cmd1 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT1
		multibuf_options="$multibuf_options -c $TMP/$MBUF_SCRIPT1 "
	fi
	if [ "$multibuf_cmd2" != "" -a "$multibuf_cmd2" != "DEFAULT" ]
	then
		echo -e "#!/bin/sh\nMOUNTPOINT=\"$MOUNTPOINT\"\ndevice=\"$device\"\nblockdev_fs_type=\"$blockdev_fs_type\"\n" >$TMP/$MBUF_SCRIPT1
		cp $TMP/$MBUF_SCRIPT1 $TMP/$MBUF_SCRIPT2
		echo -e "( $multibuf_cmd2 ) </dev/tty >/dev/tty\n">>$TMP/$MBUF_SCRIPT2
		multibuf_options="$multibuf_options -C $TMP/$MBUF_SCRIPT2 "
	fi
	echo "touch $TMP/$MVOLFLAG" >>$TMP/$MBUF_SCRIPT2
	chmod +x $TMP/$MBUF_SCRIPT1 
	chmod +x $TMP/$MBUF_SCRIPT2
}

GetPGPPASS()
{
	if [ "$scheduled" = YES -o "$PGPPASS" != "" ]
	then
		return
	fi
	PGPPASS=
	um=`umask`
	umask 077	# no read for others
	$DIALOG --title " PGP pass phrase " --inputbox "
For decrypting files using pgp(1) you need to specify a 
pass phrase for unlocking your private key.

You will see what you type now, so make sure nobody can
see your screen!
" 16 75 2>$TMP/$DIALOGFILE
	rm -f $TMP/$PHRASEFILE
	cat $TMP/$DIALOGFILE >$TMP/$PHRASEFILE
	echo >>$TMP/$PHRASEFILE
	umask $um
	# test whether input redirec works:
	echo "test" >$TMP/$TMPFILE
	cat 0<$TMP/$TMPFILE >/dev/null
	if [ $? != 0 ]
	then
		rm -f $TMP/$PHRASEFILE
		touch $TMP/$PHRASEFILE
		export PGPPASS=`cat $TMP/$DIALOGFILE`
	else
		export PGPPASSFD=3
	fi
}

clear_PGP()
{
	PGPPASS=
	rm -f $TMP/$PHRASEFILE
	touch $TMP/$PHRASEFILE
}


choose_dir_tty()
{
	while :
	do
		echo -ne "
Please enter the name of the directory.
Just press enter to keep the default setting.

Default: \"$default\"

> "
		read tmp
		if [ -z "$tmp" ]
		then
			choose_dir_result="$default"
		else
			choose_dir_result="$tmp"
		fi
		if [ -d "$choose_dir_result" ]
		then
			return 
		else
			echo -e "\aNot a directory!"
		fi
	done
}

choose_dir()
{
	currdir=`pwd`
	default="$1"

	if [ "$TTYMODE" = "YES" ]
	then
		choose_dir_tty
		return $?
	fi

	cd "$default"

	
	while :
	do
		find * $FIND_STD_OPT -prune \( -type d -o -type l \) -printf "\"%f\" %u \\\\\n" 2>/dev/null  | sort >$TMP/$TMPFILE
		echo >>$TMP/$TMPFILE
		
		lines=14
		cat - $TMP/$TMPFILE >$TMP/$TMPFILE2 <<_EOF_
$DIALOG  --title " Directory selection " --menu \
"Current path:\n`pwd`\nSelect \"<<ACCEPT>>\"-entry to accept current path.\n\"<<MANUAL>>\"-entry to enter a path by hand." 23 60 $lines  "<<ACCEPT>>" "<<ACCEPT>>" "<<MANUAL>>" "<<MANUAL>>" .. Parent \\
_EOF_

		if . $TMP/$TMPFILE2  2>$TMP/$DIALOGFILE
		then
			result=`cat $TMP/$DIALOGFILE`
			if [ "$result" = "<<ACCEPT>>" ]
			then
				choose_dir_result=`pwd`
				cd "$currdir"
				return
			else
				if [ "$result" = "<<MANUAL>>" ]
				then
					$DIALOG --title " Path: " --inputbox "Please enter the pathname:" 9 75 2>$TMP/$DIALOGFILE
					if [ $? = 0 ]
					then
					  choose_dir_result=`cat $TMP/$DIALOGFILE`
					  if [ ! -d "$choose_dir_result" ]
					  then
					    ErrorBox "\"$choose_dir_result\"\nis not a directory!\nUsing old value\n\"$default\"."
					    choose_dir_result="$default"
					    return 
					  fi
					  cd "$currdir"
					  return
					fi
				else
					cd "$result"
				fi
			fi
		else
			cd "$currdir"
			choose_dir_result="$default"
			return 
		fi
	done
}



write_settings()
{
	cat <<_EOF_
#
#	Configuration "$configname" for KBackup
#
ConfigVersion=$VERSION
ConfigNumVersion=$NUMVERSION
configname="$configname"
parent="$parent"
local="$local"
directoriesonly="$directoriesonly"
findxpertmode="$findxpertmode"
findxpertflags="$findxpertflags"
specifiedfilesonly="$specifiedfilesonly"
compression="$compression"
compress_options="$compress_options"
verify_archive="$verify_archive"
archive_format="$archive_format"
pgpkey="$pgpkey"
incremental="$incremental"
ddbufsize="$ddbufsize"
double_buffering="$double_buffering"
device="$device"
floppy_format="$floppy_format"
device_type="$device_type"
blockdev_fs_type="$blockdev_fs_type"
blockdev_mkfs="$blockdev_mkfs"
datestamp="$datestamp"
resetdevice="$resetdevice"
manualedit="$manualedit"
followtarops="$followtarops"
scheduled_only_once="$scheduled_only_once"
use_multibuf="$use_multibuf"
multibuf_nblocks="$multibuf_nblocks"
multibuf_blksize="$multibuf_blksize"
multibuf_seq_info="$multibuf_seq_info"
multibuf_cmd1="$multibuf_cmd1"
multibuf_cmd2="$multibuf_cmd2"
use_patterns="$use_patterns"
manual_command="$manual_command"
restore_write_log="$restore_write_log"
remote_mode="$remote_mode"
remote_host="$remote_host"
remote_uid="$remote_uid"
CreateReport="$CreateReport"
ReportCommand="$ReportCommand"
AutoRewind="$AutoRewind"
protect_newer="$protect_newer"
afio_user_opts="$afio_user_opts"
tar_user_opts="$tar_user_opts"
arch_filename="$arch_filename"
syncto="$syncto"
waittime="$waittime"
TMP="$realTMP"
if [ "\$EDITOR" = "" ] 
then
	EDITOR="$EDITOR"
fi
_EOF_
}

save_configname()
{
	Configuration_changed=NO
	cat >$HOME/$KBACKRCDEFAULT <<_EOF_
#
#	KBackup base configuration
#
kbackdir="$kbackdir"
datadir="$datadir"
configname="$configname"
_EOF_
}

save_config()
{
	save_configname

	mkdir $datadir/$configname 2>/dev/null

	write_settings > $datadir/$configname/Config
	
	if [ ! -f $datadir/$configname/Exclude_Files ] ; then
		cat $datadir/$oldconfig/Exclude_Files >$datadir/$configname/Exclude_Files
	fi
	if [ ! -f $datadir/$configname/Include_Files ] ; then
		cat $datadir/$oldconfig/Include_Files >$datadir/$configname/Include_Files
	fi
	if [ ! -f $datadir/$configname/Include_Patterns ] ; then
		cat $datadir/$oldconfig/Include_Patterns >$datadir/$configname/Include_Patterns
	fi
	if [ ! -f $datadir/$configname/Exclude_Patterns ] ; then
		cat $datadir/$oldconfig/Exclude_Patterns >$datadir/$configname/Exclude_Patterns
	fi
}

beep()
{
	echo -e "\a"
}

ErrorBox()
{
	if [ $scheduled = YES ]
	then
		if [ -e $TMP/$MSGFILE ] ; then
			echo -e "$*" >>$TMP/$MSGFILE
		else
			echo -e "$*" >&2
		fi
	else
		echo -e "$*" >$TMP/$TMPFILE
		lines=`wc -l <$TMP/$TMPFILE`
		lines=`expr $lines + 7`
		beep
		$DIALOG   --title " Error " --msgbox "\n$*" $lines 60 
	fi
	if [ "$lines" = "8" ]
	then
		report "ERROR: $*"
	else
		report "ERROR MESSAGE (start)::"
		report "$*"
		report "ERROR MESSAGE (end)"
	fi
}

InfoBox()
{
	if [ "$1" = "-n" ]
	then
		noreport=Y
		shift
	else
		noreport=N
	fi
	if [ $scheduled =  YES ]
	then
		if [ $noreport = Y ] ; then return ; fi
		if [ -e $TMP/$MSGFILE ] ; then
			echo -e $* >>$TMP/$MSGFILE
		else
			echo -e $* >&2
		fi
	else
		echo -e "$*" >$TMP/$TMPFILE
		lines=`wc -l <$TMP/$TMPFILE`
		lines=`expr $lines + 4`
		$DIALOG   --title " Status " --infobox "\n$*" $lines 40
	fi
	if [ $noreport = Y ] ; then return ; fi
	if [ "$lines" = "5" ]
	then
		report "$*"
	else
		report "-----------------"
		report "$*"
		report "-----------------"
	fi
}


MsgBox()
{
	if [ $scheduled =  YES ]
	then
		if [ -e $TMP/$MSGFILE ] ; then
			echo -e $* >>$TMP/$MSGFILE
		else
			echo -e $* >&2
		fi
	else
		echo -e "$*" >$TMP/$TMPFILE
		lines=`wc -l <$TMP/$TMPFILE`
		lines=`expr $lines + 7`
		$DIALOG   --title " Status " --msgbox "\n$*" $lines 40
	fi
	if [ "$lines" = "5" ]
	then
		report "Info-Msg: $*"
	else
		report "Info-Msg:"
		report "$*"
		report "Info-Msg-End"
	fi
}

insert_media()
{
	if [ "$device_type" != "FILE" ]
	then
		$DIALOG   --title " Status " --yesno \
"Please insert backup medium in
\"$device\".

Ok to continue?" 9 36
	return $?
	fi
}

check_compress_success()
{
	ret="$?"
	case	$compression in 
		NONE)	
			return $ret
			;;
		GZIP|COMPRESS)	
			if [ $ret = 0 -o $ret = 2 ] ; then
				return 0
			else
				return 1
			fi
			;;
		PGP)
			return $ret
			;;
	esac
}

#
#	sets the variable $findargs so that only those files get listed
#	that match the selection criteria. ALL FILENAMES RELATIVE TO 
#	".", that is: all filenames get compared against "./pattern"
#	To use this properly, cd to the parent directory and do a
#	echo find . $findargs >file 
#	. file
#
build_find_arguments()
{
	last_command=build_find_arguments

	findargs="$FIND_STD_OPTS -true"
	opt1=
	opt2=
	opt3=
	
	datestamp=`cat "$datadir/$configname/DateStamp"`

	if [ $findxpertmode = YES ]
	then
		findargs="$findargs $findxpertflags"
	else
		# prepare list of files to exclude for tar(1):
		echo $MOUNTPOINT>$TMP/$EXCLUDE #don't backup mountpoint
		for d in `$GREP -v "^#" $datadir/$configname/Exclude_Files | $SED "1,$ s/\\\#/#/g"`
		do
			echo $d >>$TMP/$EXCLUDE
			if [ -d $parent/$d ]
			then	# for dirs: exclude all subdirs as well
				echo $d/\* >>$TMP/$EXCLUDE
			fi
		done
		if [ "$use_patterns" = "YES" ]
		then
			$GREP -v "^#" $datadir/$configname/Exclude_Patterns | \
			$SED "1,$ s/\\\#/#/g" >>$TMP/$EXCLUDE
		fi
	
		if [ "$incremental" = "YES" ] ; then
			touch -d "$datestamp" $TMP/$DATEFILE
			findargs="$findargs -cnewer $TMP/$DATEFILE"
		fi

		if [ "$use_patterns" = "YES" ]
		then
			if [ $specifiedfilesonly = YES  ]
			then
			$GREP -v "^#"  $datadir/$configname/Include_Patterns \
			| $SED '1,$ s/\\\#/#/g
				1,$ s/^\(.*\)$/-or -path "\1"/g' \
			| tr "\n\r" "  " \
			| $SED '1,$ s/^-or //g
				1,$ s/^\(.*\)$/\\( \1 \\)/g' >$TMP/$TMPFILE
			else
			$GREP -v "^#"  $datadir/$configname/Include_Patterns \
			| $SED '1,$ s/\\\#/#/g
				1,$ s/^\(.*\)$/-or -path ".\/\1"/g' \
			| tr "\n\r" "  " \
			| $SED '1,$ s/^-or //g
				1,$ s/^\(.*\)$/\\( \1 \\)/g' >$TMP/$TMPFILE
			fi
			opt1=`cat $TMP/$TMPFILE`
			if [ "$opt1" != "" ]
			then
				findargs="$findargs -a $opt1 "
			fi
			

			if [ $specifiedfilesonly = YES  ]
			then
			  $GREP -v "^#" $datadir/$configname/Exclude_Patterns \
			| $SED '1,$ s/\\\#/#/g
				1,$ s/^\(.*\)$/-or -path "\1"/g' \
			| tr "\n\r" "  " \
			| $SED '1,$ s/^-or //g
				1,$ s/^\(.*\)$/\\( \1 \\)/g' >$TMP/$TMPFILE
			else
			  $GREP -v "^#" $datadir/$configname/Exclude_Patterns \
			| $SED '1,$ s/\\\#/#/g
				1,$ s/^\(.*\)$/-or -path ".\/\1"/g' \
			| tr "\n\r" "  " \
			| $SED '1,$ s/^-or //g
				1,$ s/^\(.*\)$/\\( \1 \\)/g' >$TMP/$TMPFILE
			fi

			opt2=`cat $TMP/$TMPFILE`
			if [ "$opt2" != "" ]
			then
				opt2=" \( -not $opt2 \) "
				findargs="$findargs -a $opt2 "
			fi
		fi
		
		# always:
		
		if [ $specifiedfilesonly = YES  ]
		then
		cat $TMP/$EXCLUDE \
		| $SED '1,$ s/^\(.*\)$/-or -path "\1"/g' \
		| tr "\n\r" "  " \
		| $SED '1,$ s/^-or //g
			1,$ s/^\(.*\)$/\\( \1 \\)/g' >$TMP/$TMPFILE
		else
		cat $TMP/$EXCLUDE \
		| $SED '1,$ s/^\(.*\)$/-or -path ".\/\1"/g' \
		| tr "\n\r" "  " \
		| $SED '1,$ s/^-or //g
			1,$ s/^\(.*\)$/\\( \1 \\)/g' >$TMP/$TMPFILE
		fi
		opt3=`cat $TMP/$TMPFILE`
		if [ "$opt3" != "" ]
		then
			opt3=" \( -not $opt3 \) "
			findargs="$findargs -a $opt3 "
		fi
	fi
		

	if [ $local = YES ] ; then
		findargs="$findargs -xdev"
	fi
	if [ $directoriesonly = YES ] ; then
		findargs="$findargs -type d"
	fi
}

create_dir_list()
{
	last_command=create_dir_list

	echo >$TMP/$DIRLISTFILE
	echo >$TMP/$TARLISTFILE

	build_find_arguments

	cd $parent
	files=.
	set -f
	echo find $files $findargs -fprintf $TMP/$DIRLISTFILE \"%p %s %u %g %Tc %m\\n\" -fprintf $TMP/$TARLISTFILE \"%p\\n\" >$TMP/$FINDFILE
	. $TMP/$FINDFILE 2>/dev/null
	set +f
}

#
# creates the file $TMP/$DIRLISTFILE where it keeps the names and information
# about all the files to back up
#
build_dir_list()
{
	if [ $specifiedfilesonly = YES  ]
	then
		( cat $datadir/$configname/Include_Files | \
		$GREP -v "^#" | \
		$SED "1,$ s/\\\#/#/g" >$TMP/$DIRLISTFILE ) 2>/dev/null
		echo >$TMP/$TARLISTFILE
		build_find_arguments

		cd $parent
		files=`cat $TMP/$DIRLISTFILE`
		set -f
		echo find $files $findargs -fprintf $TMP/$DIRLISTFILE \"%p %s %u %g %Tc %m\\n\" -fprintf $TMP/$TARLISTFILE \"%p\\n\" >$TMP/$FINDFILE
		. $TMP/$FINDFILE 2>/dev/null
		set +f
		
		return
	else
		create_dir_list
	fi
}

set_compress()
{
	touch $TMP/$PHRASEFILE
	pcompress=
	puncompress=
	tarcompress=
	uncompress=
	compress_program=
	case $compression in
		NONE)	compress=" cat " 
			uncompress=" cat "
			pcompress=" | cat "
			puncompress=" | cat "
			return
			;;
		GZIP)	compress=" gzip -c "$compress_options" " 
			compress_program="gzip"
			cat >$TMP/$COMPRESSFILE <<_EOF_
#!/bin/sh
PATH="$PATH"
gzip $compress_options \$* 2>/dev/null
_EOF_
			chmod +x $TMP/$COMPRESSFILE
			tarcompress="--block-compress --use-compress-prog $TMP/$COMPRESSFILE"
			uncompress=" gzip -c "$decompress_options" "
			pcompress=" | $compress "
			puncompress=" | $uncompress "
			;;
			
		COMPRESS) compress=" compress "$compress_options" "
			compress_program="compress"
		cat >$TMP/$COMPRESSFILE <<_EOF_
#!/bin/sh
PATH="$PATH"
compress $compress_options \$* 2>/dev/null
_EOF_
			chmod +x $TMP/$COMPRESSFILE
			tarcompress="--block-compress --use-compress-prog $TMP/$COMPRESSFILE"
			uncompress=" compress "$decompress_options" "
			pcompress=" | $compress "
			puncompress=" | $decompress "
			;;
			
		PGP) 	compress=" pgp -ef $pgpkey "$compress_options" 2>/dev/null"
			compress_program="pgp"
			cat >$TMP/$COMPRESSFILE <<_EOF_
#!/bin/sh
cd $TMP 2>/dev/null
if [ "\$1" = "-d" ]
then
	opt=-d
	pgp \$opt -f 2>/dev/tty 3<$TMP/$PHRASEFILE
else
	opt=-e
	pgp \$opt -f $pgpkey  2>/dev/null
fi
_EOF_
			chmod +x $TMP/$COMPRESSFILE
			tarcompress="--block-compress --use-compress-prog $TMP/$COMPRESSFILE"
			uncompress=" pgp -df $pgpkey $decompress_options "
			pcompress=" | $compress "
			puncompress=" | $uncompress "
	esac
}	

check_error()
{
	errno="$?"
	check_error_cmd="$last_command"
	last_command=

	if [ $errno != 0 ]	# error!
	then
		
		if [ "$1" = "-dev" ]
		then
			echo "Device Error: it seems that an operation on the device
$device has failed.
" >$TMP/$TMPFILE
			cat $TMP/$ERRFILE >>$TMP/$TMPFILE 2>/dev/null
			cat $TMP/$TMPFILE >$TMP/$ERRFILE 
		fi
		if [ "$check_error_cmd" != "" ]
		then
			echo "This command has failed:" >>$TMP/$TMPFILE
			echo "$check_error_cmd" >>$TMP/$TMPFILE
		fi
		
		lines=`wc -l <$TMP/$ERRFILE`
		if [ $lines -lt 2 ]
		then 
			echo "No Error message generated!" >$TMP/$ERRFILE
			lines=2; 
		fi
		lines=`expr 8 + $lines`
		
		if [ $scheduled = YES ]
		then
			if [ -e $TMP/$MSGFILE ] ; then
				cat $TMP/$ERRFILE >>$TMP/$MSGFILE
			else
				$TMP/$ERRFILE >&2
			fi
			return $errno
		fi

		if [ "$last_command" != "" ]
		then
			echo "The command failed was:" >>$TMP/$ERRFILE
			echo "$check_error_cmd" >>$TMP/$ERRFILE
		fi
		
		$DIALOG --title " Program Execution Error " --msgbox "
The last command returned a non-zero result ($errno).
This indicates an error during its execution. The
error message generated was:

`cat $TMP/$ERRFILE`" $lines 65
		echo >$TMP/$ERRFILE
	fi
	return $errno
}

#
# afio 2.4.1 has a nasty bug, restoring the gzip program prevents it from
# any further restoration of compressed files. As it does not notice this,
# it will happily truncate all these files and report "Backup successful" :-)
#
# Therefore: save all critical programs in $TMP/$BINDIR and set the path there.
# This is not perfectly save, but who would ever try to restore the programs
# to exactly that path???

afio_bug_save_programs()	# must be called before doing a restore
{
	if [ ! -d $TMP/$BINDIR ]; then mkdir $TMP/$BINDIR; fi
	check_error
	# try to continue:
	afio_bug_save_PATH="$PATH"
	if [ ! -d $TMP/$BINDIR ]
	then
		mkdir $TMP/$BINDIR 2>$STDERR || check_error
	fi
	PATH="$TMP/$BINDIR:$PATH"
	for i in $AFIO_PROGRAMS
	do
		place=`whereis $i | $SED "1,$ s/^$i: \(.*\)$/\1/g"`
		if [ $? = 0 ]
		then
			cp $place $TMP/$BINDIR
		fi
	done
}
afio_bug_restore_path()
{
	PATH="$afio_bug_save_PATH"
}

#
# save important options that are going to be manipulated during
# operation
#
save_options()
{
	old_compression="$compression"
	old_use_multibuf="$use_multibuf"
	old_multibuf_nblocks="$multibuf_nblocks"
	old_multibuf_blksize="$multibuf_blksize"
	old_multibuf_seq_info="$multibuf_seq_info"
	old_ddbufsize="$ddbufsize"
}
#
# restore previously saved options
#
restore_options()
{
	compression="$old_compression"
	use_multibuf="$old_use_multibuf"
	multibuf_nblocks="$old_multibuf_nblocks"
	multibuf_blksize="$old_multibuf_blksize"
	multibuf_seq_info="$old_multibuf_seq_info"
	ddbufsize="$old_ddbufsize"
}


report()	# write to report file
{
	if [ "$CreateReport" != "YES" ]
	then
		return
	fi
	if [ "$1" = "--init" ]
	then
	tmp=`date`
	if [ "$incremental" = "YES" ]
	then
		type="incremental"
	else
		type="full"
	fi
	cat <<_EOF_ >$TMP/$REPORTFILE
KBackup Report File (KBackup Version $VERSION)
=================================================================
               KBackup is Copyright (C) 1996 by Karsten Ballueder

Using configuration:        $configname
Creation date:              $tmp
Backup type:                $type
Parent directory of backup: $parent
Compression mode:           $compression
_EOF_
	else
		echo "$*" >>$TMP/$REPORTFILE
	fi
}

auto_rewind()	# rewind automatically if wanted
{
	if [ "$device_type" = "TAPE" ] 
	then
		if [ "$AutoRewind" = "YES" ]
		then
			InfoBox "Rewinding tape..."
			last_command="mt rewind"
			my_mt -f "$device" rewind 2>$STDERR
			mysleep 
			my_mt -f "$device" rewind 2>$STDERR
			check_error -dev
		else
			MsgBox \
"Please note that the tape does not
get rewound automatically. If you 
have written to or read from it, it
might not be positioned at the be-
ginning any longer.
To rewind it, go into the Actions 
Menu."
		fi
	fi
}

uncompress_logfile()
{
	InfoBox "Uncompressing logfile..." 
	old_compression="$compression"
	logfile="$1"
	filetype=`file $logfile`
	if echo "$filetype" | $FGREP gzip >/dev/null ; then
		compression=GZIP
	else
		if echo "$filetype" | $FGREP compress >/dev/null ; then
			compression=COMPRESS
		else
			if echo "$filetype" | $FGREP text >/dev/null ; then
				compression=NONE
			else
				setco=`$FGREP "compression" <$datadir/$dir/Config`
				echo $setco >$TMP/$TMPFILE
				. $TMP/$TMPFILE
			fi
		fi
	fi
	set_compress
	cat < "$logfile" | $uncompress >$TMP/$TMPFILE
}
