#!/bin/sh
#
# Show me the QA tests that did not pass
#
# Copyright (c) 1997-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

# generic initialization
__scriptname=show-me
. ./common.rc

if [ -n "$PCPQA_DIFF" ]
then
    DIFF="$PCPQA_DIFF"
else
    if which gdiff >/dev/null 2>&1
    then
	DIFF=gdiff
    else
	if which meld >/dev/null 2>&1
	then
	    DIFF=meld
	else
	    if which xxdiff >/dev/null 2>&1
	    then
		DIFF=xxdiff
	    else
		DIFF="diff -c"
	    fi
	fi
    fi
fi

tmp=/tmp/$$
trap "cd /; rm -rf $tmp $tmp.*; exit" 0 1 2 3 15
rm -rf $tmp
mkdir $tmp

nodiff=false
local=true
where=false
while getopts g:lnwx:h c
do
   case $c in

   g)	    # include group
	    rm -f $tmp.tmp
	    touch $tmp.empty
	    touch $tmp.from-g
	    sed -n <group \
		-e 's/\([0-9]\):retired /\1 /' \
		-e 's/\([0-9]\):reserved /\1 /' \
		-e 's/$/ /' \
		-e "/^[0-9][0-9]*.* $OPTARG /"'{
s/ .*//p
}' \
	    | while read seq
	    do
		rm -f $tmp.empty
		[ -f "$seq.out.bad" ] && echo "$seq" >>$tmp.tmp
	    done
	    if [ -s $tmp.tmp ]
	    then
		LC_COLLATE=POSIX sort -u -o $tmp.from-g $tmp.from-g $tmp.tmp
	    elif [ -f $tmp.empty ]
	    then
		echo "Group \"$OPTARG\" is empty or not defined; ignored" >&2
	    fi
	    ;;

   l)	    # line-mode diff
	    DIFF='diff -c'
	    ;;

   n)	    # seq #'s, no diffs
	    nodiff=true
	    ;;

   w)       # report dir
	    where=true
	    wdir=`pwd | sed -e "s;.*/;;"`
   	    ;;

   x)	    # exclude group
	    rm -f $tmp.tmp
	    touch $tmp.empty
	    touch $tmp.from-x
	    sed -n <group \
		-e 's/\([0-9]\):retired /\1 /' \
		-e 's/\([0-9]\):reserved /\1 /' \
		-e 's/$/ /' \
		-e "/^[0-9][0-9]*.* $OPTARG /"'{
s/ .*//p
}' \
	    | while read seq
	    do
		rm -f $tmp.empty
		[ -f "$seq.out.bad" ] && echo "$seq" >>$tmp.tmp
	    done
	    if [ -s $tmp.tmp ]
	    then
		touch $tmp.from-x
		LC_COLLATE=POSIX sort -u -o $tmp.from-x $tmp.from-x $tmp.tmp
	    elif [ -f $tmp.empty ]
	    then
		echo "Group \"$OPTARG\" is empty or not defined; ignored" >&2
	    fi
	    ;;

   h)      # usage
	    echo "Usage: $0 [options] [seq[ seq...]]"'

Options:
    -l		line mode diff [xdiff]
    -n		report failing tests, no diffs
    -g group	include tests from this group (additive for multiple -g options)
    -w		report directory
    -x group	exclude tests from this group (additive for multiple -x options)

Default (no -g) is to include all tests with a .out.bad file.'
	    exit
	    ;;
   esac
done
shift `expr $OPTIND - 1`

if [ ! -f $tmp.from-g ]
then
    if [ $# -eq 0 ]
    then
	# no seq ... on command line, consider all failures
	#
	ls -1 *.out.bad 2>/dev/null \
	| sed -e 's/\.out\.bad$//' \
	| LC_COLLATE=POSIX sort >$tmp.list
    else
	touch $tmp.list
    fi
else
    mv $tmp.from-g $tmp.list
fi

if [ $# -gt 0 ]
then
    # add seq ... from command line to list of candidate tests
    #
    rm -f $tmp.tmp
    for seq
    do
	echo "$seq" >>$tmp.tmp
    done
    LC_COLLATE=POSIX sort -u -o $tmp.list $tmp.list $tmp.tmp
fi

if [ -f $tmp.from-x ]
then
    LC_COLLATE=POSIX comm -23 $tmp.list $tmp.from-x >$tmp.tmp
    mv $tmp.tmp $tmp.list
fi

if [ ! -s $tmp.list ]
then
    echo "No failing tests!" >&2
    exit
fi

for id in `sort -n $tmp.list`
do
    case "$id"
    in
	[0-9])
		id=00$id
		;;
	[0-9][0-9])
		id=0$id
		;;
    esac

    if $nodiff
    then
	$PCP_ECHO_PROG $PCP_ECHO_N "$id ""$PCP_ECHO_C"
	continue
    fi

    if $where
    then
	$PCP_ECHO_PROG $PCP_ECHO_N "$wdir/$id: ""$PCP_ECHO_C"
    else
	$PCP_ECHO_PROG $PCP_ECHO_N "$id: ""$PCP_ECHO_C"
    fi
    ref=''
    if [ ! -f $id.out ]
    then
	# if not here, may be CI results, not QA Farm results,
	# so try for reference version in well-known place
	#
	if [ -f $HOME/src/pcp/qa/$id.out ]
	then
	    ref=$HOME/src/pcp/qa/
	    $PCP_ECHO_PROG $PCP_ECHO_N  "Warning: using $id.out from $ref ""$PCP_ECHO_C"
	else
	    echo  "Could not find $id.out in `pwd`"
	    cd $here
	    continue
	fi
    else
	# hack for kenj ...
	#
	case `pwd`
	in
	    */Logs/by-vm/*/qa)
	    	badref=`pwd`/
		;;
	esac
    fi
    if [ ! -f $id.out.bad ]
    then
	echo  "Could not find $id.out.bad in `pwd`"
    else
	if cmp $ref$id.out $badref$id.out.bad >/dev/null 2>&1
	then
	    echo " .out.bad and out are the same"
	else
	    echo
	    if [ "$DIFF" = xxdiff ]
	    then
		$DIFF $ref$id.out $badref$id.out.bad >/dev/null 2>&1
	    else
		$DIFF $ref$id.out $badref$id.out.bad
	    fi
	fi
    fi
    cd $here
done

$nodiff && echo
