#!/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 17.5.1f0
#
#  (C) Copyright 2004-2015,
#  National Instruments Corporation.
#  All Rights reserved.
#

# readlink "$0" can return a relative symlink
# in that case, we need to tread carefully when retrieving the directory
# for the target of the symlink
# __TMPSELF__ is the path of the file (if not a symlink), or the target
# of a symlink, which may be a relative or absolute path
__TMPSELF__=$(if [ -L "$0" ]; then readlink "$0"; else echo "$0"; fi;)
__SELF__=$(basename ${__TMPSELF__})
# In case a symlink was executed, and the symlink target is a relative
# path, we change to the directory that the path is relative to
# the directory containing the symlink), then change to the directory
# containing the symlink target.
__DIR__="$(cd "$(dirname "$0")" && cd "$(dirname "${__TMPSELF__}")" && pwd)"

# Configure logging
LOG_MSG_TAG="nikal"

# Get useful functions to help with install
if [[ -e /usr/share/nikal/installerUtility.sh ]]; then
   source /usr/share/nikal/installerUtility.sh
elif [[ -e /usr/local/natinst/nikal/bin/installerUtility.sh ]]; then
   source /usr/local/natinst/nikal/bin/installerUtility.sh
elif [[ -e "${__DIR__}/../../bin/installerUtility.sh" ]]; then
   source "${__DIR__}/../../bin/installerUtility.sh"
elif [[ -e "${__DIR__}/../bin/installerUtility.sh" ]]; then
   source "${__DIR__}/../bin/installerUtility.sh"
elif [[ -e "${__DIR__}/installerUtility.sh" ]]; then
   source "${__DIR__}/installerUtility.sh"
else
   echo "Error opening the NI-KAL driver utility library." >&2
   exit 1
fi

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

function parseArguments() {
   while [ "$1" != "" ]
   do
      case $1 in
         --no-prompt)
            promptUser=1
            ;;
         --fast)
            fastInstall=0
            ;;
         --linuxrt)
            # the linuxrt flag is deprecated
            ;;
         -*)
            error "Unrecognized Option: $1"
            echoUsage
            exit 1
            ;;
         *)
            if [ "${KERNELTARGET}" == "" ]; then
               KERNELTARGET=$1
            else
               error "Kernel target already set: ${KERNELTARGET} Cannot accept another kernel target: $1"
               echoUsage
               exit 1
            fi
            ;;
      esac
      shift
   done
}

#
# Globals
#
# Boolean flags, 0 means on/true/yes/enabled, 1 means off/false/no/disabled
promptUser=0
fastInstall=1
KERNELTARGET=
nikalDir=
headersDir=
kernelmodDir=
kernelVersion=

parseArguments $*

# must be root
if ! isRoot; then
   error "Please run $0 as root"
   exit 1
fi

if ! nikalEnableKernelVersioning; then
   error "Error while preparing the kernel module versioning environment."
   exit 1
fi

# This is needed for the ./configure script, when it gets run
if ! nikalGetRequestedKernelVersion; then
   error "Error identifying kernel version to build against."
   if [ "${promptUser}" = "0" ]; then
      echoUsage
   fi
   exit 1
fi

# This is needed for the ./configure script, when it gets run
if ! withVersioning nikalGetRequestedKernelSourcesDir; then
   error "Error locating kernel sources for the requested kernel version (${kernelVersion})."
   if [ "${promptUser}" = "0" ]; then
      echoUsage
   fi
   exit 1
fi

# This is needed for the ./configure script, when it gets run
if ! withVersioning nikalGetRequestedKernelModulesDir; then
   error "Error locating kernel module installation directory for the requested kernel version (${kernelVersion})."
   if [ "${promptUser}" = "0" ]; then
      echoUsage
   fi
   exit 1
fi

if [ -f "${versioning_script}" ]; then
   cleanup_versioning_env > /dev/null 2>&1
fi

if ! nikalGetDirs; then
   error "Unable to locate the installed location for NI-KAL."
   exit 1
fi

# Global update flag
NIKALKERNELINST_OPTS=" -g"

if [ "${promptUser}" = "0" ]; then
   NIKALKERNELINST_OPTS+=" --prompt"
fi

if [ "${fastInstall}" = "0" ]; then
   NIKALKERNELINST_OPTS+=" -f"
fi

if ! KERNELTARGET="${kernelVersion}" KERNELHEADERS="${headersDir}" $nikalDir/bin/nikalKernelInstaller.sh ${NIKALKERNELINST_OPTS}; then
   error "Update of National Instruments drivers failed."
   exit 1
fi

exit 0
