#!/bin/bash
# FileName : runDiag
# Function : Bash script to install the Broadcom diagnostics dirver and
#            then run the Linux diagnostics

usage()
{
echo "NAME"
echo "        $0 - executes the Broadcom Linux Diagnostic"
echo ""
echo "SYNOPSIS"
echo "        $0 [-h] [--help] [--silent] [--drvrsRm] [--drvrsKeep]"
echo ""
echo "DISCRIPTION"
echo "        -h           displays this help screen"
echo "        --help       displays this help screen"
echo "        --silent     reduces screen output"
echo "        --drvrsRm    remove installed drivers without prompting"
echo "        --drvrsKeep  keep installed drivers without prompting"
echo "                     has precedence over --drvrsRm"
echo ""
}

verbose=1
reqDrvrsRm=0
reqDrvrsKeep=0

while [ $# -ge 1 ]; do
  case $1 in
    --drvrsRm)    reqDrvrsRm=1;;
    --drvrsKeep)  reqDrvrsKeep=1;;
    --silent )    verbose=0;;
    --help)       usage; exit 1;;
    -h)           usage; exit 1;;
    *)            usage; exit 1;;
  esac
  shift
done

drvrsPresent()
{
  count=0
  drvrsPresent=0;
  for drvr in ${drvrNames[@]}; do
    present=`lsmod | grep ${drvr} | awk '{print $1}'`
    if [ "x${present}" != "x" ]; then
      drvrPresent[$count]=1;
      drvrsPresent=1;
    else
      drvrPresent[$count]=0;
    fi
    count=$(( $count + 1 ))
  done
  
  if [ $verbose == 1 ]; then
    if [ $drvrsPresent == 0 ]; then
      echo "No driver presently installed for Broadcom NetXtreme Chips"
    else
      count=0
      while [ "x${drvrNames[$count]}" != "x" ]; do
        if [ ${drvrPresent[$count]} == 1 ]; then
          echo "...${drvrNames[$count]} driver present"
        fi
        count=$(( $count + 1 ))
      done
    fi
  fi
}

drvrsRm()
{
  if [ $drvrsPresent == 0 ] ; then
    return; 
  fi
  
  if [ $reqDrvrsKeep == 1 ]; then
    return; 
  fi
  
  if [ $reqDrvrsRm == 0 ]; then
    echo "Enter \"c\" to remove driver(s) and continue"
    echo "            any other values exits the run"
    read continueRun
    if [ "x$continueRun" != "xc" ]; then
      echo "Exited Broadcom diagnostic run."
      exit 1;
    fi
  fi

  unameStr=`uname -r`
  kernelVer=${unameStr:0:3}
  if [ $kernelVer == "2.4" ]; then
    diagDrvrIfUpDown down;
  fi
  
  count=0
  for drvrHere in ${drvrPresent[@]}; do
    if [ $drvrHere == 1 ]; then
      rmmod ${drvrNames[$count]}
      if [ $verbose == 1 ]; then
        echo "...rmmod ${drvrNames[$count]}"
      fi
    fi
    count=$(( $count + 1 ))
  done
}


checkExists()
{
  if [ -e $1 ]; then
    if [ $verbose == 1 ]; then
      if [ -d $1 ]; then
        echo "...found $1 directory"
      else 
        echo "...found $1 file"
      fi 
    fi 
  else
    echo "...can not find $1"
    echo "Enter \"c\" to continue running diagnostics"
    echo "            any other values exits the run"
    read continueRun
    if [ "x$continueRun" == "xc" ]; then
      if [ $verbose == 1 ]; then
        echo "Countinuing to run diagnostic without $1"
      fi 
    else
      echo "Exited Broadcom diagnostic run."
      exit 1
    fi
  fi
}

diagDrvrMake()
{
  if [ $reqDrvrsKeep == 1 ]; then
    return; 
  fi
  
  tmpDrvrTar=drvr.tar
  tmpDrvrTarZip=${tmpDrvrTar}.gz
  drvrName=${drvrBaseName}-${drvrVersion}
  drvrTarZip=${drvrName}.tar.gz
  
  checkExists $drvrVersion;
  cd $drvrVersion
  checkExists $drvrTarZip
  cp $drvrTarZip $tmpDrvrTarZip
  gunzip -f $tmpDrvrTarZip
  rm -rf $drvrName
  tar -xf $tmpDrvrTar
  rm -rf $tmpDrvrTar
  checkExists $drvrName
  cd ${drvrName}/src
  make
  unameStr=`uname -r`
  kernelVer=${unameStr:0:3}
  if [ $kernelVer == "2.6" ]; then
    drvrImg=${drvrBaseName}.ko
  else
    drvrImg=${drvrBaseName}.o
  fi
  checkExists $drvrImg
  insmod $drvrImg
  echo "...insmod $drvrImg"
  sleep 5
}

diagDrvrIfUpDown()
{
  eths=`ifconfig -a | grep eth | awk '{print $1}'`
  for eth in $eths; do
    ethStr=`ethtool -i $eth`
    if [ x${ethStr:0:6} == "xdriver" ]; then
      ethStr=`echo $ethStr | awk '{ print $2 }'`

      count=0
      for drvrHere in ${drvrPresent[@]}; do
        if [ $ethStr == ${drvrNames[$count]} ]; then
          if [ $1 == "up" ]; then
            ifconfig $eth up
            echo "...ifconfig $eth up"
          elif [ $1 == "down" ]; then
            ifconfig $eth down
            echo "...ifconfig $eth down"
          else
            echo "...woh program error with diagDrvrIfUpDown"
            exit 1
          fi
        fi
        count=$(( $count + 1 ))
      done      
    else
      echo "...could not get driver information for $eth"
    fi
  done
  
}

diagRun()
{
  cd $dirStart
  machine=`uname -m`
  case $machine in
    ppc64)    ./b57ldiagp;;
    i386)     ./b57ldiagi;;
    i486)     ./b57ldiagi;;
    i586)     ./b57ldiagi;;
    i686)     ./b57ldiagi;;
    x86_64)   ./b57ldiagi64;;
    *)        echo "Unknow machine architecture - $machine"; exit 1;;
  esac
}

drvrNames=(tg4 tg3 tg3d bcm5700 bcm57diag)
drvrPresent=(0 0 0)
drvrsPresent=0
drvrVersion="9.5.9"
drvrBaseName="bcm57diag"
dirStart=$PWD

drvrsPresent;
drvrsRm;
diagDrvrMake;
diagDrvrIfUpDown up;
diagRun;
reqDrvrsRm=1
drvrsPresent;
drvrsRm;
