#!/bin/bash
# Cleversafe open-source code header - Version 1.1 - December 1, 2006
#
# Cleversafe Dispersed Storage(TM) is software for secure, private and
# reliable storage of the world's data using information dispersal.
#
# Copyright (C) 2005-2007 Cleversafe, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
# USA.
#
# Contact Information: Cleversafe, 10 W. 35th Street, 16th Floor #84,
# Chicago IL 60616
# email licensing@cleversafe.org
#
# Author: Greg Dhuse <gdhuse@cleversafe.com>, 
#           based on a script by <rubini@linux.it>

DEVICE="dsd"
BUS="dsdbus"

FILES="dsdbus 0"     # /dev/foo + minor number
MODE="666"           # Bus is usable by anyone

INSMOD=/sbin/insmod; # use /sbin/modprobe if you prefer

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

# kernel version, used to look for modules
KERNEL=`uname -r`

MODDIR="/lib/modules/${KERNEL}/kernel/drivers/${SECTION}"
if [ ! -d $MODDIR ]; then MODDIR="/lib/modules/${KERNEL}/${SECTION}"; fi

# Root or die
if [ "$(id -u)" != "0" ]
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="$1"
	mknod $file c $MAJOR_BUS 0
	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
    cd - > /dev/null
}

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

# Load and create files
function load_device () {
    if [ -f $MODDIR/$DEVICE.ko ]; then
	devpath=$MODDIR/$DEVICE.ko
    else if [ -f ./$DEVICE.ko ]; then
	devpath=./$DEVICE.ko
    else
	devpath=$DEVICE; # let insmod/modprobe guess
    fi; fi
    if [ "$devpath" != "$DEVICE" ]; then
	echo "   Loading file $devpath"
    fi

    if $INSMOD $devpath $OPTIONS; then
	MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
	MAJOR_BUS=`awk "\\$2==\"$BUS\" {print \\$1}" /proc/devices`
	create_files $FILES
    else
	echo "FAILED!"
     fi
}

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


case "$1" in
  start)
     echo "Loading $DEVICE"
     load_device
     ;;
  stop)
     echo "Unloading $DEVICE"
     unload_device
     ;;
  force-reload|restart)
     echo "Reloading $DEVICE"
     unload_device
     load_device
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|force-reload}"
     exit 1
esac

exit 0
