#!/bin/bash

###############################################################################
#
# (C) ccrl.nj.nec.com 1999
#
# /.../gm.../binary/gm_install
#
###############################################################################

export OBJFILE=sbin/gm
export DEVNAME1=gm
export MINOR1=0
export DEVNAME2=gmp
export MINOR2=1

export MAJOR=41
export VER=`uname -r`

#
# handy functions for rmmod/insmod
#
function xrmmod () {
        grep -qe "^$1" /proc/modules || return
        echo rmmod $1
        rmmod $1 || exit 1
}

function xinsmod () {
        echo insmod $*
        insmod -f $* || exit 1
}

#
# Installs the driver object file in the actual driver directory
# of your system.
#
function makeins () {

	echo "install driver in /lib/modules/$VER/net"
	mkdir -p /lib/modules/$VER/net
	install -m 644 -c $1 /lib/modules/$VER/net
	depmod -a
}
                 
#
# Creates the nodes in /dev directory.
#    
function makedev () {

	for dev in 0 1 2 3; do
		echo "/dev/$1$dev: char $2 `expr $3 "+" $dev "*" 2`"
		rm -f /dev/$1$dev
		mknod /dev/$1$dev c $2 `expr $3 "+" $dev "*" 2`
		chmod 666 /dev/$1$dev
	done

	# symlink for default device
	# rm -f /dev/$1
	# ln -s /dev/${1}0 /dev/$1
}

#
# Brings the network up and unload/load the driver.
#
function netinst () {

    sync ; sync
    echo "ifconfig myri0 down - in case it was up"
    ifconfig myri0 down
    echo "Removing any existing gm driver."
    xrmmod gm
    sleep 2
    echo "Adding new GM driver."
    xinsmod -f  gm
    echo " "
    echo tail /var/log/messages
    tail --lines 5 /var/log/messages | grep -i gm
    echo " "
    echo "Done"
}

#
# Test the user is su, otherwise stop installation.
#
if test "$UID" = "0"; then

	#
        # running as root anyway, don't need sudo
	#
	echo "Running $0 as supersuser ..."
	
	echo "*** creating directories and copy driver ***"
	makeins $OBJFILE
	
	echo "************* creating nodes ***************"
	makedev $DEVNAME1 $MAJOR $MINOR1
	makedev $DEVNAME2 $MAJOR $MINOR2
	
	echo "************* installing net ***************"
	netinst	
else
	echo "Sorry, the installation has to be done by superuser!"
	exit 1
fi



