#!/bin/sh
KERNEL=`uname -r`
ARCH=`uname -m`
MODDIR=/lib/modules/$KERNEL/kernel/drivers/net
KVER=`uname -r | cut -c1-3`

echo "--- VIA Gigabit Ethernet Driver Installation --- "

## Check Linux Distro version ##
case "$KERNEL" in
     2.4.20-8)
	OS=Redhat	
	VER=9
	;;
     2.4.22-1.2115.nptl)
        OS=FedoraCore
        VER=1
        ;;
     2.4.22-1.2174.nptl)
       OS=FedoraCore
       VER=1
        ;;
     2.6.5-1.358)
       OS=FedoraCore
       VER=2
        ;;
      2.6.3-7mdk)
        OS=Mandrake
	VER=10
        ;;
     *)
       echo "*** Sorry, the current system is not supported in this package."
       exit 0
       ;;
esac

## Check the kernel version is 2.6 or 2.4. ##
if [ $KVER == "2.6" ]
then
  # 2.6 kernel
  TARGET=velocityget.ko
  BUILTIN=via-rhine.ko
  MODFILE=/etc/modprobe.conf
else
  TARGET=velocityget.o
  BUILTIN=via-rhine.o
  MODFILE=/etc/modules.conf
fi

## Check the Linux dirsto is 64bit or not. ##
if [ $ARCH != "x86_64" ]
then
  echo "What kind of CPU do you use?"
  echo "1. VIA C3"
  echo "2. VIA C3(Nehemiah)/Intel Pentium 2/3/4"  
  echo "3. AMD K7/K8"
  echo "Please choose item 1,2 or 3:"

  read choice

  case "$choice" in
	1)
	CPUTYPE=c3;;
	2)
	CPUTYPE=pentium;;
	3)
	CPUTYPE=k7;;
  esac
     
	ARCH="$CPUTYPE"
fi

## Move the built-in LAN driver
if [ -f $MODDIR/$BUILTIN ]
then
   mv $MODDIR/$BUILTIN $MODDIR/$BUILTIN.bak
fi

cp $OS/$VER/$ARCH/$TARGET $MODDIR

## Modify the modules.conf or modprobe.conf file.
DEVICE=`grep via-rhine $MODFILE | awk '{print $2}'` 

mv $MODFILE $MODFILE.bak
sed 's/via-rhine/velocityget/g' $MODFILE.bak > $MODFILE

depmod -a

## Shutdown the network and reload it by using new driver.
ifdown $DEVICE > /dev/null
rmmod via-rhine 
rmmod mii 
modprobe velocityget > /dev/null 2> /dev/null 
ifup $DEVICE > /dev/null 2> /dev/null

echo "--- Installation completed. ---"

