#!/bin/bash
#
#  updateNIDrivers <kernelVersion>
#  script to update installed NI Drivers for the currently running kernel. Not all
#  NI Drivers are supported by this script.  This script will print out which
#  drivers have been updated.
#  kernelVersion is an optional argument so you can compile NI drivers for a
#     kernel version other then the one currently running.  If this is not
#     provided NI drivers will be compiled for the currently running kernel.
#
#  version 14.0.0b2
#
#   Copyright 2004-2005,
#  National Instruments Corporation.
#  All Rights reserved.
#
#  originated:  24 June 2004
#

nikalDir=`cat /etc/natinst/nikal/nikal.dir`
. $nikalDir/bin/installerUtility.sh
statusFail=1
statusSuccess=0
kFalse=0
kTrue=1

echoUsage()
{
   echo "usage: $0 <--no-prompt> <kernelVersion>"
   echo "   kernelVersion"
   echo "      Specify the version of a kernel installed on the system."
   echo "      A /lib/modules/<kernelVersion> directory should exist for each"
   echo "      kernel installed. The currently running kernel is the default"
   echo "      for $0 if no argument is provided."
   echo "      Run 'uname -r' at the command prompt to find the version of the"
   echo "      currently running kernel."
   echo "   --no-prompt"
   echo "      runs in no prompt mode.  Useful if calling this script from"
   echo "      another script.  Messages are still printed to the screen and"
   echo "      the return value can be checked for status."
}

parseArguments()
{
   promptUser=$kTrue

   while [ "$1" != "" ]
   do
      case $1 in
         --no-prompt)
            promptUser=$kFalse
            ;;
         --linuxrt)
            installType="--linuxrt"
            ;;
         *)
            if [ "-" = $(echo $1|cut -c -1) ]
            then
               echo "Unrecognized Option: $1"
               echoUsage
               exit $statusFail
            elif [ "" != "$kernelTarget" ]
            then
               echo "Received kernel target, $kernelTarget, already. Cannot accept another kernel target: $1"
               echoUsage
               exit $statusFail
            else
               if [ ! -d /lib/modules/$1 ]; then
                  echo "error: /lib/modules/$1 not found!"
                  echoUsage
                  exit $statusFail
               else
                  kernelTarget=$1

                  # The ./configure script below is expecting this
                  export KERNELTARGET=$kernelTarget
                  if [ -z "$KERNELHEADERS" ]; then
                     export KERNELHEADERS=/lib/modules/$KERNELTARGET/source/
                  fi
               fi
            fi
            ;;
      esac
      shift
   done
}

# must be root
if [ `id -ur` != 0 ]; then
   echo "Please run $0 as root"
   echoUsage
   exit $statusFail
fi

parseArguments $*

# Unload NI-KAL if its already loaded
if [ "`$UNAME -r`" = "$kernelTarget" ]; then
   echo -n "Unloading NI-KAL (nikal): "

   recursiveUnloadKernelDriver nikal

   echo "done"
fi

if [ -n "$kernelTarget" ]; then
   modulePath=/lib/modules/$kernelTarget/kernel/natinst
   nikalPath=`find $modulePath -name nikal.ko`
fi

# Remove KAL from /lib/modules/... and update module db
if [ -n "$nikalPath" ]; then
   echo -n "Removing NI-KAL (nikal): "

   $RM -f $nikalPath

   if [ "$installType" != "--linuxrt" ]; then
      /sbin/depmod -a $kernelTarget >/dev/null 2>&1
   fi

   echo "done"
fi

# Do a make clean in KAL's directory and get it set up to build
cd $nikalDir/src/nikal
./configure
make clean

# make install will put the newly-built nikal.ko in the /lib/modules/.../ dir
echo "Installing NI-KAL:"
make install >/tmp/nikalMakeOutput 2>&1

if [ "$?" = "0" ]; then
   echo " NI-KAL successfully updated."
   rm -f /tmp/nikalMakeOutput
else
   echo " NI-KAL update failed."
   echo " "
   cat /tmp/nikalMakeOutput
   echo " "
   echo "ERROR: make of nikal kernel module failed, not installing kernel module."
   echo "   updateNIDrivers should be called again after fixing the problem."
   echo "   Logging failure..."
   /usr/local/bin/niSystemReport >/tmp/niSystemReport.out 2>&1
   gzip -f /tmp/niSystemReport.out
   echo "   Include the file /tmp/niSystemReport.out.gz when contacting"
   echo "   National Instruments for support."
   echo " "
   exit $statusFail
fi

# Attempt to load it if its configured for the current kernel
if [ "`$UNAME -r`" = "$kernelTarget" ]; then
   echo -n "Loading NI-KAL (nikal): "

   if /sbin/modprobe nikal >/tmp/nikalMakeOutput 2>&1 ; then
      echo "done"
   else
      echo "Could not load nikal."
      exit 1
   fi
fi


if ! $nikalDir/bin/nikalKernelInstaller.sh -g $installType; then
   echo "Update of National Instruments drivers failed."
   exit $statusFail
fi

if configuredForCurrentKernel; then
   if [ $promptUser -eq $kTrue ]; then
      echo "Rebooting is required to ensure that National Instruments drivers"
      echo "have been successfully updated."
      while :
      do
         echo -n "Would you like to reboot now? [yes|no] "
         read rebootConfirm
         if [ "$rebootConfirm" != "yes" ] && [ "$rebootConfirm" != "no" ]; then
            echo "Please input yes or no."
         else
            break
         fi
      done
      if [ "$rebootConfirm" = "yes" ]; then
         echo "rebooting"
         reboot
         exit $statusSuccess
      fi
      echo "Please reboot manually before attempting to use your NI drivers and products."
   else
      echo "Rebooting may be required to ensure that National Instruments drivers"
      echo "have been successfully updated."
   fi
else
   echo "National Instruments kernel drivers have been successfully installed."
   echo "You can now use your NI products when you boot into kernel ${KERNELTARGET}."
fi

exit $statusSuccess
