#!/bin/sh

check_for_root() 
{
    if [ $(id -u) -ne 0 ]
	then
	echo "You need to be root to do this! Please read the INSTALL instructions!"
	exit
    fi
    return
}

create_static_nodes() 
{
    #user forced us to create static nodes
    echo "Warning : Using static device nodes for the iowarrior is not recommeded. "
    echo "          See INSTALL instructions for details!"
    echo -n "Proceed anyway (default=no) [yes/no] ? "
    read RESULT
    if [ -z $RESULT ]
	then 
	return
    else
	if [ $RESULT != "yes" ]
	    then
	    return
	fi
    fi
    DEVICE="iowarrior"
    MAJOR=180
    MINORBASE=208
    MAXDEVICE=16
    mkdir /dev/usb 2> /dev/null 
    rm -f /dev/usb/iowarrior*
    echo "creating static devicenodes /dev/usb/iowarrior[0..15]"
    OFFSET=0
    MINOR=$MINORBASE
    while [ $OFFSET -lt $MAXDEVICE ]
      do
      mknod -m 666 /dev/usb/${DEVICE}${OFFSET} c $MAJOR $MINOR
      OFFSET=`expr $OFFSET + 1`
      MINOR=`expr $MINORBASE + $OFFSET`
    done
    return
}

create_udev_rules () 
{
    echo "Checking udev installation..."
    if [ ! -f /etc/udev/udev.conf ]
	then
	echo "Error : cannot find the udev-config file (udev is probably not installed ?). Stop!"
	return
    fi
    if [ ! -x /sbin/udevd ]
	then
	echo "Error : cannot find the udevd-binary (udev is probably not installed ?). Stop!"
	return
    fi
    echo "udev seems to be installed on this system"
    echo "Checking where udev expects our iowarrior rules..."
    RULES_DIR=$(awk '/^ *udev_rules *= *\"/ {split($$1,parts,"\""); print parts[2]}' /etc/udev/udev.conf)
    if [ -z $RULES_DIR ]
	then
	RULES_DIR="/etc/udev/rules.d"
	echo "Rules directory is not set explictly using the default : $RULES_DIR"
    else
	echo "Rules are expected in : $RULES_DIR"
    fi
    if [ -d $RULES_DIR ]
	then
	install -b 10-iowarrior.rules $RULES_DIR 2> /dev/null 
	RULES_INSTALLED=$?
	if [ $RULES_INSTALLED -eq 0 ]
	    then
	    echo "Rules have been setup, now make udev recognize them..."
	    pidof /sbin/udevd > /dev/null 
	    UDEV_PID=$?
	    if [ $UDEV_PID -eq 0 ]
		then
		echo "The udevd-daemon seems to be running..."
		if [ ! -x /sbin/udevcontrol ]
		    then
		    echo "Warning :"
		    echo "Unable find the udevcontrol utility ."
		    echo "You will have to restart the udev-daemon manually or do a reboot "
		    echo "to make udev recognize the rules for iowarriors."
		    return
		fi
		udevcontrol reload_rules 2> /dev/null
		UDEV_UPDATED=$?
		if [ $UDEV_UPDATED -eq 0 ]
		    then
		    echo "Rules were sucessfully installed and reloaded"
		else
		    echo "Warning :"
		    echo "There was a problem reloading the rules into udev!"
		    echo "Restart the udev-daemon manually or do a reboot"
		    echo "to make udev recognize the rules for iowarriors."
		    return
		fi
	    else
		echo "Looks like udev is not running!"
		echo "Make sure its started at boot time in order to automatically "
		echo "create the devices nodes for the iowarriors"
	    fi
	else
 	    echo "Ooops, unable to install our rules into directory $RULES_DIR !"
	fi
    else
	echo "Ooops, the installation directory $RULES_DIR does not exist!"
	echo "Please check your udev-configuration"
    fi
    return
}

print_usage() 
{
    echo "Usage : make_iow_devices [--force-static]"
    echo "Creates device-nodes for iowarriors."
    echo
    echo "The default is to simply add a new rules-file for the udev-daemon"
    echo "and then make the daemon recognize these rules for iowarriors"
    echo
    echo "If udev is not installed on your system, you can create static device-nodes"
    echo "for the iowarriors by using the commandline argument :"
    echo "     --force-static  Create static device-nodes /dev/usb/iowarrior[0..15]"
    echo "                     But before you do this read the INSTALL instructions" 
    echo "                     which explains why you shouldn't!"
    echo 
    echo "     --help          Print this message"
    return
}

#main
check_for_root 
if [ -z $1 ]
    then 
    create_udev_rules
    exit
else
    if [ $1 == "--force-static-nodes" ]
	then
	create_static_nodes
	exit
    fi
    if [ $1 == "--help" ]
	then 
	print_usage
	exit
    fi
    echo "Inavlid argument : $1"
    echo "Try make_iow_devices --help for a list of options"
fi
exit
