#! /bin/sh

#
# RedHat/Fedora initscript header (chkconfig info)
#
###
# chkconfig: 345 95 05
# description: create device nodes for drivers used to communicate
#              with the SGI System Controller
###

#
# SUSE (SLES) initscript header
#
### BEGIN INIT INFO
# Provides:          sgi_snsc_mkdev
# Required-Start:    $syslog $local_fs
# Default-Start:     3 5
# Short-Description: SGI System Controller device driver creation
# Description:       Creates the SGI System Controller device nodes
#                    (/dev/snsc/*) using the devices listed in /sys/class.
### END INIT INFO


# this script is only relevant on IA64 systems
if [ `/bin/arch` != "ia64" ]; then
    echo "WARNING:  $0:  only for use on IA64 systems (not IA32!)"
    exit 0
fi

# Source function library.
if [ -r /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
    RH_INIT=y
elif [ -r /etc/rc.status ]; then
    . /etc/rc.status
    rc_reset   # reset the status of this service
    LSB_INIT=y
fi

# skip if this isn't a "start"
[ "$1" = "start" ] || exit 0

device="snsc"
mode="600"

# skip if there are no /sys/class/${device} entries
[ -e /sys/class/${device} ] || exit 0

# remove the existing directory of devices
if [ -e /dev/${device} ] ; then
    rm -rf /dev/${device}
fi

echo -n "Creating device nodes for System Controller drivers:"

mkdir /dev/${device}

for n in $(ls /sys/class/${device}) ; do
    if [ -e "/dev/${device}/${n}" ] ; then
        rm -f /dev/${device}/${n}
    fi
    major_minor=$(awk -F ':' '{printf "%s %s", $1, $2}' /sys/class/${device}/${n}/dev)
    mknod /dev/${device}/${n} c $major_minor
    chmod $mode /dev/${device}/${n}
done 


#
# Clean up and report status
#

if [ "$RH_INIT" = "y" ]; then
    [ $? -eq 0 ] && success && echo
elif [ "$LSB_INIT" = "y" ]; then
    rc_status -v
    rc_exit
fi
