#!/bin/sh
#-------------------------------------------------------------------------------
#
#  Scriptname: patchscsi                       Platform: SGI IRIX 6.x
#      Author: Michael van de Ven         Creation date: 17/08/1999
#
# Description: Patches the CDROM Volume Monitoring daemon (mediad)
#
#
# Copyright (c) 1998-1999 GEAR Software BV.
#
#-------------------------------------------------------------------------------
#


#
# REVISION LIST
#    
# Revision             Author                   Date
#------------------------------------------------------------------------------
# 0.1 (creation)       Michael van de Ven       17/08/1999
#


#
# VARIABLE DEFINITION
#
# Variable                      Description
#------------------------------------------------------------------------------
# A                             Temp variable for read command.
# ANSWER                        Temp variable that holds an answer.
# DEFAULT                       Variable that holds a default value.
# USERID                        The uid of the user running deinstall.
#


#
# FUNCTION DEFINITION
#
# Function                      Description
#------------------------------------------------------------------------------
# entertocontinue               Show the "Press <enter> to continue."
# message.
# getyesno                      Get a yes/no answer from the user.
# hr                            Displays a 80 char long line.
#

#  This script edits the 'wd93'/'adp78' driver variable tables to set the
#  parameters for the CD recorder properly.
#
#  In WD93 the following settings will be affected:
#    - The minimum time out will be set to 40 (seconds).
#    - The sync enable bit corresponding to the CD recorders SCSI-bus/
#      SCSI-id will be reset.
#    - Disconnects on the the SCSI-bus to which the CD recorder is connected
#      will not be allowed.
#
#  In ADP78 the following settings will be affected:
#    - The sync enable bit corresponding to the CD recorders SCSI-bus/
#      SCSI-id will be reset.
#    - Disconnects on the the SCSI-bus/SCSI-id corresponding to the CD recorder
#      will be allowed.
#    - Synchronous xfer rate will be set to 6.7 Mhz.



PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/bsd:.
#export PATH

umask 022

SCRIPTNAME=`basename $0`
old=`pwd`
cd `dirname $0`
SCRIPTDIR=`pwd`
UPDATE=$SCRIPTDIR/update
cd ${old}

SYS="/var/sysgen/master.d"
WD_FILE="wd93"
ADP_FILE="adp78"
SAVED_WD_FILE="${WD_FILE}.save"
SAVED_ADP_FILE="${ADP_FILE}.save"
SAVE_DIR="/etc/gear_save"
SAVE_DIR_WAS_CREATED="n"
FILE_WAS_COPIED="n"
NEW_FILE=""

CmdArgs ()
{
  BUSID=""
  TARGETID=""

  while getopts c:t: options ; do
    case $options in
      c)
        BUSID=`echo "$OPTARG" | sed -e 's/ //g'`
      ;;
      t)
        TARGETID=`echo "$OPTARG" | sed -e 's/ //g'`
      ;;
    esac
  done
}

CheckSCSICtrlr ()
{
  if [ -f "${SYS}/${WD_FILE}" ]  && [ ! -f "${SYS}/${ADP_FILE}" ] ; then
    FILE="$WD_FILE"
    SAVED_FILE="$SAVED_WD_FILE"
    SCSI_CTRL="wd93"
  elif [ -f "${SYS}/${ADP_FILE}" ] && [ ! -f "${SYS}/${WD_FILE}" ] ; then
    FILE="$ADP_FILE"
    SAVED_FILE="$SAVED_ADP_FILE"
    SCSI_CTRL="adp78"
  elif [ -f "${SYS}/${ADP_FILE}" ] && [ -f "${SYS}/${WD_FILE}" ] ; then
    echo ""
    echo "  To what SCSI controller is your recorder connected: "
    echo "  1)  Western Digital version WD33C93B"
    echo "  2)  Adaptec         version ADAPTEC 7880"
    while true ; do
      echo ""
      echo "  Type [1-2], q = quit: \c"
      read a
      case "$a" in
        1)
          FILE="$WD_FILE"
          SAVED_FILE="$SAVED_WD_FILE"
          SCSI_CTRL="wd93"
          break
        ;;
        2)
          FILE="$ADP_FILE"
          SAVED_FILE="$SAVED_ADP_FILE"
          SCSI_CTRL="adp78"
          break
        ;;
        q)
          exit
        ;;
      esac
    done
  else
    exit
  fi
}

MakeNewFileName ()
{
  pat="$1"
  dir="$2"

  if [ ! -d "$dir" ] ; then
    return
  fi

  ls -1 "$dir" |
  awk "BEGIN { maximum=0
               pat=\"$pat.\"
               lpat=length(pat) }
       \$1 ~ /$pat.[0-9]*\$/ { start=lpat+1
                               len=(length(\$1)-start+1)
                               m=substr(\$1,start,len)
                               if (m>maximum)
                                  maximum=m }
       END { maximum=maximum+1
             print \"$dir\" \"/\" pat maximum }"
}

ExitOK ()
{
  if [ -n "$1" ] ; then
    echo ""
    echo "$1"
  fi
  exit 0
}

ExitError ()
{
  if [ -n "$1" ] ; then
    echo ""
    echo "$1"
  fi
  if [ "{FILE_WAS_COPIED}" = "y" ]  && [ -n "${NEW_FILE}" ] ; then
    rm -f "${NEW_FILE}" > /dev/null 2>&1
  fi
  if [ "${SAVE_DIR_WAS_CREATED}" = "y" ] ; then
    rmdir "${SAVE_DIR}" > /dev/null 2>&1
  fi
  exit 1
}

GetYesNoAnswer ()
{
  question="$1"
  default="$2"
  while true; do
    echo ""
    printf "$question (y or n) "
    if [ -n "$default" ] ; then
      printf " [$default] "
    fi
    read answer
    case "$answer" in
      y|Y|yes|YES)
        return 0
      ;;
      n|N|no|NO)
        return 1
      ;;
      *)
        if [ -z "$answer" ] ; then
          case "$default" in
            y|Y|yes|YES)
              return 0
            ;;
            n|N|no|NO)
              return 1
            ;;
            *)
            ;;
          esac
        fi
        echo ""
        echo "  ERROR: Invalid response, expected \"y(es)\" or \"n(o)\"."
      ;;
    esac
  done
}

CmdArgs $*

echo "  Some CD Recorders may cause problems on the SCSI bus. To solve this,"
echo "  the file \"${SYS}/${WD_FILE}\" or the file"
echo "  \"${SYS}/${ADP_FILE}\" must be changed."
echo "  Some parameters in this file will be adapted so the CD-Recorder"
echo "  will work correctly."
echo "  To do this, you will be prompted for the SCSI bus to which the"
echo "  CD-Recorder is connected and the SCSI-ID assigned to it."
echo "  If you have just installed GEAR you can try to run GEAR with"
echo "  the SCSI bus parameters left unchanged by typing \"n\" or RETURN."
echo "  If this doesn't work you can change the parameters later by running"
echo "  the script \"$SCRIPTNAME\" in the directory"
echo "  \"$SCRIPTDIR\"."
echo "  If you want to change the SCSI bus paramaters right now you can"
echo "  type \"y\"."
echo ""
GetYesNoAnswer "  Change parameters now?" "n"
if [ $? != 0 ] ;  then
   ExitOK
fi

CheckSCSICtrlr

GetYesNoAnswer "  Is it OK to change the file \"${SYS}/${FILE}\"?"
if [ $? != 0 ] ; then
  ExitOK "  The file \"${SYS}/${FILE}\" has not been changed"
fi

if [ ! -f "${SYS}/${FILE}" ] ; then
  ExitError "  The file \"${SYS}/${FILE}\" was not found"
fi

if [ ! -d "${SAVE_DIR}" ] ; then
  mkdir "${SAVE_DIR}" > /dev/null 2>&1
  echo ""
  if [ ! -d "${SAVE_DIR}" ] ; then
    echo "  Creating the directory \"${SAVE_DIR}\" has failed"
    echo "  Check whether you have the correct priveleges (i.e. root)"
    ExitError "  The file \"${SYS}/${FILE}\" has not been changed"
  fi
  echo "  Created directory \"${SAVE_DIR}\""
  SAVE_DIR_WAS_CREATED="y"
fi
NEW_FILE="${SAVE_DIR}/${SAVED_FILE}"
if [ -f "${NEW_FILE}" ] ; then
  GetYesNoAnswer "  The file \"${NEW_FILE}\" already exists, overwrite?" "n"
  if [ $? != 0 ] ; then
    NEW_FILE="`MakeNewFileName ${SAVED_FILE} ${SAVE_DIR}`"
  fi
fi
cp "${SYS}/${FILE}" "${NEW_FILE}" > /dev/null 2>&1
if [ $? != 0 ] ; then
  echo ""
  echo "  Copying the \"${SYS}/${FILE}\" file has failed"
  echo "  Check whether you have the correct priveleges (i.e. root)"
  ExitError "  The file \"${SYS}/${FILE}\" has not been changed"
fi
echo ""
echo "  The file \"${SYS}/${FILE}\" has been saved"
echo "  as \"${NEW_FILE}\""
FILE_WAS_COPIED="y"

if [ ! -x "${UPDATE}" ] ; then
  rm -f "${NEW_FILE}"
  echo ""
  echo "  The executable \"${UPDATE}\""
  echo "  was not found"
  echo "  Check whether \"${UPDATE}\""
  echo "  is present and executable"
  ExitError "  The file \"${SYS}/${FILE}\" has not been changed"
fi

echo ""
if [ -n "$BUSID" ] && [ -n "$TARGETID" ] ; then
  "$UPDATE" -c "$BUSID" -t "$TARGETID" -i "${NEW_FILE}" -o "${SYS}/${FILE}" \
            -f "$SCSI_CTRL"
    status=$?
else
  "$UPDATE" -i "${NEW_FILE}" -o "${SYS}/${FILE}" \
            -f "$SCSI_CTRL"
  status=$?
fi
if [ $status = 0 ] ; then
  echo ""
  echo "  The file \"${SYS}/${FILE}\" has been changed"
  ExitOK "  You must reboot the system after installation is complete"
else
  mv -f "${NEW_FILE}" "${SYS}/${FILE}" > /dev/null 2>&1
  echo ""
  echo "  The editing of \"${SYS}/${FILE}\" failed"
  ExitError "  The file \"${SYS}/${FILE}\" has been restored from \"${NEW_FILE}\""
fi
