#!/bin/sh
#
#	getEmailMedia	[MediaType dataLoc]
#
#	getEmailMedia is used to get all the installation media information
#	It can be passed default MediaType and datatLoc values
#	MediaType could be "TAPE", "CD-ROM", "TFILE"
#
#############################################################################
#############################################################################
#
#	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/openwin/bin
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() {
    MORE=""  more
}

EchoN() {
    echo -n "$*"
}

Beep() {
    $jsr EchoN ""	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
}

OSvendor=sun4
OSarchs=$OSvendor

LegalVendor() {
    OSVER=`/bin/uname -r`
    OSTYPE=`/bin/uname -s`
    test $OSTYPE = "SunOS" && test $OSVER -lt 5
    return $?
}



SetExport() {
    file=$1
    variable=$2
    entry=$3

    test -f $file || touch $file
    grep -s ${variable} ${file} 2>&1 > /dev/null

    if [  "$?" = 0 ]; then
        grep -v ${variable} ${file} > /tmp/infile$$
        cp /tmp/infile$$ ${file}
        rm /tmp/infile$$
    fi

    echo "${variable}=\"${entry}\" ; export ${variable}" >> $file
}



NL="
"

EnterMedia() {
    NL="
"
    if [ $# -gt 0 ]; then
	value="$1"
    fi	
    while : ; do
	read choice junk
	case $choice in
	    /*) echo "$choice"
	        break;;
	    "") echo "$value"
		break ;;
	    *)  $jsr EchoN "${NL}	Specify full path: " >/dev/tty;;
	esac
    done
}


GetMediaHost() {
    if [ $# -gt 0 ]; then
	value="$1"
    fi	
    while : ; do
        read choice junk
        if [ -z "$choice" -a ! -z "$value" ]; then
            echo ${value}:
            break
        elif [ -z "$choice" -o ! -z "$junk" ]; then
            $jsr EchoN "        Type hostname: "       >/dev/tty
        else
            echo ${choice}:
            break
        fi
    done

}


PATH=$sysPATH;    	export PATH
umask 000

tapeDev=/dev/rst8


cdDev=/cdrom

if [ -z "$Proot" ]; then
    echo "$0 environment not initialized"                               1>&2
    exit 1
fi

if [ $# -gt 0 ] ; then
   mediaType=$1
   mediaLoc=$2
 
   case $mediaLoc in
        /*) place="local" ; remote=""; tapedev=$mediaLoc
            ;;
      *:/*) place="remote" ; remote="`echo $mediaLoc | sed -n 's@:.*@@p'`" 
	    tapedev="`echo $mediaLoc | sed -n 's@.*:@@p'`" ;;
         *) echo "Error: Media specification ($mediaLoc) must be fully rooted" 1>&2
            exit 1 ;;
   esac
   case $mediaType in
	TAPE)  level=1.1 ;;
	CDROM) level=1.2 ;;
	TFILE) level=1.3 ;;
   esac
else
   default=
   tapdev=
   level=0
fi 

if [ -z "$bin_dir" ]; then
  bin_dir=$Proot/install/bin.${OSvendor}
fi 

tmp=$Proot/install/tmp
pkgs=$Proot/install/pkgs
recordFile=$Proot/install/.cdsloadrc.${OSvendor}
device=$Proot/install/tmp/.device.log

info_menu=
while [ ! -z "$level" ]; do
    options=
    choice=
    if [ -s $device ]; then
       TAPE_LABEL=`head -1 $device`
       export TAPE_LABEL
       rm $device
    fi

    echo "cat << EOMENU" > $tmp/menu$$   
    #display text between "LEVEL${level}" and nearest "LEVEL_END"
    {
	test -z "$info_menu" || \
	    sed -n -e '/^LEVEL'$info_menu'$/,/^LEVEL_END$/!b' \
		-e '/^LEVEL/!p' $bin_dir/menu_emailMedia

	sed -n -e '/^LEVEL'$level'$/,/^LEVEL_END$/!b' \
		-e '/^LEVEL/!p' $bin_dir/menu_emailMedia
    } >> $tmp/menu$$
 
    echo "EOMENU" >> $tmp/menu$$
 
    . $tmp/menu$$

    rm $tmp/menu$$
    info_menu=

    case $level in
	0)	level=1;;
	1)	options="1 2 3"
		case "$default" in
		  TAPE) option="1" 
		        tapedev=$tapeDev
                        ;;
		  CDROM) option="2"
			 tapedev=/cdrom
                         ;;
		  TFILE) option="3" 
			 tapedev="$Proot/install" ;;
		esac ;;
	1.1)	
		mediaType=TAPE
		case "$place" in
		  local) option="1" ; default=1;;
		  remote) option="2" ; default=2 ;;
		esac 
		options="1 2";;
	1.1.1)  
		remote=
		level=1.1.1.f;;
	1.1.2) 
		if [ -z "$remote" ] ; then
                   $jsr EchoN "	Type the hostname of the machine connected to the tape drive: "
		else
		   $jsr EchoN "	Type the hostname of the machine connected to the tape drive: [ $remote ] "
		fi
		remote=`$jsr GetMediaHost $remote`
                echo ""
                options=""
                level=1.1.1.f
                ;;
	1.1.1.f)
                if [ "$tapedev" = "$tapeDev" ]; then
                   default=1 ; option="1"
                else
                   default=2 ; option="2"
                fi
                options="1 2" ;;
	1.1.1.f.1)
		mediaLoc=${remote}${tapeDev}
                level=1.1.1.f.f
                options="";;
        1.1.1.f.2)
                if [ -z "$tapedev" ]; then
                  $jsr EchoN "	Type the tape device: "
                else
                  $jsr EchoN "	Type the tape device: [ $tapedev ] "
                fi
                mediaLoc=${remote}`$jsr EnterMedia $tapedev`
                echo ""
                level=1.1.1.f.f
                options="";;
	1.1.1.f.f)
	     	if $BOURNE $bin_dir/media_iface $$ open $mediaLoc; then	
		   case $mediaType in
			CD-ROM) variable="DeviceCD" ;;
			TAPE)   variable="DeviceTAPE" ;;
		   esac
		   SetExport $recordFile ${variable} $mediaLoc
                   exit 0
		else
	           $jsr EchoN "${NL}    Respecify $TAPE_LABEL mount point? [y/n] "
		   $jsr Affirmative ||{
		   exit $? 
		   }
		   level=1.1
		fi ;;
        #
        # yvonne cdrom
        #
        #          1.2
        #          ---
        #          1.2.1
        #          1.2.1.f
        #          -------
        #          1.2.1.f.1   1.1.1.f.f  1.2
	1.2)	
		mediaType=CD-ROM
                case "$place" in
                  local) option="1" ; default=1 ;;
                  remote) option="2"; default=2 ;;
		esac
                options="1 2";;
	1.2.1)
		remote=
		level=1.2.1.f ;;
	1.2.1.f )
                if [ "$tapedev" = "$cdDev" ]; then
                   default=1
                   option=1
                else
                   default=2
                   option=2
                fi
		options="1 2" ;;
	1.2.1.f.1)
                mediaLoc=${remote}/cdrom
                level=1.1.1.f.f
                options="";;
        1.2.1.f.2)
                if [ -z "$tapedev" ]; then
                  $jsr EchoN "	Type the CD-ROM mount point: "
                else
                  $jsr EchoN "	Type the CD-ROM mount point: [ $tapedev ] "
                fi
                mediaLoc=${remote}`$jsr EnterMedia $tapedev`
                echo ""
                level=1.1.1.f.f
                options="";; 
	1.2.2)
		if [ -z "$remote" ] ; then
                   $jsr EchoN \
"	Type the hostname of the machine where the CD-ROM is mounted: "
		else
                   $jsr EchoN \
"	Type the hostname of the machine where the CD-ROM is mounted:[ $remote ] "
		fi
                remote=`$jsr GetMediaHost $remote`
                echo ""
                options=""
                level=1.2.1.f
                ;;
	1.3) 
	        mediaType=TFILE
                case "$place" in
                  local) option="1" ; default=1;;
                  remote) option="2"; default=2 ;;
                esac 
                options="1 2";;
        1.3.f)
		if [ -z "$tapedev" ] ; then
                   $jsr EchoN "	Type the file name with full path:"
		else
                   $jsr EchoN "	Type the file name with full path: [ $tapedev ]"
		fi
                mediaLoc=${remote}`$jsr EnterMedia $tapedev`
                level=1.1.1.f.f
                options="" ;;
	1.3.1)
		remote=
		level="1.3.f"
		options="" ;;
	1.3.2) 
		if [ -z "$remote" ] ; then
		   $jsr EchoN "	Type the hostname of the machine where the file is located: "
		else
		   $jsr EchoN "	Type the hostname of the machine where the file is located:[ $remote ] "
	 	fi
                remote=`$jsr GetMediaHost $remote`
                echo ""
                options=""
		level=1.3.f ;;
	*)      echo "Unrecognized level: $level" 
	        exit 1 ;;
    esac


    if [ ! -z "$options" ]; then
	if [ -z "$choice" ]; then
	  if [ -z "$default" ]; then
	    $jsr EchoN "${NL}	Type your choice: "
	  else
	    $jsr EchoN "${NL}	Type your choice: [ $default ] "
	  fi
	  read choice
	fi
	case $choice in
	    h)  info_menu=$level.h
		;;
	    p)  
		if [ "$level" = "1.1.1.f" ]; then
		   level=1.1 ; default=$remote
		elif [ "$level" = "1.2.1.f" ]; then
		   level=1.2 ; default=$remote
		else
		   test "$level" = "1.1" -o "$level" = "1.2" && exit 255
		   level=`echo $level | sed -n 's@\(.*\)\..@\1@p'`
 		fi
		$jsr Clear
		;;
	    m)	
		exit 250
		;;
	    "") 
		level="$level.$option" 
		;;
	    *)	for option in $options; do
		    if [ "X$choice" = "X$option" ]; then
			echo ""
			level="$level.$option"
			break
		    fi
		done
		;;
	esac
    fi
done

