#! /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
# Required-Stop: $local_fs
# 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)
if [ -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

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

# The list of filenames and minor numbers: $PREFIX is prefixed to all names
PREFIX="asihp"
FILES="      i 0"
# We dont use the minor numbers
#FILES="   i 0   ia 16         ib 32         ic 64        id 128"

function device_specific_post_load () {
    true; # fill at will
}
function device_specific_pre_unload () {
    true; # fill at will
}

# 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
    local devlist=""
    local file
    while true; do
	if [ $# -lt 2 ]; then break; fi
	file="${PREFIX}$1"
	mknod $file c $MAJOR $2
	devlist="$devlist $file"
	shift 2
    done
    if [ -n "$OWNER" ]; then chown $OWNER $devlist; fi
    if [ -n "$GROUP" ]; then chgrp $GROUP $devlist; fi
    if [ -n "$MODE"  ]; then chmod $MODE  $devlist; fi
}

# Remove device files
function remove_files () {
    cd /dev
    local devlist=""
    local file
    while true; do
	if [ $# -lt 2 ]; then break; fi
	file="${PREFIX}$1"
	devlist="$devlist $file"
	shift 2
    done
    rm -f $devlist
}

# Load and create files
function load_device () {
    if /sbin/modprobe  $DEVICE $OPTIONS; then
	MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
	remove_files $FILES
	create_files $FILES
	device_specific_post_load
    else
	echo " FAILED!"
     fi
}

# Unload and remove files
function unload_device () {
    device_specific_pre_unload 
    /sbin/rmmod $DEVICE
    remove_files $FILES
}

 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
        $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
        $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
