#!/bin/sh
#

prefix=/usr/local
exec_prefix=${prefix}
sbindir=${exec_prefix}/sbin
libexecdir=/usr/local/libexec/amanda

PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
export PATH

USE_VERSION_SUFFIXES="no"
if [ "$USE_VERSION_SUFFIXES" = "yes" ]; then
        SUF="-2.4.5p1"
else
        SUF=
fi

getparm() {
        $AMGETCONF $CONFIG $1 2>/dev/null | grep -v BUGGY
}

CONFIG=$1
libexecdir=$libexecdir  
sbindir=$sbindir
AMGETCONF=$sbindir/amgetconf$SUF
AMVERIFY=$sbindir/amverify$SUF
LOGDIR=`getparm logdir`
AMDUMPLOG=${LOGDIR}/amdump.1
AMFLUSHLOG=${LOGDIR}/amflush.1
if [ -f $AMDUMPLOG ]; then
  if [ -f $AMFLUSHLOG ]; then
    if [ $AMDUMPLOG -nt $AMFLUSHLOG ]; then
      AMLOG=$AMDUMPLOG
    else
      AMLOG=$AMFLUSHLOG
    fi
  else
    AMLOG=$AMDUMPLOG
  fi
else
  if [ -f $AMFLUSHLOG ]; then
    AMLOG=$AMFLUSHLOG
  else
    echo "Nothing to verify"
    exit 1;
  fi
fi


FIRST_SLOT=`grep "taper: slot" $AMLOG | fgrep 'exact label match
new tape
first labelstr match' | sed 1q | sed 's/://g' | awk '{print $3}'`
if [ X"$FIRST_SLOT" = X"" ]; then
  FIRST_SLOT='-1'
fi

NBTAPES=`grep -c "taper: wrote label " $AMLOG`

if [ X"$NBTAPES" != X"0" ]; then
  $AMVERIFY $CONFIG $FIRST_SLOT $NBTAPES
else
  echo "Nothing to verify"
fi
