#
# usage:        tapepos.sh
#
# abstract:     This Bourne Shell script positions the tape to the
#		start of the 2nd file on the tape.
#
# note(s):      1. This routine must be called using a . (period)
#
#		2. Screen
#
#-----------------------------------------------------------------------------
#----------first time-----------
#
#     T1. Specify Tape Device and Position Tape to MATLAB Product Files
# 
# The MATLAB product files are located in the second tar file on the
# distribution tape. We need to move the tape to the start of this file.
# 
# Please specify the tape device that was used to read the MATLAB
# distribution tape initially.
#
#----------------
# Be sure that it is a 'no swap' device . . .
#----------------
#
#     [/dev/rst0] >
#
#----------------
#
# Error: /dev/rst0 does not exist. Please specify a valid device . . .
#
#     [/dev/rst0] >
#
#----------------
#
# Looking for analogous 'no rewind' device to use . . .
#
#----------------
# Found it:   /dev/nrst0
#----------------
# Already one . . .
#----------------
#
# Please load the MATLAB distribution tape and press <return>
#----------------
# Cannot find one . . .
#
# Please press <return> to continue or type h for help:
#
#----------------
#
#----------second and later times-----------
#
#     T1. Specify Tape Device and Position Tape to MATLAB Product Files
# 
# The MATLAB product files are located in the second tar file on the
# distribution tape. We need to move the tape to the start of this file.
# 
#----------------
# Please specify your tape device with 'no-rewind' . . .
#----------------
# Please specify your tape device with 'no-rewind' and 'no-swap' . . .
#----------------
#
# Type h at the next prompt for help . . .
#
#     [/dev/nrst0] >
#
#----------------
# Error: /dev/nrst0 does not exist. Please specify a valid device . . .
#
#     [/dev/nrst0] >
#----------------
#
# Please load the MATLAB distribution tape and press <return> "
#
#-----------------------------------------------------------------------------
#
#		3. Help screen (sun4 example)
#
#-----------------------------------------------------------------------------
#
#                         Device Help [sun4]
# ----------------------------------------------------------------------------
#  Required Option:     'no-rewind' device (will not rewind when closed)
#
#  Device Syntax:       /dev/nr[mt|st|ar]#
#
#		        meta-language: [ ] - select one
#			                |  - or
#				        #  - a number
#
#                       n   - no rewind
#                       r   - raw
#                       mt  - magnetic tape
#                       st  - SCSI tape
#                       ar  - Archive 1/4 inch tape drive
#                       #   - device number
#
#  Examples:            /dev/nrmt0  /dev/nrst0  /dev/nrar1
#
#  Man Pages:           type 'man mtio' or 'man st' at the UNIX prompt
# ----------------------------------------------------------------------------
#
#		4. computer types
#
#			sun4     - Sun Sparc (SunOS 4.1.4)
#			hp700    - HP 9000/700 (HP-UX 10.01)
#			sgi      - SGI (IRIX 5.3)
#			sgi64    - SGI (IRIX 6.1)
#			ibm_rs   - IBM RS/6000 (AIX 4.1)
#			sol2     - Sun Sparc (Solaris 2.5)
#			alpha    - DEC Alpha (OSF/1 3.2)
#
#-----------------------------------------------------------------------------
#
# Copyright (c) 1994-1998 The Mathworks, Inc. All Rights Reserved.
# $Revision: 1.15 $  $Date: 1998/12/24 17:31:46 $
#----------------------------------------------------------------------------
#
#               Get tape device information and position device
#               -----------------------------------------------
#
# Get the default device, create help file ($temp_file2)
#
    case "$ARCH" in
	sun4)
	    ARCHIVE_DEVICE=/dev/rst0
	    ARCHIVE_DEVICEN=/dev/nrst0
	    computer='Sun Sparc (SunOS 4.1.4)'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 Required Option:     'no-rewind' device (will not rewind when closed)

 Device Syntax:       /dev/nr[mt|st|ar]#

                      meta-language: [ ] - select one if required
                                      |  - or
                                      #  - a positive integer

                      n   - no rewind
                      r   - raw
                      mt  - magnetic tape
                      st  - SCSI tape
                      ar  - Archive 1/4 inch tape drive
                      #   - device number

 Examples:            /dev/nrst0  /dev/nrmt0  /dev/nrar0

 Man Pages:           type 'man mtio' or 'man st' or 'man 4 ar' at the
                      UNIX prompt
EOF
#-----------------------------------------------------------------------------
	    ;;
	hp700)
	    ARCHIVE_DEVICE=/dev/rmt/0m
	    ARCHIVE_DEVICEN=/dev/rmt/0mn
	    computer='HP 9000/700 (HP-UX 10.01)'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 Required Option:     'no-rewind' device (will not rewind when closed)

 Device Syntax:       /dev/rmt/[c#d]#[l|m|h]n[b]
		      /dev/rmt/[c#d]#qic[525|150|120]n[b]

                      meta-language: [ ] - select one if required 
                                      |  - or
                                      #  - a positive integer

                      c#d      - controller number (usually blank)
                      #        - device number
                      l, m, h  - density (l = low, m = medium, h = high)
                      n        - no-rewind
                      b        - Berkeley style
                      qic      - QIC device

 Examples:            /dev/rmt/0mn  /dev/rmt/0qicn

 Man Pages:           type 'man 7 mt' at the UNIX prompt
EOF
#-----------------------------------------------------------------------------
	    ;;
	sgi)
	    ARCHIVE_DEVICE=/dev/tapens
	    ARCHIVE_DEVICEN=/dev/nrtapens
	    computer='SGI (IRIX 5.3)'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 Required Options:    'no-rewind' device (will not rewind when closed)
                      'no-swap'   device (will not byte swap on input)

 Device Syntax:       /dev/nrtapens[v]
		      /dev/rmt/tps#d#nrns[v][.density]

                      meta-language: [ ] - select one if required 
                                      |  - or
                                      #  - a positive integer

		      tps      - SCSI tape interface
                      #        - (1st) controller number, (2nd) id number
		      nr, ns   - no-rewind, no-swap
                      v        - variable blocksize
                      density  - for 9 track or Exabyte 8500

 Examples:            /dev/nrtapens /dev/nrtapensv /dev/rmt/tps0d0nrns

 Man Pages:           type 'man mtio' or 'man tps' at the UNIX prompt
EOF
#-----------------------------------------------------------------------------
	    ;;
	sgi64)
	    ARCHIVE_DEVICE=/dev/tapens
	    ARCHIVE_DEVICEN=/dev/nrtapens
	    computer='SGI (IRIX 6.1)'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 Required Options:    'no-rewind' device (will not rewind when closed)
                      'no-swap'   device (will not byte swap on input)

 Device Syntax:       /dev/nrtapens[v]
		      /dev/rmt/tps#d#nrns[v][.density]

                      meta-language: [ ] - select one if required 
                                      |  - or
                                      #  - a positive integer

		      tps      - SCSI tape interface
                      #        - (1st) controller number, (2nd) id number
		      nr, ns   - no-rewind, no-swap
                      v        - variable blocksize
                      density  - for 9 track or Exabyte 8500

 Examples:            /dev/nrtapens /dev/nrtapensv /dev/rmt/tps0d0nrns

 Man Pages:           type 'man mtio' or 'man tps' at the UNIX prompt
EOF
#-----------------------------------------------------------------------------
	    ;;
	ibm_rs)
	    ARCHIVE_DEVICE=/dev/rmt0
	    ARCHIVE_DEVICEN=/dev/rmt0.1
	    computer='IBM RS/6000 (AIX 3.2)'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 Required Option:     'no-rewind' device (will not rewind when closed)

 Device Syntax:       /dev/rmt#.[1|5]

                      meta-language: [ ] - select one if required
                                      |  - or
                                      #  - a positive integer

		      r   - raw
		      mt  - magnetic tape device
                      #   - device number
                      1   - no rewind (on close) and no retension
                            (on open) for density #1
                      5   - no rewind (on close) and no retension
                            (on open) for density #2

 Examples:            /dev/rmt0.1 /dev/rmt0.5

 Man Pages:           type 'man 4 rmt' at the UNIX prompt
EOF
#-----------------------------------------------------------------------------
	    ;;
	sol2)
	    ARCHIVE_DEVICE=/dev/rmt/0mb
	    ARCHIVE_DEVICEN=/dev/rmt/0mbn
	    computer='Sun Sparc (Solaris 2.5)'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 Required Option:     'no-rewind' device (will not rewind when closed)

 Device Syntax:       /dev/rmt/#[l|m|h|u|c][b]n

                      meta-language: [ ] - select one if required
                                      |  - or
                                      #  - a positive integer

                      #        - device number
		      l, m, h  - density (l = low, m = medium, h = high)
		      u, c     - density (u = ultra, c = compress)
                      b        - Berkeley style
		      n        - no-rewind

 Examples:            /dev/rmt/0mbn /dev/rmt/0mn

 Man Pages:           type 'man mtio' or 'man st' at the UNIX prompt
EOF
#-----------------------------------------------------------------------------
	    ;;
	alpha)
	    ARCHIVE_DEVICE=/dev/rmt0h
	    ARCHIVE_DEVICEN=/dev/nrmt0h
	    computer='DEC Alpha (OSF/1 3.2)'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 Required Option:     'no-rewind' device (will not rewind when closed)

 Device Syntax:       /dev/nrmt#[a|l|m|h]

                      meta-language: [ ] - select one if required
                                      |  - or
                                      #  - a positive integer

		      n           - no-rewind
		      r           - raw
		      mt          - magnetic tape device
                      #           - device number
                      a, l, m, h  - density (l = low, m = medium, h = high)

 Examples:            /dev/nrmt0h /dev/nrmt1h

 Man Pages:           type 'man mtio' at the UNIX prompt
EOF
#-----------------------------------------------------------------------------
	    ;;
	*)
	    ARCHIVE_DEVICE='?'
	    computer='unknown'
#-----------------------------------------------------------------------------
	    cat > $temp_file2 << 'EOF'
 No help for this architecture . . . . 
EOF
#-----------------------------------------------------------------------------
	    ;;
    esac
#
    ntry=0
    while true
    do
        . $dir/$clearsc_sh
	ntry=`expr $ntry + 1`
#-----------------------------------------------------------------------------
echo '    T1. Specify Tape Device and Position Tape to MATLAB Product Files'
echo ''
echo 'The MATLAB product files are located in the second tar file on the'
echo 'distribution tape. We need to move the tape to the start of this file.'
echo ''
#-----------------------------------------------------------------------------
	if [ "$ntry" = "1" ]; then
#-----------------------------------------------------------------------------
echo 'Please specify the tape device that was used to read the MATLAB'
echo 'distribution tape initially.'
echo ''
#-----------------------------------------------------------------------------
            case "$ARCH" in
	        sgi|sgi64)
#-----------------------------------------------------------------------------
echo "Be sure that it is a 'no swap' device . . ."
echo ''
#-----------------------------------------------------------------------------
		    ;;
	        *)
		    :
		    ;;
	    esac
	else
            case "$ARCH" in
	        sgi|sgi64)
#-----------------------------------------------------------------------------
echo "Please specify your tape device with 'no-rewind' and 'no-swap' . . ."
echo ''
#-----------------------------------------------------------------------------
		    ;;
	        *)
#-----------------------------------------------------------------------------
echo "Please specify your tape device with 'no-rewind' . . ."
echo ''
#-----------------------------------------------------------------------------
		    ;;
	    esac
	fi
	if [ "$ntry" = "1" ]; then
	    foundit=0
	    while true
	    do
                mesg="    Device? ([$ARCHIVE_DEVICE]) "
                . $dir/$echon_sh
	        read ans
	        if [ `expr "//$ans" : '//[ 	]*$'` -ne 0 ]; then
	            device=$ARCHIVE_DEVICE
	        else
		    device=`expr "//$ans" : '//[ 	]*\([^ 	]*\)[ 	]*'`
	        fi
#
# Try to determine the corresponding no-rewind device.
#
# Basic algorithm:
#
#	1. Does it match any current file. If not error
#	2. Get the directory part (dirpart) and file part (filepart)
#       3. Get no-rewind strings.
#	4. Check for existence.
#
#	   . If the file already has the norewind string return the input
#	     string
#	   . Find all files with length + length of each no-rewind string
#	   . Fine the files with only the filepart and the rewind
#	     string. Return the list of files.
#
#       4. if exist then continue; else error
#
        	File=$device
        	. $dir/$actualp_sh
        	if [ "$Filepath" = "" ]; then
#-----------------------------------------------------------------------------
echo ''
echo "Error: $device does not exist. Please specify a valid device . . ."
echo ''
#-----------------------------------------------------------------------------
		    continue
	        else
#-----------------------------------------------------------------------------
echo ''
echo "Looking for analogous 'no rewind' device to use . . ."
echo ''
#-----------------------------------------------------------------------------
		    filepart=`expr //$device : '.*/\(.*\)' \| $device`
		    if [ "$device" = "$filepart" ]; then
			dirpart="."
		    else
		        dirpart=`expr //$device : '//\(.*\)/.*'`
		    fi
	            case "$ARCH" in
		    	sun4)
			    nrstring='n'
		            ;;
		    	hp700)
			    nrstring='n'
		            ;;
		        sgi)
			    nrstring='nr'
		            ;;
		        sgi64)
			    nrstring='nr'
		            ;;
		    	ibm_rs)
			    nrstring='.1|.5'
		    	    ;;
			sol2)
			    nrstring='n'
		    	    ;;
			alpha)
			    nrstring='n'
		    	    ;;
		    esac
		    (cd $dirpart; ls) > $temp_file3
		    newfilepart=`echo "$filepart $nrstring" | cat - $temp_file3 | awk '
#-----------------------------------------------------------------------------
    BEGIN { newdirpart = ""; fpx = "" }
    NR == 1 { fp = $1; lfp = length(fp)
	      nnrs = split($2,nrs,"|")
	      for (i = 1; i <= nnrs; i = i + 1) {
		  lnrs[i] = length(nrs[i])
		  inrs = index($1, nrs[i])
		  if (inrs != 0)
		      fpx = substr($1,1,inrs - 1) substr($1,inrs + lnrs[i])
	      }
              next }
	    { f[NR-1] = $1; finv[$1] = 1 }
    END { if ( fp != "" && fpx != "" ) {   	# nrw and rw 
	      if (finv[fp] == 1 && finv[fpx] == 1)
		      newdirpart = fp
	  }
	  else					# rw only
	      for (nf = 1; nf <= NR - 1; nf = nf + 1 )
	          for (i = 1; i <= nnrs; i = i + 1) {
		      l = length(f[nf])
		      if (l != lnrs[i] + lfp) continue
		      inrs = index(f[nf], nrs[i])
		      if (inrs == 0) continue
		      subfp = substr(f[nf],1,inrs - 1) substr(f[nf],inrs + lnrs[i])
		      if (subfp == fp) {
		          if (newdirpart == "") 
		              newdirpart = f[nf]
		          else
		              newdirpart = newdirpart " " f[nf]
		      }
		  }
	  print newdirpart }'`
#-----------------------------------------------------------------------------
		    if [ "$newfilepart" != "" ]; then
#
# If there is a match of filepart with any in the newfilepart list then use
# filepart. Otherwise use the first one in newfilepart
#
		        newdevice=`echo "$filepart" "$newfilepart" | awk '
#-----------------------------------------------------------------------------
	{ fp = $1
	  nfp = $2
	  for (i = 3; i <= NF; i = i + 1)
	      if (fp == $i) { 
		 nfp = fp
		 exit
	      }
	}
    END { print nfp }'`
#-----------------------------------------------------------------------------
#
# Determine whether there is more than one choice
#
		        nchoice=`echo "$filepart" "$newfilepart" | awk '
#-----------------------------------------------------------------------------
	{ fp = $1
	  nchoice = 0
	  if (NF > 2) nchoice = 1
	  for (i = 3; i <= NF; i = i + 1)
	      if (fp == $i) { 
		 nchoice=0
		 exit
	      }
	}
    END { print nchoice }'`
#-----------------------------------------------------------------------------
			
		        newdevice=$dirpart/$newdevice
#
		        if [ "$device" != "$newdevice" ]; then
			    device=$newdevice
			    if [ "$nchoice" = "0" ]; then
#-----------------------------------------------------------------------------
echo "Found it:  $device"
echo ''
#-----------------------------------------------------------------------------
			    else
#-----------------------------------------------------------------------------
echo "Found one:  $device"
echo ''
#-----------------------------------------------------------------------------
			    fi
		        else
#-----------------------------------------------------------------------------
echo "Already one . . ."
echo ''
#-----------------------------------------------------------------------------
		        fi
		        foundit=1
		    else
#-----------------------------------------------------------------------------
echo "Cannot find one . . ."
echo ''
#-----------------------------------------------------------------------------
		    fi
		fi
		break
	    done
	    if [ "$foundit" = "0" ]; then
                mesg='Please press <return> to continue or type h for help: '
                . $dir/$echon_sh
	        read ans
	        if [ `expr "//$ans" : '//[ 	]*$'` -ne 0 ]; then
		    continue
	        else 
		    ans=`expr "//$ans" : '//[ 	]*\([^ 	]*\)[ 	]*'`
	        fi
	        if [ "$ans" = "h" ]; then
	            . $dir/$clearsc_sh
#-----------------------------------------------------------------------------
echo "                      Device Help    [$computer]"
echo '------------------------------------------------------------------------'
#-----------------------------------------------------------------------------
	            cat $temp_file2
#-----------------------------------------------------------------------------
echo '------------------------------------------------------------------------'
#-----------------------------------------------------------------------------
            	    mesg='Please press <return> to continue: '
            	    . $dir/$echon_sh
	    	    read ans
    	    	    . $dir/$clearsc_sh
	    	    continue
	        fi
		continue
	    fi
	else
#-----------------------------------------------------------------------------
echo 'Type h at the next prompt for help . . .'
echo ''
#-----------------------------------------------------------------------------
	    foundit=0
	    while true
	    do
                mesg="    Device? ([$ARCHIVE_DEVICEN]) "
                . $dir/$echon_sh
	        read ans
	        if [ `expr "//$ans" : '//[ 	]*$'` -ne 0 ]; then
		    device=$ARCHIVE_DEVICEN
	        else 
		    device=`expr "//$ans" : '//[ 	]*\([^ 	]*\)[ 	]*'`
	        fi
	        if [ "$device" = "h" ]; then
	            . $dir/$clearsc_sh
#-----------------------------------------------------------------------------
echo "                      Device Help    [$computer]"
echo '------------------------------------------------------------------------'
#-----------------------------------------------------------------------------
	            cat $temp_file2
#-----------------------------------------------------------------------------
echo '------------------------------------------------------------------------'
#-----------------------------------------------------------------------------
            	    mesg='Please press <return> to continue: '
            	    . $dir/$echon_sh
	    	    read ans
    	    	    . $dir/$clearsc_sh
	    	    break
	        else
        	    File=$device
        	    . $dir/$actualp_sh
        	    if [ "$Filepath" = "" ]; then
#-----------------------------------------------------------------------------
echo ''
echo "Error: $device does not exist. Please specify a valid device . . ."
echo ''
#-----------------------------------------------------------------------------
		    else
		        foundit=1
		        break
	            fi
	        fi
	    done
	    if [ "$foundit" = "0" ]; then
		continue
	    fi
#-----------------------------------------------------------------------------
echo ''
#-----------------------------------------------------------------------------
	fi
#
	mesg="    Please load the MATLAB distribution tape and press <return> "
        . $dir/$echon_sh
#-----------------------------------------------------------------------------
	read ans
#-----------------------------------------------------------------------------
echo ''	
#-----------------------------------------------------------------------------
#
#    Need to use the mt command: Need its location on each system.
#
#	sun4    	/bin/mt -f device { fsf 1| rewind }
#	hp700		/bin/mt -t device { fsf 1| rew }
#       sgi		/bin/mt -f device { fsf 1| rewind }
#       sgi64		/bin/mt -f device { fsf 1| rewind }
#	ibm_rs		/bin/mt -f device { fsf 1| rewind }
#	sol2    	/bin/mt -f device { fsf 1| rewind }
#	alpha   	/bin/mt -f device { fsf 1| rewind }
#
# Try to position the device after the first file
#
	ARCHIVE_DEVICEN=$device
	case "$ARCH" in
	    hp700)
		cmd_options1="-t $ARCHIVE_DEVICEN fsf 1"
		;;
	    *)
		cmd_options1="-f $ARCHIVE_DEVICEN fsf 1"
		;;
	esac
#
#-----------------------------------------------------------------------------
	mesg="Attempting to move tape to start of second file . . . (please wait) "
        . $dir/$echon_sh
#-----------------------------------------------------------------------------
#
	REWIND=1
#
# Attempt to rewind the tape first
#
	. $dir/$trewind_sh
        if [ "$ERROR_TAPE_REWIND" = "1" ]; then
    	    . $dir/$clearsc_sh
	    continue
	fi
	REWIND=1
	mt $cmd_options1 2>$temp_file
	if [ $? -ne 0 ]; then
	    REWIND=
	    nc=`wc -c $temp_file | awk '{print $1}'`
#-----------------------------------------------------------------------------
echo ''
echo ''
echo '---------------------------------------------------------------------'
echo "Error: 'mt $cmd_options1' command failed . . ."
echo ''
	    if [ "$nc" = "0" ]; then
#-----------------------------------------------------------------------------
echo '. . . no message . . .'
#-----------------------------------------------------------------------------
	    else
	        cat $temp_file | awk '{print "" $0}'
	    fi
#-----------------------------------------------------------------------------
echo '---------------------------------------------------------------------'
echo ''
            mesg='Please press <return> to continue: '
            . $dir/$echon_sh
#-----------------------------------------------------------------------------
	    read ans
    	    . $dir/$clearsc_sh
	    continue
	else
#-----------------------------------------------------------------------------
echo ''
#-----------------------------------------------------------------------------
#
# Check the status to see how we did.
#
#    DON'T USE IT! The interface is not standard.
#
#    sun4:   /dev/rst0     good - does tell the file position
#    ibm_rs: /dev/rmt0.1   bad  - doesn't tell the file position
#
#	    case "$ARCH" in
#		hp700)
#		    cmd_options1="-t $device status"
#		    ;;
#		*)
#		    cmd_options1="-f $device status"
#		    ;;
#	    esac
#	    mt $cmd_options1 2>$temp_file
#
#-----------------------------------------------------------------------------
# 
# If no TMW_Archive lines then output an informational message.
#
	    if [ "$DECRYPT_PRODUCTS" = "" ]; then
#-----------------------------------------------------------------------------
echo ''
echo "NOTE: Since there is no valid TMW_Archive INCREMENT line in your 'license.dat'"
echo "      file the 'Select MATLAB Products' screen(s) starting with the next"
echo '      screen will initially show NO selections.'
echo ''
#-----------------------------------------------------------------------------
    		. $dir/$cont_sh
	    elif [ "$LOCALARCH_ON_TAPE" = "0" ]; then
#-----------------------------------------------------------------------------
echo ''
echo 'NOTE: Products for your local architecture are NOT on the tape. A default'
echo '      architecture has been selected. You may still install any of the'
echo '      architecture depending products appearing on the next screen using'
echo '      this host architecture. Change the default as you see fit.'
echo ''
#-----------------------------------------------------------------------------
    		. $dir/$cont_sh
	    fi
	    break
	fi
    done
