#!/bin/sh

#
#
# read_label [host:]mediaLoc tapenum
# Hariraj: Added the tapenum commandline variable so that it could
# be passed onto media_cntrl. media_cntrl needs it to support
# multiple CDROM mount points. /cdrom/CDROM1 ... etc
#

#
# prints to stdout the label information obtained from [host:]mediaLoc
#
#
#############################################################################
#
#	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=''

Clear() {
    clear		2>/dev/null	#beware of bad termcap entry
}

Page() {
    TERM=vt100 MORE=""  more		#make sure we don't clear screen
}

EchoN() {
    echo  "$*\c"
}

Beep() {
    tput bel		2>/dev/null
}


Affirmative() {
    while : ; do
	read Affirm
	case $Affirm in
	    [yY]*) return 0 ;;
	    [nN]*) return 1 ;;
	    *)     $jsr EchoN "Please press y (yes) or n (no) : " ;;
	esac
    done
}

Proceed() {
    test "X$1" = 'X-q' || $jsr EchoN "Press [Return] to proceed, q to quit : "
    while : ; do
	read proc < /dev/tty
	case $proc in
	    "")    return 0 ;;
	    [qQ]*) return 1 ;;
	    *) 	   $jsr EchoN "Press [Return] to proceed, q to quit : " ;;
	esac
    done
}



test -z "$bin_dir"  && bin_dir=`pwd`;           export bin_dir

dataLoc=$1; 	shift
tapenum=$1; shift
dataID=$$; 

useGUI=${useGUI?'useGUI environment not initialized'}

openMedia="$BOURNE  $bin_dir/media_cntrl $dataID  open"
readMedia="$BOURNE  $bin_dir/media_cntrl $dataID  read"
closeMedia="$BOURNE $bin_dir/media_cntrl $dataID close"

dataTapenum="" ;	dataTapeset="" ;	dataFilesets=""
dataTapevers="";	dataUniq=""
while [ "X${dataTapenum}" = "X" -o "X${dataTapeset}" = "X" ] ; do  #{
  if $openMedia -r $dataLoc $tapenum; then  #{
    if $readMedia 0 | $bin_dir/vtar tBf - /tmp > /tmp/label$$; then #{
      sed -n  -e 's@^/tmp/tapenum@dataTapenum=@p'	\
          -e 's@^/tmp/tapeset@dataTapeset=@p'	\
          -e 's@^/tmp/fscnt@dataFilesets=@p'	\
          -e 's@^/tmp/vers@dataTapevers=@p'	\
          -e 's@^/tmp/update@updateUniq=@p'	\
          -e 's@^/tmp/uniq@dataUniq=@p' < /tmp/label$$ > /tmp/labval$$
	  . /tmp/labval$$	#source file to initialize variables
	  rm /tmp/labval$$
    fi  #}
    rm /tmp/label$$
    if [ "X${dataTapenum}" = "X" -o "X${dataTapeset}" = "X" ] ; then #{
      echo "Unable to identify media format"   1>&2
    else
	    break
    fi #}
  else
    echo "Unable to read media $dataLoc"   1>&2
  fi  #}
  if $useGUI; then  #{
    exit 1; 
  else
    $jsr EchoN "Try to read media $dataLoc again? [y/n] "	>/dev/tty
    $jsr Affirmative >/dev/tty || exit 1
  fi  #}	
done  #}
$closeMedia


cat - <<EOF_DATAFILE
dataTapenum=$dataTapenum
dataTapeset=$dataTapeset
dataFilesets=$dataFilesets
dataTapevers=$dataTapevers
dataUniq=$dataUniq
updateUniq=$updateUniq
EOF_DATAFILE
