#! /bin/bash
# Based on sample init script for the a driver module <rubini@linux.it>
# Modified by <eblennerhassett@audioscience.com>
# 
# chkconfig: 2345 65 72
# description: AudioScience HPI driver

### BEGIN INIT INFO
# Provides: asihpi
# Required-Start: $local_fs $syslog resmgr
# Required-Stop: $local_fs $syslog resmgr
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Start the AudioScience HPI Driver
### END INIT INFO

# Use utility functions that are present (only used for success/failure messages)
#  This is getting to be a pain in the neck!
if [ -f /etc/redhat-lsb/lsb_log_message ]; then
# For some reason, the aliases in /lib/lsb/init-functions don't survive
# back into this script. - Stephen Brown
  SUCCESS=fc_log_success_msg
  FAILURE=fc_log_failure_msg
elif [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
  SUCCESS=log_success_msg
  FAILURE=log_failure_msg
elif [ -f /etc/rc.d/init.d/functions ]; then
# RH 9 seems to lack the LSB init-functions
  . /etc/rc.d/init.d/functions
  SUCCESS=success
  FAILURE=failure
else
  SUCCESS=echo
  FAILURE=echo
fi

function fc_log_failure_msg {
       /etc/redhat-lsb/lsb_log_message failure $*
}
function fc_log_success_msg {
       /etc/redhat-lsb/lsb_log_message success $*
}


DEVICE="asihpi"
#SECTION="sound"
PACKAGE_NAME="AudioScience HPI Driver"

# Everything below this line should work unchanged for any char device.
# Obviously, however, no options on the command line: either in
# /etc/${DEVICE}.conf or /etc/modules.conf (if modprobe is used)

# Optional configuration file: format is
#    owner  <ownername>
#    group  <groupname>
#    mode   <modename>
#    options <insmod options>
CFG=/etc/${DEVICE}.conf

# Defaults if no config file exists
OWNER="root"
GROUP="audio"
MODE="a+rw"

# Root or die
if [ "$(id -u)" != "0" ] &&  [ "$1" != "status" ]
  then
  echo "You must be root to load or unload kernel modules"
  exit 1
fi

# Read configuration file
if [ -r $CFG ]; then
    OWNER=`awk "\\$1==\"owner\" {print \\$2}" $CFG`
    GROUP=`awk "\\$1==\"group\" {print \\$2}" $CFG`
    MODE=`awk "\\$1==\"mode\" {print \\$2}" $CFG`
    # The options string may include extra blanks or only blanks
    OPTIONS=`sed -n '/^options / s/options //p' $CFG`
fi


# Create device files
function create_files () {
    cd /dev
    if test ! -e $DEVICE ; then
	mknod $DEVICE c $MAJOR 0
    fi
    if [ -n "$OWNER" ]; then chown $OWNER $DEVICE; fi
    if [ -n "$GROUP" ]; then chgrp $GROUP $DEVICE; fi
    if [ -n "$MODE"  ]; then chmod $MODE  $DEVICE; fi

}

# Remove device files
function remove_files () {
    cd /dev
    rm -f $DEVICE
}

# Load and create files
function load_device () {
    if /sbin/modprobe  $DEVICE $OPTIONS; then
	remove_files
    else
	echo " FAILED!"
     fi
}

# Unload and remove files
function unload_device () {
    if /sbin/rmmod $DEVICE ; then
        remove_files
    fi
}

 case "$1" in
   start)
     if test `cat /proc/modules | grep -c asihpi` -eq 0 ; then
        load_device
     fi
     if test `cat /proc/modules | grep -c asihpi` -eq 0 ; then
        $FAILURE "Starting $PACKAGE_NAME"
        exit 1
     else
 	MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
    	create_files
        $SUCCESS "Starting $PACKAGE_NAME"
        exit 0
     fi
      ;;
   stop)
     if test `cat /proc/modules | grep -c asihpi` -ne 0 ; then
        unload_device
     fi
     if test `cat /proc/modules | grep -c asihpi` -eq 0 ; then
        $SUCCESS "Stopping $PACKAGE_NAME"
        exit 1
     else
        $FAILURE "Stopping $PACKAGE_NAME"
        exit 0
     fi
      ;;
   force-reload|restart)
     if test `cat /proc/modules | grep -c asihpi` -ne 0 ; then
        unload_device
        if test `cat /proc/modules | grep -c asihpi` -ne 0 ; then
           $FAILURE "Restarting $PACKAGE_NAME"
           exit 1
        fi
     fi
     load_device
     if test `cat /proc/modules | grep -c asihpi` -eq 0 ; then
        $FAILURE "Restarting $PACKAGE_NAME"
        exit 1
     else
 	MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
    	create_files
        $SUCCESS "Restarting $PACKAGE_NAME"
        exit 0
     fi
     ;;
  status)
     if test `cat /proc/modules | grep -c asihpi` -eq 0 ; then
        echo "$PACKAGE_NAME stopped"
        exit 3
     else
        echo "$PACKAGE_NAME running"
        exit 0
     fi
      ;;
   *)
     echo "Usage: $0 {start|stop|restart|force-reload|status}"
      exit 1
 esac

exit 0
