#!/bin/sh
#
#  DHR 03/04/98 - Updates for hsz70 v7.0a kit
#  BVT 062397 - Changes for hsz70 v7.0
#  BVT 061097 - Changes for hsz70 v.002 
#
do_ptisp () { # $1 is one-time-flag
  #
  # Has a familiar adapter. Is it SBS-450, and DECptisp pkg
  # not already installed?
  #
#  echo BVT: do_ptisp $1
  if [ "$1" != 0 ]
  then
    return 1
  fi
  pkginfo -q PTIptisp
  if [ $? -eq 0 ]
  then
      cat <<END_TEXT

## Note: The PTIptisp package appears to be installed. Quit
         pkgadd, do a 'pkgrm PTIptisp', then run this script
         again to add the new DECptisp driver package.

END_TEXT
  fi
 
  pkginfo -q DECptisp
  if [ $? -ne 0 ]
  then 
    cat <<END_TEXT

## Note: When using the SBS-450 SCSI adapter, DEC part# SWXA3-CA,
         device name ptisp or PTI,ptisp, select the DECptisp package
         to process first, then select the DEChsz70 package.

END_TEXT
  else
#   PTI_CUR=810P028110 # old kit version number
    PTI_CUR=810P028132
    PTI_VER=`pkginfo -x DECptisp | tail -1 | sed -e 's/^ *//' | awk '{ print $2 }'`
    if [ "${PTI_VER}" = ${PTI_CUR} ]
    then
      cat <<END_TEXT

## Note: The DECptisp package appears to be installed and current.
         You need only select the DEChsz70 package to process now.

END_TEXT
    else
      cat <<END_TEXT

## Note: The DECptisp package appears to be installed, but not
         current. Quit pkgadd, do a 'pkgrm DECptisp', then run
         this script again to add the new DECptisp driver package.

END_TEXT

    fi ;
  fi
  return 0
}

do_PTI () { # $1 is one time flag
#  echo BVT: do_PTI $1
  cat <<END_TEXT

## Note: You may have a down-rev PROM on your DEC Fast
         Wide Differential adapter. IF so, upgrade to a 
         prom whose last 3 digits are 431 or greater. 

END_TEXT
  do_ptisp $1
  RV=$?
  return $RV
}

do_QLGC () {
#  echo BVT: do_QLGC
  # Has a familiar adapter. Is it SUN QLGC,isp?
  cat <<END_TEXT

## Note: Select only the DEChsz70 package when using the SUN(tm) UDWIS/S
         adapter, SUN(tm) part# X1065A, device name QLGC,isp.

END_TEXT
  return 1
}

do_SUNW () {
#  echo BVT: do_SUNW
  # Has a familiar adapter. Is it SUN QLGC,isp?
  cat <<END_TEXT

## Note: Select only the DEChsz70 package when using the SBS-450
         Fast Wide Differential adapter in SUN(tm) emulation mode,
         device name SUNW,isp.

END_TEXT
  return 1

}

do_UNKN () {
#  echo BVT: do_UNKN
  cat <<END_TEXT

## Note: I see an unfamiliar looking SCSI adapter, $1,
         in the prtconf output. Either your hardware isn't properly
         installed, or you are using an unsupported adapter. If the
         latter, choose "other" from the "Select SCSI adapter" menu.

END_TEXT
  return 1
}

do_none () {
#  echo BVT: do_none
  cat <<END_TEXT

## Note: I don't see any familiar looking SCSI adapters in the
         prtconf output. Either your hardware isn't properly
         installed, or you are using an unsupported adapter.
         Install hardware or choose "manual adapter configuration"
         from the "Select SCSI adapter" menu.

END_TEXT
  return 1
}

cleanup () {
  rm -f ${TMPFILE} ${TMPENV}
  return
}

call_exit () {
  cleanup
  exit $1
}

#
# Executable Starts Here. Mainline
#

TMP=/tmp
DATE=`date '+%a%b%d-%T'`
LOG="/var/adm/DEChsz70.${DATE}.log"
echo "Logging installation to $LOG"
PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/openwin/bin
PKGADD=/usr/sbin/pkgadd
#
# Set by DEChsz70/install/postinstall, used as flag and file
#
TMPENV=${TMP}/DEChsz70_environment
TMPFILE=${TMP}/DEChsz70.$$
OLD_PTI_ADP=PTI,ptisp
PTI_ADP=ptisp
PTI_EMU_ADP=SUNW,ptisp
SUN_ADP=QLGC,isp
DO_PTISP=0
export TMP DATE LOG PATH PKGADD TMPENV TMPFILE OLD_PTI_ADP PTI_ADP PTI_EMU_ADP SUN_ADP 

trap 'call_exit 3' TERM ABRT KILL QUIT HUP

{
cat <<END_TEXT

## Storageworks RAID Array Installation Manager

   The installation manager guides you through the installation
   of the RAID software packages. DEChsz70 is the local Storage-
   works RAID manager software. DECptisp, required only if you 
   use a DEC/PTI SCSI adapter that isn't in SUN emulation mode,
   is the adapter driver software.

   **NOTE**
   This version of DEChsz70 (7.0a), does not contain an Agent.
   Therefore, all configuration and monitoring must be done 
   with the CLI via a serial port.  Refer to the documentation
   for more information.

END_TEXT
# Error exit if no "pkgadd" utility
if [ ! -f "${PKGADD}" ]
then
cat <<END_TEXT

## Error: Sorry, I can't find the pkgadd utility in ${PKGADD}. You
          will have to execute "pkgadd -d . -a admin" by hand, or
          install pkgadd in /usr/sbin.

   Exiting.

END_TEXT
  exit 1
fi

# Error exit if steamd is already running?
ps -ea | grep steamd >/dev/null 2>&1 &&
cat <<END

## Note: Steamd is running on this machine. Installation will not
         automatically start the new executable. After installation
         you can use the configuration utility to "kill -9" the run-
         ning daemon and then start the new one.

END

# BVT 032797
# Look thru prtconf for familiar SCSI adapters
# Take installed hardware with an "isp" even if there is no driver yet attached
# Take all adapters with an "isp" that have a driver attached
OK_ADPS="${PTI_ADP} ${OLD_PTI_ADP} ${PTI_EMU_ADP} ${SUN_ADP}"
ADP_HW_CLASSES=`prtconf | grep "^[	 ]*.*isp" | grep "driver not" | awk \
 '{ print $1 ;  }' \
| sort \
| uniq \
| sed 's/ *//'`
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_HW_CLASSES=$ADP_HW_CLASSES ADP_CLASSES=$ADP_CLASSES
SPACE=${ADP_HW_CLASSES:+' '}
# assignment converts newlines back to spaces
ADP_CLASSES=`echo "$ADP_HW_CLASSES${SPACE}$ADP_CLASSES" | tr ' ' '\012' | sort | uniq`
  
#
# issue advice, warnings, and recommendations for each adapter
#
if [ ! "${ADP_CLASSES}" ]
then
  do_none
else
  cat << END
## Note:  Found adapter(s) of class(es):

END
  for i in ${ADP_CLASSES}
  do
    echo "	$i"
  done
  echo ""
  for i in ${ADP_CLASSES} 
  do
    echo $OK_ADPS | grep $i >/dev/null 2>&1 &&
    case $i in
    ${PTI_ADP})
      do_ptisp ${DO_PTISP}
      DO_PTISP=$?
      continue;;
    ${OLD_PTI_ADP})
      do_PTI ${DO_PTISP}
      DO_PTISP=$?
      continue;;
    ${PTI_EMU_ADP})
      do_SUNW
      continue;;
    ${SUN_ADP})
      do_QLGC
      continue;;
    parse_error)
      echo "## Error: Can't parse "prtconf" line $i."
      echo "Attempting to contine."
      continue;; 
    *)
      do_UNKN $i
      continue;;
    esac
  done
fi
pkginfo -q DEChsz70
if [ "$?" = "0" ]
then 
  cat <<END_TEXT

## Warning: The DEChsz70 package is already fully or partially
            installed. Quit with "q", then uninstall the Agent
            with uninstall.sh utility, or config.sh utility,
            or the "pkgrm DEChsz70" command, then rerun the
            installation.

END_TEXT
fi

${PKGADD} -d . -a admin 2>&1 | tee ${TMPFILE}
RC1=$?
fgrep "no packages were found" ${TMPFILE} >/dev/null 2>&1
RC4=$?
fgrep "No changes were made" ${TMPFILE} >/dev/null 2>&1
RC2=$?
fgrep "ERROR" ${TMPFILE} >/dev/null 2>&1
RC3=$?
#echo BVT: RC1=$RC1 RC2=$RC2 RC3=$RC3 RC4=$RC4
RET=1 
if [ "$RC4" = 0 ]
then
  cat <<END_TEXT

## Error: Installation exiting, no packages available.

END_TEXT
  exit 1
fi
if [ "$RC1" = 3 -a "$RC2" = 1 -a "$RC3" = 1 ]
then
  #
  # Force reconfiguration boot in case pkgadd misses exit code.
  #
  touch /reconfigure

  cat <<END_TEXT
>>>> 
>>>> REBOOT before running DEChsz70 (See IMPORTANT NOTICE, above).
>>>>
END_TEXT
fi
if [ "$RC1" != 0 -o "$RC2" = 0 -o "$RC3" = 0 ]
then
  cat <<END_TEXT

## Error: Pkgadd reports ERROR(s):

	`fgrep ERROR ${TMPFILE}`

END_TEXT
  RET=1
else
  RET=0
fi
pkginfo -q DEChsz70
if [ $? -eq 0 ]
then 
#  test -f ${TMPENV} && { fgrep "services:   nisplus files" /etc/nsswitch.conf || cat <<END_TEXT
#
## Note: Ensure that ports 4998, 4999 added to the /etc/services
#         file are available by checking the /etc/nsswitch.conf
#         entry:
#
#           services: ... files ...
#
#         If "files" doesn't appear before [NOTFOUND=return]
#         you may need to add the port numbers to the nis+
#         database. See your system administrator, and the
#         nsswitch.conf and services man pages. 
#
#END_TEXT
#}

test -f ${TMPENV} && cat <<END_TEXT
>>>> 
>>>> REBOOT your system before attempting to use it. 
     (See IMPORTANT NOTICE, above).
>>>>
END_TEXT
fi
cleanup ;
} | tee $LOG
exit ${RET}
