#!/bin/sh
# Startup script for settting up ccwgroup devices on S390/zSeries with Kernel 2.6
#Version 1.0

# If tr exists
function tolower () {
	which tr &> /dev/null
	if [ $? != 0 ]; then
		echo no tr found
    		VALUE=$1
    		return 1
	fi
	VALUE=`echo $1 | tr '[:upper:]' '[:lower:]'`
}

# paramter: $1=driver $2=deviceaddress
function isdriveraddress () {
	if [ "x$1" = "xlcs" -o "x$1" = "xctc" ]; then
		local DRIVER=cu3088
	else
		local DRIVER=$1
	fi    
	[ -h $CCWDIR/$DRIVER/$2 ] || {
		echo address $2 is not a valid $DRIVER device number
		VALID=false
		return 0
	}
	return 1
}


function loadctc {
	[ -d $CTCDIR ] || {
		echo -n Loading ctc...
		modprobe ctc
		if [ -d $CTCDIR ]; then
			echo ok
		else
			echo failed
		fi
	}
}

function loadqeth {
	[ -d $QETHDIR ] || {
		echo -n Loading qeth...
		modprobe qeth_mod
		if [ -d $QETHDIR ]; then
			echo ok
		else
			echo failed
		fi
	}
}


function loadlcs {
	[ -d $LCSDIR ] || {
		echo -n Loading lcs...
		modprobe lcs
		if [ -d $LCSDIR ]; then
			echo ok
		else
			echo failed
		fi
	}
}




# parameter $1=driver $2-$4=deviceaddresses
function newgroup () {
	if [ "x$1" = "xqeth" ]; then
		tolower $2
	    	local FIRST=$VALUE
	    	tolower $3
	    	local SECOND=$VALUE
	    	tolower $4
	    	local THIRD=$VALUE
	    	VALID=true
		loadqeth
	    	isdriveraddress qeth $FIRST
	   	isdriveraddress qeth $SECOND
	    	isdriveraddress qeth $THIRD
	    	[ x$VALID = xtrue ] || return 1
	    	GROUPDIR="$QETHDIR/$FIRST"
	    	if [ ! -h "$GROUPDIR" ]; then

			echo "$FIRST,$SECOND,$THIRD" > $QETHDIR/group 
	    	fi
	fi
	if [ "x$1" = "xctc" ]; then
		tolower $2
	    	local FIRST=$VALUE
	    	tolower $3
	    	local SECOND=$VALUE
            	VALID=true
		loadctc
            	isdriveraddress ctc $FIRST
            	isdriveraddress ctc $SECOND
            	[ x$VALID = xtrue ] || return 1
	    	GROUPDIR="$CTCDIR/$FIRST"
	    	if [ ! -h "$GROUPDIR" ]; then
			echo "$FIRST,$SECOND" > $CTCDIR/group
	    	fi
	fi
	if [ "x$1" = "xlcs" ]; then
	    	tolower $2
	    	local FIRST=$VALUE
	    	tolower $3
	    	local SECOND=$VALUE
            	VALID=true
		loadlcs
            	isdriveraddress lcs $FIRST
            	isdriveraddress lcs $SECOND
            	[ x$VALID = xtrue ] || return 1
	    	GROUPDIR="$LCSDIR/$FIRST"
	    	if [ ! -h "$GROUPDIR" ]; then
			echo "$FIRST,$SECOND" > $LCSDIR/group
	    	fi
	fi
	[ -h "$GROUPDIR" ] || {
	    	echo "failed to create ccwgroup for driver $1 and adresses $FIRST $SECOND $THIRD"
	    	GROUPDIR="nothing"
	}
}


function addparm () {
	[ "x$1" != "xnothing" ] || return 1
    	[ -f $1/$2 ] || {
		echo There is no option \"$2\" in $1
		return 1
    	}
    	echo $3 > $1/$2 
}

function setonline () {
	[ -e "$1" ] && {
    		echo 1 > $1/online
    	}
}


function parse () {
	if [ "x$1" = "xgroup" ]; then
		[ "$GROUPDIR" = "nothing" ] || setonline $GROUPDIR
		newgroup $2 $3 $4 $5
		continue
    	else
		addparm $GROUPDIR $1 $2
    	fi
}




# Main
if [ ! -f /etc/ccwgroup.conf ]; then
	echo "No /etc/ccwgroup.conf file"
    	exit 2
fi
3< /etc/ccwgroup.conf
SYSFSDIR=`grep "sysfs" /proc/mounts | awk '{ print $2 }'`
SYSFSDIR=${SYSFSDIR:-/sys}
if [ "$SYSFSDIR" = "" -o ! -d $SYSFSDIR ]; then
	echo "sysfs not mounted ?!"
    	exit 1
fi
CCWDIR=$SYSFSDIR/bus/ccw/drivers
QETHDIR=$SYSFSDIR/bus/ccwgroup/drivers/qeth
CTCDIR=$SYSFSDIR/bus/ccwgroup/drivers/ctc
LCSDIR=$SYSFSDIR/bus/ccwgroup/drivers/lcs
GROUPDIR=nothing

while read -u 3 LINE
do
	LINE=${LINE%%\#*} #remove comments
    	[ "x$LINE" = "x" ] && continue
	parse $LINE
done
setonline $GROUPDIR
