#!/bin/sh
#
#  Adaptec Linux iSCSI Host Bus Adapter
#
# Copyright as an unpublished work
# (c) 2004, Adaptec, Inc.
# All right reserved.
# description: loads the asa72xx device driver during system boot up.
#
# Source function library.

if [ -d /etc/SuSEconfig ]
then
   echo "SuSE mode";
   [ -f /etc/init.d/functions ] && . /etc/init.d/functions 
   # rc.status needs to know what we are doing to write the right messages, but
   # we already shifted original $1 away. Set it again for rc.status
   #set ${ACTION:-somethingelse}
   . /etc/rc.status
   # rc_reset
elif [ -f /etc/issue ]
then
   echo "RedHat mode";
   . /etc/init.d/functions
else
   echo "Unknown distribution mode";
fi

module="asa72xx"
paramoptions=" "

if [ -f /etc/asa72xx.conf ]; then
    paramoptions=`cat /etc/asa72xx.conf | awk '
			/^IB:*/ { printf $1" " }
			/^DeviceQueueDepth:*/ { printf $1" " }
			/^Bind:*/ { printf $1" " }'`
    paramoptions=`echo $paramoptions`
else
    echo "$0: /etc/asa72xx.conf not found, no adjustable parameter options"
    exit 1
fi

alloptions="asa72xxc=\"${paramoptions}\""

#check to see if make_modules_install worked.....
#
case "$1" in
  start)
       #check to see if module is loaded.....
       #
       if [ -e /proc/scsi/asa72xx ]
       then
          exit 1
       fi

       cmd="/sbin/insmod ${module}.o ${alloptions}"
       suse_cmd="/sbin/insmod ${module}.o"

       if [ -f /lib/modules/`uname -r`/scsi/asa72xx.o ] 
       then
          cmd="/sbin/insmod /lib/modules/`uname -r`/scsi/${module}.o ${alloptions}"
          suse_cmd="/sbin/insmod /lib/modules/`uname -r`/scsi/${module}.o"
       elif [ -f /lib/modules/`uname -r`/kernel/drivers/scsi/asa72xx.o ]
       then
          #
          # Lets make sure the scsi drivers are loaded. User, add options 
          # to modules.conf
          #
          cmd="/sbin/insmod /lib/modules/`uname -r`/kernel/drivers/scsi/${module}.o ${alloptions}"
          suse_cmd="/sbin/insmod /lib/modules/`uname -r`/kernel/drivers/scsi/${module}.o"
       else
          echo "$0: Unable to find asa72xx.o in /lib/modules/... directory !!"
       fi

       insmod -q scsi_mod 2> /dev/null
       insmod -q sd_mod 2> /dev/null

       # if SuSE
       if [ -d /etc/SuSEconfig ]
       then
          $suse_cmd asa72xxc="$paramoptions"
          rc_status -v
       elif [ -f /etc/issue ]
       then
          action $"Bringing up asa72xx iSCSI interface: "  $cmd
       else
          echo "Unknown distribution mode";
          exit
       fi

       ;;
stop)
       cmd="/sbin/rmmod asa72xx"

       if [ -d /etc/SuSEconfig ]
       then
          echo $"Shutting down asa72xx iSCSI interface: " 
          $cmd 
          rc_status -v
       elif [ -f /etc/issue ]
       then
          action $"Shutting down asa72xx iSCSI interface: " $cmd
       else
          echo "Unknown distribution mode";
          exit
       fi

       ;;
restart)
       $0 stop
       $0 start

       ;;
*)
       echo $"Usage: $0 {start|stop|restart}"
       exit 1
esac

exit 0
