#! /bin/sh
# 062397 bvt - modified for hsz70
# 061097 bvt - modified for hsz70
#
#  Request script for RAID installation
#
check_yes () {

  RETV=1
  if [ "$1" = y -o "$1" = Y -o "$1" = "yes" -o "$1" = "YES" ]
  then
    RETV=0
  fi
  if [ "$1" = "yeS" -o "$1" = "yES" -o "$1" = "yEs" -o "$1" = "Yes" -o "$1" = "YeS" -o "$1" = "YEs" ]
  then
    RETV=0
  fi
  return $RETV
}

check_yes_exit () {
  check_yes $1
  if [ $? -eq 0 ]
  then
    exit 3
  fi
}

check_no () {

  RETV=1
  if [ "$ANS" = n -o "$ANS" = N -o "$ANS" = "no" -o "$ANS" = "NO" -o "$ANS" = "nO" -o "$ANS" = "No" ]
  then
    RETV=0
  fi
  return $RETV
}

check_no_exit () {
  check_no $1
  if [ $? -eq 0 ]
  then
    exit 3
  fi
}

check_quit () {
  RETV=1
  if [ "$1" = q -o "$1" = Q ]
  then
    RETV=0
  fi
  return $RETV

}
check_quit_exit () {
  check_quit $1
  if [ $? -eq 0 ]
  then
    exit 3
  fi
}

pti_adapter_notices () {
cat <<END_TEXT

## Note: For proper operation of the RAID unit with the DEC/PTI SWXA3-CA
         Ultra SCSI adapter you must use the latest adapter driver package,
         DECptisp, supplied with the installation media.

END_TEXT
}

sun_adapter_notices () {
cat <<END_TEXT

## Note: For proper operation of your RAID subsystem with the SUN(tm) UDWIS/S
         (X1065A) adapter, do not load the DECptisp driver.  Use the isp driver
         included with Solaris.

END_TEXT
#
# Check for 2.x patches
#
  OSREL=`uname -r`
  case $OSREL in
  5.4)
cat <<END_TEXT

## Note: Solaris 2.4 is no longer supported.  You should upgrade
         to a supported version of Solaris.
         Refer to the release notes for more information.

END_TEXT
     ;;
  5.5)
cat <<END_TEXT

## Note: Under Solaris 2.5, you must install operating system
         patch ID# 103936-03 for proper RAID unit operation.
         This is available through SUNSolve.

END_TEXT
     ;;
  5.5.1)
cat <<END_TEXT

## Note: Under Solaris 2.5.1, you must install operating system
         patch ID# 103934-06 for proper RAID unit operation.
         This is available through SUNSolve.

END_TEXT
     ;;
  *)
     ;;
  esac
}

check_adapter () {
  rv=1
  ADAPTER=$1
# note that assignment will remove line feeds too soon, so VAL is redundant
  LN=`prtconf | grep "^[ 	]*${ADAPTER}" | grep -v "driver not"`
  if [ "$LN" ]
  then 
    VAL=`prtconf | grep "^[        ]*${ADAPTER}" | grep -v "driver not" | awk -F, '{ if ( NF == 2 ) print $1 ; else if ( NF >= 3 ) print $1 "," $2 ; else print "parse_error" ; }' \
	| sort \
        | uniq \
        | sed 's/ *//'`
#   echo VAL=$VAL
    if [ "$VAL" = "parse_error" ]
    then
      cat <<END_TEXT

## Warning: While looking for $ADAPTER in the 'prtconf' output
            I found a line LN1 I couldn't parse:

LN1=$LN.

Attempting to continue...

END_TEXT
    elif [ "$VAL" != "$ADAPTER" ]
    then
      cat <<END_TEXT

## Warning: The prtconf command doesn't show a '$ADAPTER'
            SCSI adapter installed on this machine. Under normal
            circumstances the adapter should already be installed.
            You may continue as is, or remove this adapter from
            the configuration list.

END_TEXT
    else
      rv=0
    fi
  else
    cat <<END_TEXT

## Warning: The prtconf command doesn't show a '$ADAPTER'
            SCSI adapter installed on this machine. Under normal
            circumstances the adapter should already be installed.
            You may continue as is, or remove this adapter from
            the configuration list.

END_TEXT
  fi
  return $rv  
}

verify_adapter () {
   ADAPTER=$1
   VAL=`prtconf | grep "^[ 	]*${ADAPTER}" \
        | awk -F, '{ if ( NF == 2 ) print $1 ; else if ( NF == 3 ) print $1 "," $2 ; else print "error parsing prtconf" ; }' \
        | sort \
        | uniq \
        | sed 's/ *//'`
#   echo VAL=$VAL
   if [ "$VAL" != "$ADAPTER" ]
   then
cat <<END_TEXT


## ERROR: The prtconf command doesn't show a $ADAPTER
          SCSI adapter installed on this machine. Try
          selecting "d" (other) from the menu.

END_TEXT
    return 1
  fi
  return 0  
}

#
# Start of script execution
#
trap 'exit 3' TERM ABRT KILL QUIT HUP

#
# Check for sd.conf file
#
TRANSDIR="/kernel/drv"
if [ ! -f /kernel/drv/sd.conf ]
then
  cat <<END_TEXT

## Error: ${TRANSDIR}/sd.conf file is missing."

END_TEXT
  exit 1
fi

#
# Get the SCSI adapters 
#
OLD_PTI_ADP=PTI,ptisp
PTI_ADP=ptisp
PTI_EMU_ADP=SUNW,isp
SUN_ADP=QLGC,isp
OK_ADPS="$PTI_ADP $OLD_PTI_ADP $PTI_EMU_ADP $SUN_ADP"
ADP_LIST=""
export ADP_LIST
#
# Look thru prtconf for familiar SCSI adapters, without drivers
#
ADP_HW_CLASSES=`prtconf | grep "^[       ]*.*isp" | grep "driver not" | awk \
 '{ print $1 ;  }' \
| sort \
| uniq \
| sed 's/ *//'`
ADP_HW_CLASSES=`echo "$ADP_HW_CLASSES" | tr ' ' '\012'| sort | uniq`
if [ "${ADP_HW_CLASSES}" ]
then
  cat <<END_TEXT
## Warning:  'prtconf' shows the following SCSI adapter(s) do not have
             a driver attached:

END_TEXT
  for i in $ADP_HW_CLASSES
  do
    echo "		$i"
  done
  cat <<END_TEXT

	If the RAID box is to be attached to one of these, then quit,
	add the driver, and then run this script. Otherwise the adapter
	must be manually configured by selecting (d) from the menu.
END_TEXT
#  DO_MAN=true
fi

# Look thru prtconf for familiar SCSI adapters with drivers attached
# note that assignment will remove line feeds too soon, so ADP_CLASSES
# is redundant use of prtconf.
ADP_CLASSES=`prtconf | grep "^[       ]*.*isp" | grep -v "driver not" | awk -F, '{ if ( NF == 2 ) print $1 ; else if ( NF >= 3 ) print $1 "," $2 ; else print "parse_error" ; }' \
| sort \
| uniq \
| sed 's/ *//'`
#echo BVT: ADP_CLASSES=$ADP_CLASSES
#
# issue advice, warnings, and recommendations for each adapter
if [ "${ADP_CLASSES}" ]
then
  for i in ${ADP_CLASSES}
  do
    #echo BVT: i=$i
    if [ "$i" = "parse_error" ]
    then
      cat <<END_TEXT

## Warning: While checking the 'prtconf' output for isp adapters I found
          a line I couldn't parse:

	$ADP_CLASSES
    
	Attempting to continue...

END_TEXT
    else
      for j in $OK_ADPS
      do
        #echo BVT: OK_ADPS=$OK_ADPS
        if [ "$i" = "$j" ]
        then
          ADP_LIST="$i ${ADP_LIST}"
          break
        fi
      done
    fi
  done
fi
#echo BVT: ADP_LIST=$ADP_LIST

if [ "${ADP_LIST}" ]
then
  #
  # I found the following adapters, and will modify the sd.conf
  # files treating them all as RAID
  #
  cat <<END_TEXT
  
## Info: I have found the following Fast Wide Differential
         SCSI adapter class(es):

END_TEXT
  for k in ${ADP_LIST}
  do
    echo "		${k}"
  done
  cat <<END_TEXT

         I can edit the /kernel/drv/sd.conf file automatically
         so that all of the adapters listed above will support
         the RAID array, or you may manually select what adapters
         will support it.
         Chose:  y for automatic setup
                 n for manual setup
                 q to skip setting up the sd.conf file (not recomended)
END_TEXT
  ANS=`ckyorn -p "Continue with edit " \
-h "Configure manually only if you have special circumstances "`
  case $ANS in

  q | Q | quit | QUIT )
    check_quit_exit "q"
    break;;

  y | Y )
    DO_MAN=false
    break;;

  n | N )
    DO_MAN=true
    ADP_LIST=
    break;; 
  esac
fi
while ${DO_MAN}
do
  cat << END_TEXT

## Info: Select the SCSI adapter you are using. The adapter name is used
         to set "PARENT=" in /kernel/drv/sd.conf. This file is used by
         the system to determine the location of SCSI targets that need
         to have a driver loaded. 

END_TEXT

  while true
  do
    cat <<END_TEXT
## Select SCSI adapter:

  a) SUN(tm) UDWIS/S UFWD adapter (Sun P/N=X1065A)
     SUN(tm) DWIS/S FWD adapter (Sun P/N=X1062A)
	prtconf entry = QLGC,isp

  b) DEC/PTI SBS-450A UFWD adapter (DEC P/N=SWXA3-CA)
     DEC/PTI SBS-440A FWD adapter (DEC P/N=SWSAP-BC)
 	prtconf entry = ptisp (current firmware) or PTI,ptisp (OLD firmware)

  c) DEC/PTI SBS-440A FWD adapter (DEC P/N=SWSAP-BC) in SUN emulation mode
     (SW1=on)
	prtconf entry = SUNW,ISP

  d) manually add adapter not listed above (may be unsupported...)

  e) edit adapter list (manually add or delete adapters).

  f) skip this step (for the expert user, see "scsi" and
                     "driver.conf" in the man pages).

  x) finished editing adapter list, write sd.conf file

  q) quit the installation completely (abort).
END_TEXT
    echo "\nAdapter list is: ${ADP_LIST:-empty}"
    PROMPT="Enter [a,b,c,d,e,f,x,q]: \c" 
    echo ${PROMPT}
    read REPLY

    for i in $REPLY
    do
    case $i in
    q|Q|quit|QUIT)
      check_quit_exit "q"
      break 3;;
    x|X)
      if [ ! "${ADP_LIST}" ]
      then
        ANS=`ckyorn -p "Leave configuration with no adapters configured " \
-h "For each adapter specified the /etc/kernel/drv/sd.conf will be modified"`

        check_no $ANS
        if [ $? = 0 ]
        then
          continue
        fi
        ADP_LIST="None"
        cat <<END_TEXT

## Warning: You must edit /kernel/drv/sd.conf by hand.

END_TEXT
      fi
      break 3;;

    f|F)
      ADP_LIST="None"
      cat <<END_TEXT

## Warning: You must edit /kernel/drv/sd.conf by hand.

END_TEXT
      break 3;;

    b|B)
      ADAPTER=${PTI_ADP}
      verify_adapter ${ADAPTER} || ADAPTER=${OLD_PTI_ADP}
      verify_adapter ${ADAPTER} || continue ;
      ADP_LIST="${ADP_LIST} ${ADAPTER}"
      pkginfo DECptisp >/dev/null 2>&1
      pti_adapter_notices
      if [ "$?" != "0" ]
      then
        cat <<END_TEXT

## Warning: The <DECptisp> package is a prerequisite package and
            should be installed.

END_TEXT

        ANS=`ckyorn -Q -p "Do you want to continue with the installation of this package " \
-h "To use the adapter supplied with the RAID unit you must install the driver package"`

        check_no_exit $ANS
      fi 
      continue;;
    a|A)
      ADAPTER="QLGC,isp"
      verify_adapter $ADAPTER || continue ;
      ADP_LIST="${ADP_LIST} ${ADAPTER}"
      sun_adapter_notices
      continue;;

    c|C)
      ADAPTER=${PTI_EMU_ADP}
      verify_adapter ${ADAPTER} || continue ;
      ADP_LIST="${ADP_LIST} ${ADAPTER}"
      continue;;

    d|D)
      cat <<END_TEXT

  To check the adapter name, and ensure that an adapter is installed on
  your machine, please determine the name of the adapter from the output
  of the "prtconf" system command, which you must run in another window.

## WARNING: this is EXAMPLE output, not the actual output from your machine!
                    -------
  Typical output looks like:

	SUNW,SPARCserver-1000
	    options, instance #0
	    cpu-unit, instance #1
	        TI,TMS390Z55, instance #1
	        profile, instance #1
	    mem-unit, instance #2
	    io-unit, instance #3
	        sbi, instance #0
	            dma, instance #0
	                esp, instance #0
	                .
                        . 
	            QLGC,isp, instance #0
	                sd (driver not attached)
	                st (driver not attached)
	                sd, instance #7
	     .
	     .

  where the SUN SCSI adapter name in this case is "QLGC,isp".
  The SBS440 may show up as "PTI,ptisp" or "ptisp". The name
  you enter will be used in the /kernel/drv/sd.conf.
END_TEXT

      ADAPTER=`ckstr -d esc -p "Enter the SCSI adapter device name (CR to escape)" \
-h "The name of the SCSI adapter as listed in the prtconf command output"`
      check_quit_exit $ADAPTER
#
#
#
      if [ "$ADAPTER" = "esc" ]
      then
        continue
      fi
# BVT added 01/20/97
# BVT added 04/30/97
      check_adapter $ADAPTER
      ADP_LIST="${ADP_LIST} ${ADAPTER}"
          ;;

    e|E)
      while true
      do
	cat <<END
## Edit menu:

  a) add an adapter to the list.

  d) remove an adapter from the list.

  x) done with edits.
END
        echo "\nAdapter list is: ${ADP_LIST:-empty}"
        PROMPT="Enter [a,d,x]: \c" 
        echo ${PROMPT}
        read REPLY

        for i in $REPLY
        do
          case $i in
          x|X)
            break 2;;
          a|A)
	    PROMPT="Enter adapter identifier: \c"
	    echo ${PROMPT}
	    read ADAPTER
	    ADP_LIST="${ADP_LIST} ${ADAPTER}"
		;;
	  d|D)  
	    PROMPT="Enter adapter identifier: \c"
	    echo ${PROMPT}
	    read ADAPTER
	    ADP_LIST=`echo $ADP_LIST | sed "s/$ADAPTER//"`
	    continue;;
	  *)
		;;
	  esac
	done
      done           ;;
    esac
    done
  done
done
ADP_LIST=`echo ${ADP_LIST}|tr " " "\012"|sort|uniq|tr "\012" " "`

echo $ADP_LIST
#cat <<END_TEXT
#
## NOTE: Using "$ADP_LIST" as the SCSI adapter device name(s)
#         for the "PARENT=" value in the /kernel/drv/sd.conf file.
#
#END_TEXT

#
# Proper handling to "export" (re) defined shell variables.
#
TRANSDIR=`echo $TRANSDIR | awk ' BEGIN { FS="/" } \
                                       { for( i = 2; i < NF; i++ ) \
                                           printf( "%s%s", $i, FS ) ; \
                                         printf( "%s\n", $NF ) }'`
cat >$1 <<!
TRANSDIR=$TRANSDIR
ADP_LIST=$ADP_LIST
!
exit 0
