#! /bin/sh
# PCP QA Test No. 417
# Exercise tools with a corrupted archive
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

rm -f $seq.out
if [ $PCP_VER -lt 3600 ]
then
    # V1 archives supported
    arch=src/bigace
    case $PCP_PLATFORM
    in
	irix|linux)
	    ln $seq.$PCP_PLATFORM.1 $seq.out || exit 1
	    ;;
	darwin|solaris)
	    # same output for Mac OS X and OpenSolaris
	    ln $seq.linux.1 $seq.out || exit 1
	    ;;
	*)
	    _notrun "Need qualified output for $PCP_PLATFORM"
	    ;;
    esac
else
    # no support for V1 archives
    arch=src/bigace_v2
    case $PCP_PLATFORM
    in
	irix|linux)
	    if [ $PCP_VER -lt 3604 ]
	    then
		ln $seq.$PCP_PLATFORM.2 $seq.out || exit 1
	    else
		ln $seq.$PCP_PLATFORM.3 $seq.out || exit 1
	    fi
	    ;;
	darwin|solaris)
	    # same output for Mac OS X and OpenSolaris
	    if [ $PCP_VER -lt 3604 ]
	    then
		ln $seq.linux.2 $seq.out || exit 1
	    else
		ln $seq.linux.3 $seq.out || exit 1
	    fi
	    ;;
	*)
	    echo "Arrgh ... need qualified output for $PCP_PLATFORM"
	    exit 1
	    ;;
    esac
fi

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp; exit \$status" 0 1 2 3 15

_check_core()
{
    if [ "`echo core*`" != "core*" ]
    then
	$PCP_ECHO_PROG $PCP_ECHO_N "Dumped core! (saved in $here as""$PCP_ECHO_C"
	for c in core*
	do
	    $sudo mv $c $here/$seq.$c
	    $PCP_ECHO_PROG $PCP_ECHO_N " $seq.$c""$PCP_ECHO_C"
	done
	echo ")"
    fi
}

rm -rf $tmp
mkdir $tmp
cd $tmp

# make the corrupted archive
#
cp $here/$arch.meta bad.meta
dd ibs=1 count=512 if=$here/$arch.index of=bad.index 2>/dev/null
dd ibs=1 count=512 if=$here/$arch.index >>bad.index 2>/dev/null
dd if=$here/$arch.0 ibs=1 count=2052 of=bad.0 2>/dev/null
dd if=/dev/zero ibs=1 count=1024 >>bad.0 2>/dev/null

# deal with an annoying libc error ... using TZ=:Australia/Melbourne
# causes the -z option to report the wrong time, at least for some
# times of the year
#
TZ=EST-10
export TZ

# real QA test starts here

echo
echo "=== pminfo ==="
pminfo -n $here/src/bigace.pmns -a bad >$tmp.out 2>$tmp.err
cat $tmp.err $tmp.out
_check_core

echo
echo "=== pminfo -f irix ==="
pminfo -n $here/src/bigace.pmns -a bad -f irix
_check_core

echo
echo "=== pmdumplog -t ==="
pmdumplog -n $here/src/bigace.pmns -z -t bad
_check_core

echo
echo "=== pmdumplog -t, no index ==="
mv bad.index save.index
pmdumplog -n $here/src/bigace.pmns -z -t bad
mv save.index bad.index
_check_core

echo
echo "=== pmdumplog -l ==="
pmdumplog -n $here/src/bigace.pmns -z -l bad
_check_core

echo
echo "=== pmdumplog -L ==="
pmdumplog -n $here/src/bigace.pmns -z -L bad
_check_core

echo
echo "=== pmdumplog -m ==="
pmdumplog -n $here/src/bigace.pmns -z -m bad >$tmp.out 2>$tmp.err
cat $tmp.err $tmp.out
_check_core

echo
echo "=== pmlogsummary ==="
pmlogsummary -n $here/src/bigace.pmns bad >$tmp.out 2>$tmp.err
cat $tmp.err $tmp.out | sed -e 's/^\*//g'
_check_core

# success, all done
status=0
exit
