#!/bin/bash

# This program is part of the tbackup package,
# (c) 1993,1994,1995,1998 Koen Holtman.
#
# The tbackup package is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
# No warranty. See the GNU General Public License for more details.


export bupbin=/usr/lib/tbackup/bin
export bupdir=/usr/lib/tbackup
export PATH=$bupbin\:$PATH
export ttytitle=verify

source tinit

#get method names and parameters

source getverpars

source makelogtty

source handlepars

echo
echo "Verify in progress..."
echo

echo " Note: if a low level media read error occurs, tverify may crash or hang"
echo " in stead of reporting the error."
echo " If tverify never prints a SUCCESS or ERROR message, you can assume that"
echo " the verify failed due to a bad archive."
echo

# make named pipe

mkfifo -m 700 archive

# execute verify.

# readmethod is supposed to interact with the user.

# verifymethod should start a background task that deletes the pipe
# `archive' when finished.
# This method should do no i/o on the user tty.

#check for verify errors.
mkfifo vererror
gawk -f - <<-'blex' vererror >logtty &
$0 == "afio: \"TBACKUP%.gz\": No such file or directory" { next; }
$0 == "afio: \"TBACKUP%.mgz\": No such file or directory" { next; }
$0 == "TBACKUP%.gz: does not exist" { next; }
$0 == "TBACKUP%.mgz: does not exist" { next; }
{ err++; print; }
END { if(err) print err >"vererrcount"; }
blex
errpid=$!

cd /tmp/tbup
source $verifymethod\.vx
cd /tmp/tbup
source $readmethod\.rx

# wait for `archive' to disappear.
while [ -e archive ]; do sleep 1; done

#wait for error counter.
wait $errpid 2>/dev/null
if [ -e vererrcount ]; then
 printf "\007"
 e=`cat vererrcount`
 echo "**ERROR**: There were $e verify errors, the archive contents are bad"
 echo "or differ from the filesystem contents."
 echo "See the $ttytitle log for detailed archiver error message(s)."
 echo
 echo "Press enter to continue."
 if [ $batchmode = n ]; then enter; fi
else
 echo "**SUCCESS**: No verify errors."
fi
echo

#finalise methods
#what such methods would want to do is beyond me, we put it in for symmetry.

if [ -f $bupbin/$readmethod\.rf ]; then
 cd /tmp/tbup
 source $readmethod\.rf
fi
if [ -f $bupbin/$verifymethod\.lf ]; then
 cd /tmp/tbup
 source $verifymethod\.lf
fi

source texit

