#!/bin/sh
#
# vtar usual_tar_options
#
#
# Interface to tar with:  
#    1) Additional level of error-checking
#	grep through stderr for "serious" errors to set exit status 
#    2) Modified pipe-reading behavior
#	When tarOpts include 'B' option, continue reading 'til EOF marker.
#	    This causes the vtar process to wait for the writing process to
#	    finish before exiting. 
#	    Note that standard tar does NOT normally read the EOF marker.
#    3) The 'V' option (as in tar tV) displays listing in two columns of
#	the form: [dl-][r-][w-][x-][r-][w-][x-][r-][w-][x-] filename[/]
#
#
#############################################################################
#
#	Copyright (C) Cadence Design Systems, Inc. All rights
#	reserved.  Unpublished -- rights reserved under the
#	copyright laws of the United States of America.
#
#			RESTRICTED RIGHTS LEGEND
#	Use, duplication, or disclosure by the Government is subject
#	to restrictions as set forth in subparagraph (c)(l)(ii) of 
#	the Rights in Technical Data and Computer Software clause 
#	at DFARS 52.227-7013.
#
#			Cadence Design Systems, Inc.
#			555 River Oaks Parkway
#			San Jose, CA 95134	USA
#
#############################################################################
BOURNE=/bin/sh
XTERMPATH=/usr/bin/X11
XTERMBIN=xterm
XTERMOPTS="-T 'SoftLoad 6.0 Console' -fn 9x15  -sl 200 -g 80x40+0+0 -bg \#e0e0e0 -fg \#800000 -sb -e ${BOURNE} softload_GUI"
sysPATH=/bin:/usr/bin:/usr/ucb:$XTERMPATH

jsr=''


FltrVerbose() {		#put into simple 'perms + name' format
	sed 's@^\(..........\).* 19[89][0-9] \([^ ]*\).*@\1 \2@'
}


argv0=$0
tarOpts=$1 ;	shift
tarArgs="$*"

pipeRead=false
case $tarOpts in
    *B*) 
	pipeRead=true;;
esac

filterOut=false
case $tarOpts in
    *t*V*)
	tarOpts=`echo $tarOpts | tr V v`	#replace V option with v
	filterOut=true;;
esac

case $tarOpts in
    *c*) tarOpts=`echo $tarOpts | sed 's@c@cd@'`;;
esac



test -z "$loadLog" && loadLog=/dev/null;      export loadLog

baderrs=/tmp/baderrs$$
tarlog=/tmp/tarlog$$
rm -f $baderrs $tarlog

#These are critical tar errors. If they are found on stderr exit nonzero
cat - >$baderrs <<TAR_ERR_STRINGS
cannot create temporary file
invalid blocksize
The block size is 0 bytes
tape blocksize error
file name too long
missing links to
directory checksum error
directory read error
symbolic link too long
Read error on
Symbolic link failed
can't create
Cannot create
HELP - extract write error
tape read error
out of memory, link and directory modtime info lost
ran off end of tape
End of tape
TAR_ERR_STRINGS

cmnd="tar  $tarOpts $tarArgs 2>$tarlog"

$filterOut && cmnd="( $cmnd | $jsr FltrVerbose )"
$pipeRead  && cmnd="$cmnd && cat - >/dev/null"
echo "$cmnd" >> $loadLog

oldUmask=`umask`; umask 000
eval $cmnd
cmndStat=$?
umask $oldUmask

cat $tarlog >> $loadLog
if [ $cmndStat -ne 0 ] || [ `fgrep -f $baderrs $tarlog | wc -l` -ne 0 ]; then
    cat $tarlog	1>&2
    rm -f $tarlog $baderrs
    exit 1
fi
rm -f $tarlog $baderrs
exit 0
