#
# usage:        pdextrct.sh
#
# abstract:     This Bourne Shell script extracts product files from
#		tape and prepare them for installation
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Screen format
#
#-------------------------------------------------------------------------
#   T3. Extract Files from Tape and Prepare Them for Installation
#
# Extracting product files from tape. This may take a while . . . (please wait)
#
# Prepare product files:
#
#    matlab.common  matlab.sun4   matlab.hp700   matlab.alpha
#    . . . .
#
# Continue? ([y]/n)
#
#-------------------------------------------------------------------------
#
# Copyright (c) 1994-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.11 $  $Date: 1998/12/24 17:31:41 $
#----------------------------------------------------------------------------
#
#                               Extract product files
#                               ---------------------
#
# Output the screen
#
    . $dir/$clearsc_sh
#--------------------------------------------------------------------------
echo '    T3. Extract Files from Tape and Prepare Them for Installation'
echo ''
#
# Extract product files
#	
#--------------------------------------------------------------------------
#
    mesg='Extracting product files from tape. This may take a while . . . (please wait) '
    . $dir/$echon_sh
#
    if [ ! -d $TAPE_DIR/tpd ]; then
	mkdir $TAPE_DIR/tpd
    fi
    (cd $TAPE_DIR/tpd
     tar -xf $ARCHIVE_DEVICEN $PIECES 2>$temp_file
     if [ $? -ne 0 ]; then
            nc=`wc -c $temp_file | awk '{print $1}'`
#-----------------------------------------------------------------------------
echo ''
echo '---------------------------------------------------------------------'
echo "Error: 'tar -cf $ARCHIVE_DEVICEN ...' command failed . . ."
echo ''
            if [ "$nc" = "0" ]; then
#-----------------------------------------------------------------------------
echo '. . . no message . . .'
#-----------------------------------------------------------------------------
            else
                cat $temp_file
            fi
#-----------------------------------------------------------------------------
echo '---------------------------------------------------------------------'
echo ''
	    . $dir/$abort_sh
    else
	nc=`wc -c $temp_file | awk '{print $1}'`
        if [ "$nc" != "0" ]; then
#-----------------------------------------------------------------------------
echo ''
echo '---------------------------------------------------------------------'
echo "Error: 'tar -cf $ARCHIVE_DEVICE ...' . . ."
echo ''
	    cat $temp_file | awk '{print "" $0}'
#-----------------------------------------------------------------------------
echo '---------------------------------------------------------------------'
#-----------------------------------------------------------------------------
	else
#-----------------------------------------------------------------------------
echo ''
#-----------------------------------------------------------------------------
	fi
    fi
   ) 
#
# Prepare product files - max 3 per line
#
#--------------------------------------------------------------------------
    echo ''
    echo 'Prepare product files:'
    echo ''
#--------------------------------------------------------------------------
    mesg='    ' 
    . $dir/$echon_sh
    iperline=0
    error_last=0
    for file in $PIECES
    do
        if [ "$iperline" = "3" ]; then
	    if [ "$error_last" = "1" ]; then
		error_last=0
	    else
#--------------------------------------------------------------------------
    echo ''
#--------------------------------------------------------------------------
	    fi
	    mesg='    ' 
    	    . $dir/$echon_sh
	    iperline=1
	else
	    iperline=`expr $iperline + 1`
	fi
        piece_name=`echo $file | cat - $MAPS_DIR/piece.map | awk '
#--------------------------------------------------------------------------
    NR == 1 { file = $1; next }
    NF == 0 { next }
	    { if ($1 == file) {
	          print $2
		  exit
	      }
	      next }'`
#--------------------------------------------------------------------------
#
	(cd $MATLAB
	 tar -xf $TAPE_DIR/tpd/$file 2>$temp_file
         if [ $? -ne 0 ]; then
	     mesg="$piece_name"'*    '
    	     . $dir/$echon_sh
             nc=`wc -c $temp_file | awk '{print $1}'`
#-----------------------------------------------------------------------------
echo ''
echo '---------------------------------------------------------------------'
echo "Error: 'tar -cf "'$TAPE_DIR'"/tpd/$file'  command failed . . ."
echo ''
             if [ "$nc" = "0" ]; then
#-----------------------------------------------------------------------------
echo '. . . no message . . .'
#-----------------------------------------------------------------------------
             else
		 cat $temp_file | awk '{print "" $0}'
             fi
#-----------------------------------------------------------------------------
echo '---------------------------------------------------------------------'
#-----------------------------------------------------------------------------
	 else
	     mesg="$piece_name   "
    	     . $dir/$echon_sh
	     nc=`wc -c $temp_file | awk '{print $1}'`
             if [ "$nc" != "0" ]; then
#-----------------------------------------------------------------------------
echo ''
echo '---------------------------------------------------------------------'
echo "Error: 'tar -cf "'$TAPE_DIR'"/tpd/$file' . . ."
echo ''
		 cat $temp_file | awk '{print "" $0}'
#-----------------------------------------------------------------------------
echo '---------------------------------------------------------------------'
#-----------------------------------------------------------------------------
	     fi
	 fi
	)
        nc=`wc -c $temp_file | awk '{print $1}'`
        if [ "$nc" != "0" ]; then
	    iperline=3
	    error_last=1
        fi
	rm -f $TAPE_DIR/tpd/$file > /dev/null 2>&1
    done
#-----------------------------------------------------------------------------
    echo ''
    echo ''
#-----------------------------------------------------------------------------
    . $dir/$cont_sh
#
