#!/bin/sh

# Description: Script to build the power driver
# Version: 1.0

# ***********************************************************************************************

 # This file is provided under a dual BSD/GPLv2 license.  When using or 
 # redistributing this file, you may do so under either license.

  # GPL LICENSE SUMMARY

  # Copyright(c) 2011 Intel Corporation. All rights reserved.

  # This program is free software; you can redistribute it and/or modify 
  # it under the terms of version 2 of the GNU General Public License as
  # published by the Free Software Foundation.

  # This program is distributed in the hope that it will be useful, but 
  # WITHOUT ANY WARRANTY; without even the implied warranty of 
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  # General Public License for more details.

  # You should have received a copy of the GNU General Public License 
  # along with this program; if not, write to the Free Software 
  # Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  # The full GNU General Public License is included in this distribution 
  # in the file called LICENSE.GPL.

  # Contact Information:
  # Gautam Upadhyaya <gautam.upadhyaya@intel.com>
  # 1906 Fox Drive, Champaign, IL - 61820, USA

  # BSD LICENSE 

  # Copyright(c) 2011 Intel Corporation. All rights reserved.
  # All rights reserved.

  # Redistribution and use in source and binary forms, with or without 
  # modification, are permitted provided that the following conditions 
  # are met:

  #   * Redistributions of source code must retain the above copyright 
  #     notice, this list of conditions and the following disclaimer.
  #   * Redistributions in binary form must reproduce the above copyright 
  #     notice, this list of conditions and the following disclaimer in 
  #     the documentation and/or other materials provided with the 
  #     distribution.
  #   * Neither the name of Intel Corporation nor the names of its 
  #     contributors may be used to endorse or promote products derived 
  #     from this software without specific prior written permission.

  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
  # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
  # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
  # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  # ***********************************************************************************************

# set the path to include "standard" locations so commands below can be found

PATH="/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/local/gnu/bin:.:"${PATH}""
export PATH

# ------------------------------ COMMANDS ------------------------------------

CUT="cut"
ECHO="echo"
GREP="grep"
HEAD="head"
SED="sed"
UNAME="uname"
WHICH="which"

COMMANDS_TO_CHECK="${CUT} ${GREP} ${HEAD} ${UNAME}"

# if any of the COMMANDS_TO_CHECK are not executable, then exit script

OK="true"
for c in ${COMMANDS_TO_CHECK} ; do
  CMD=`${WHICH} $c 2>&1` ;
  if [ -z "${CMD}" ] ; then
    OK="false"
    echo "ERROR: unable to find command \"$c\" !"
  fi
done
if [ ${OK} != "true" ] ; then
  echo "Please add the above commands to your PATH and re-run the script ... exiting."
  exit 255
fi

# ------------------------------ CONSTANTS -----------------------------------

# base driver name and version
DRIVER_BASE=apwr
DRIVER_MAJOR=3
DRIVER_MINOR=1
# basic name of driver
DRIVER_NAME=${DRIVER_BASE}${DRIVER_MAJOR}_${DRIVER_MINOR}

# ------------------------------ VARIABLES -----------------------------------

SCRIPT=$0
SCRIPT_ARGS="$*"
SCRIPT_DIR=`dirname "$SCRIPT"`
PLATFORM=`${UNAME} -m`
KERNEL_VERSION=`${UNAME} -r`
MACHINE_TYPE=`${UNAME} -m`
DRIVER_DIRECTORY=$PWD
DRIVER_SOURCE_DIRECTORY=$PWD
PER_USER_MODE="NO"

# ------------------------------ FUNCTIONS -----------------------------------

# function to show usage and exit

print_usage_and_exit()
{
  echo ""
  echo "Usage: $0 [ options ]"
  echo ""
  echo " where \"options\" are the following:"
  echo ""
  echo "    --help | -h"
  echo "      prints out usage"
  echo ""
  echo "    --non-interactive | -ni"
  echo "      attempts to automatically build the driver using"
  echo "      default values without prompting for user input"
  echo ""
  echo "    --print-driver-name"
  echo "      returns the name of the driver that would be built"
  echo "      based on the current running kernel"
  echo ""
  echo "    --install-dir=path"
  echo "      \"path\" is an existing, writable directory where the"
  echo "      driver will be copied after it is successfully built;"
  echo "      this defaults to \"${DRIVER_DIRECTORY}\""
  echo ""
  echo "    --print-kernel-checksum"
  echo "      returns kernel checksum information for running kernel;"
  echo "      this can be used to verify driver/kernel compatibility"
  echo ""
  echo "    --kernel-file=file"
  echo "      \"file\" is pathname of kernel file for currently"
  echo "      running kernel; used for comparing kernel checksums;"
  echo "      this can be either compressed (vmlinuz) or"
  echo "      uncompressed (vmlinux) kernel file, but must be the"
  echo "      kernel that was booted; this defaults to \"/boot/vmlinuz-${KERNEL_VERSION}\""
  echo ""
  echo "    --kernel-version=version"
  echo "      \"version\" is version string of kernel that should"
  echo "      be used for checksum or for building the driver;"
  echo "      this defaults to \"${KERNEL_VERSION}\""
  echo ""
  echo "    --kernel-src-dir=path"
  echo "      \"path\" directory of the configured kernel source tree;"
  echo "      several paths are searched to find a suitable default,"
  echo "      including \"/lib/modules/${KERNEL_VERSION}/{source,build}\","
  echo "      \"/usr/src/linux-${KERNEL_VERSION}\", and \"/usr/src/linux\""
  echo ""
  echo "    --c-compiler=c_compiler"
  echo "      \"c_compiler\" is the C compiler used to compile the kernel;"
  echo "      this defaults to \"gcc\""
  echo ""
  echo "    --make-command=make_command"
  echo "      \"make_command\" is the make command used to build the kernel;"
  echo "      this defaults to \"make\""
  echo ""
  echo "    --make-args=args"
  echo "      arguments to pass to make command (e.g., \"-n\" causes all make"
  echo "      commands to be shown but does not actually carry them out,"
  echo "      \"V=1\" shows the detailed build commands, etc.)"
  echo ""
  echo "    --exit-if-driver-exists"
  echo "      exits if a pre-built driver for the current running"
  echo "      kernel exists in the driver install directory"
  echo ""
  echo "    --non-verbose"
  echo "      decrease the amount of (helpful) messages"
  echo ""
  echo "    --config-file=file"
  echo "      \"file\" is pathname of configuration file to read"
  echo "      default VARIABLE=VALUE entries from; NOTE: order of"
  echo "      this option (relative to the other options) matters"
  echo ""
  echo "    --per-user | -pu"
  echo "      build the driver in secure sampling mode"
  echo ""
  echo ""
  exit 0
}

# print kernel checksum info for future comparision
# check for kernel file in kernel source tree
#  KERNEL_FILE=${KERNEL_SRC_DIR}/vmlinux
#  KERNEL_FILE=`cat /proc/cmdline | tr ' ' '\n' | grep BOOT_FILE | cut -d '=' -f 2`

print_kernel_checksum()
{
  KERNEL_FILE=$1
  MD5SUM=`${WHICH} ${MD5SUM:-md5sum} 2>&1`

  if [ -x "${MD5SUM}" ] ; then
    if [ -r "${KERNEL_FILE}" ] ; then
      CHECKSUM=`${MD5SUM} ${KERNEL_FILE}`
      echo "${CHECKSUM}  ${kernel_version}  ${MACHINE_TYPE}"
    fi
  fi
}

read_config_file()
{
  CONFIG_FILE=$1
  if [ -r "${CONFIG_FILE}" ] ; then
    driver_install_dir=`${GREP} "^VDK_INSTALL_DIR=" ${CONFIG_FILE} | ${SED} -e s^VDK_INSTALL_DIR=^^`
    c_compiler=`${GREP} "^VDK_C_COMPILER=" ${CONFIG_FILE} | ${SED} -e s^VDK_C_COMPILER=^^`
    make_command=`${GREP} "^VDK_MAKE_COMMAND=" ${CONFIG_FILE} | ${SED} -e s^VDK_MAKE_COMMAND=^^`
    kernel_src_dir=`${GREP} "^VDK_KERNEL_SRC_DIR=" ${CONFIG_FILE} | ${SED} -e s^VDK_KERNEL_SRC_DIR=^^`
  else
    if [ $non_verbose -eq 1 ] ; then
      echo ""
      echo "Warning: unable to access config file \"${CONFIG_FILE}\" ... option ignored ..."
    fi
  fi
}

exit_if_file_inaccessible()
{
  proposed_file=$1
  attr=${2:-f}
  # check for executable
  if [ "$attr" = "x" ] ; then
    file=`${WHICH} $proposed_file 2>&1`
    if [ ! -$attr "$file" ] ; then
      echo "ERROR: file \"$proposed_file\" either does not exist or is not an executable!"
      exit 111
    fi
  # otherwise assume regular file
  else
    if [ ! -$attr "$proposed_file" ] ; then
      echo "ERROR: \"$proposed_file\" is either not a file or is not accessible!"
      exit 111
    fi
  fi
}

exit_if_directory_inaccessible()
{
  dir=$1
  err=$2
  if [ ! -d "$dir" ] ; then
    echo "ERROR: \"$dir\" either does not exist or is not a directory!"
    exit $err
  fi
  if [ ! -r "$dir" ] ; then
    echo "ERROR: directory \"$dir\" is not accessible!"
    exit $err
  fi
}

non_interactive=0
print_driver_name=0
print_kernel_checksum=0
use_install_dir=0
build_kernel=2  #  0=no, 1=yes, 2=maybe
exit_if_driver_exists=0
non_verbose=0

while [ $# -gt 0 ] ; do
  case "$1" in
    --help | -h)
      print_usage_and_exit
      ;;
    --non-interactive | -ni)
       non_interactive=1
       ;;
    --print-driver-name)
       print_driver_name=1
       build_kernel=0
       ;;
    --install-dir=*)
       driver_install_dir=`echo $1 | sed s?^--install-dir=??g`
       use_install_dir=1
       ;;
    --kernel-file=*)
       kernel_file=`echo $1 | sed s?^--kernel-file=??g`
       ;;
    --kernel-version=*)
       kernel_version=`echo $1 | sed s?^--kernel-version=??g`
       ;;
    --kernel-src-dir=*)
       kernel_src_dir=`echo $1 | sed s?^--kernel-src-dir=??g`
       ;;
    --c-compiler=*)
       c_compiler=`echo $1 | sed s?^--c-compiler=??g`
       ;;
    --make-command=*)
       make_command=`echo $1 | sed s?^--make-command=??g`
       ;;
    --make-args=*)
       make_args=`echo $1 | sed s?^--make-args=??g`
       ;;
    --print-kernel-checksum)
       print_kernel_checksum=1
       build_kernel=0
       ;;
    --exit-if-driver-exists)
       exit_if_driver_exists=1
       ;;
    --non-verbose)
       non_verbose=1
       ;;
    --config-file=*)
       config_file=`echo $1 | sed s?^--config-file=??g`
       read_config_file $config_file
       ;;
    --per-user | -pu)
       PER_USER_MODE="YES"
       ;;
    *)
       echo ""
       echo "Invalid option: \"$1\""
       print_usage_and_exit
       ;;
  esac
  shift
done

if [ -z "${kernel_version}" ] ; then
  kernel_version=${KERNEL_VERSION}
fi

if [ -z "${kernel_src_dir}" ] ; then
  kernel_src_dir=/usr/src/linux-${kernel_version}
else
  exit_if_directory_inaccessible $kernel_src_dir 110
fi

if [ -z "${kernel_file}" ] ; then
  kernel_file=/boot/vmlinuz-${kernel_version}
else
  exit_if_file_inaccessible $kernel_file
fi

if [ -z "${c_compiler}" ] ; then
  c_compiler=gcc
else
  exit_if_file_inaccessible $c_compiler x
fi

if [ -z "${make_command}" ] ; then
  make_command=make
else
  exit_if_file_inaccessible $make_command x
fi

# function to describe default option

show_preamble()
{
  if [ $non_verbose -eq 0 ] ; then
    echo ""
    echo "Options in brackets \"[ ... ]\" indicate default values"
    echo "that will be used when only the ENTER key is pressed."
  fi
  echo ""
}

# function to return absolute path location (from script directory)

get_absolute_path()
{
  target_dir=$1
  if [ -d ${target_dir} ] ; then
    cd ${SCRIPT_DIR} -
    cd ${target_dir} -
    actual_dir=$PWD
    cd ${SCRIPT_DIR} -
    echo "${actual_dir}"
  else
    echo "${target_dir}"
  fi
}

# function to repeat this script or exit with error code

repeat_or_exit()
{
  EXIT_CODE=$1
  echo ""
  # for now, just exit with error
  exit ${EXIT_CODE}
  if [ $non_interactive -eq 1 ] ; then
    exit ${EXIT_CODE}
  fi
  echo -n "Retry building the driver? (yes/no) [Yes] "
  read YESNO
  if [ "${YESNO}" = "N" -o "${YESNO}" = "n" -o "${YESNO}" = "no" -o "${YESNO}" = "No" ] ; then
    echo ""
    exit ${EXIT_CODE}
  else
    exec ${SCRIPT} ${SCRIPT_ARGS}
  fi
  echo ""
}

# ----------------------------- PRE-CHECK ------------------------------------

# check if OS platform is supported

# if ARCH variable is set, unset it to avoid conflicts below
unset ARCH

if [ "${PLATFORM}" = "ia64" ] ; then
  ARCH="x64"
elif [ "${PLATFORM}" = "x86_64" ] ; then
  ARCH="x32_64"
elif [ "${PLATFORM}" = "i386" -o "${PLATFORM}" = "i486" -o "${PLATFORM}" = "i586" -o "${PLATFORM}" = "i686" ] ; then
  ARCH="x32"
else
  echo ""
  echo "ERROR: Unsupported platform \"${PLATFORM}\" ... exiting."
  echo ""
  exit 254
fi

# determine if using kernel 2.6 sources


KS_MAKEFILE=${kernel_src_dir}/Makefile
if [ -r ${KS_MAKEFILE} ] ; then
  KS_VERSION=`${GREP} "^VERSION" ${KS_MAKEFILE} | ${HEAD} -1 | ${SED} -e 's/ //g' | ${CUT} -d '=' -f 2`
  KS_PATCHLEVEL=`${GREP} "^PATCHLEVEL" ${KS_MAKEFILE} | ${HEAD} -1 | ${SED} -e 's/ //g' | ${CUT} -d '=' -f 2`
  KS_SUBLEVEL=`${GREP} "^SUBLEVEL" ${KS_MAKEFILE} | ${HEAD} -1 | ${SED} -e 's/ //g' | ${CUT} -d '=' -f 2`
  KERNEL_26X=`echo "${KS_VERSION}.${KS_PATCHLEVEL}.${KS_SUBLEVEL}" | ${GREP} ^2.6.`
else
  KERNEL_26X=`echo ${kernel_version} | ${GREP} ^2.6.`
fi

# GU: pwr driver also has sublevel requirements of >= 32 -- extract
# the 'sublevel' component of the kernel version number here. Note
# that we could have extracted it above, but we aren't
# guaranteed the 'if [-r ...]' rule (above) will match.
KERNEL_VERSION_VERSION=`uname -r | awk 'BEGIN{FS="[^0-9]"} {print $1}'`
KERNEL_VERSION_INTERFACE=`uname -r | awk 'BEGIN{FS="[^0-9]"} {print $2}'`
KERNEL_VERSION_OTHER=`uname -r | awk 'BEGIN{FS="[^0-9]"} {print $3}'`
MIN_KERNEL_VERSION=2 # kernel MUST be >= 2
MIN_KERNEL_INTERFACE=6 # if kernel is 2.X then interface MUST be >= 6
MIN_KERNEL_OTHER=32 # if kernel is 2.6.X then 'other' MUST be >= 32

# if not using kernel 2.6.x, then exit with error
# GU: MODIFIED -- if not using kernel 2.6.32 or greater then exit with error

is_compatible_kernel=0
if [ $KERNEL_VERSION_VERSION -gt $MIN_KERNEL_VERSION ]; then
    is_compatible_kernel=1
elif [ $KERNEL_VERSION_VERSION -eq $MIN_KERNEL_VERSION ] && [ $KERNEL_VERSION_INTERFACE -ge $MIN_KERNEL_INTERFACE ] && [ $KERNEL_VERSION_OTHER -ge $MIN_KERNEL_OTHER ]; then
    is_compatible_kernel=1
fi
#if [ -z "$KERNEL_26X" ] ; then
if [ $is_compatible_kernel -eq 0 ]; then
  echo ""
  echo "ERROR: Linux kernels prior to $MIN_KERNEL_VERSION.$MIN_KERNEL_INTERFACE.$MIN_KERNEL_OTHER are unsupported ... exiting."
  echo ""
  exit 254
fi

# print checksum and exit, if requested

if [ $print_kernel_checksum -eq 1 ] ; then
  print_kernel_checksum $kernel_file
  exit 0
fi

# check whether kernel is for UP or SMP

SMP=`${UNAME} -v | ${GREP} SMP`
if [ -z "${SMP}" ] ; then
  ARITY="up"
else
  ARITY="smp"
fi

# check driver file extension

if [ -n "${KERNEL_26X}" ] ; then
  EXT="ko"
else
  EXT="o"
fi

# name of the driver that will be built (see Makefile)

DRIVER_FILENAME=${DRIVER_NAME}-${ARCH}-${kernel_version}${ARITY}.${EXT}

if [ $print_driver_name -eq 1 ] ; then
  echo "${DRIVER_FILENAME}"
  exit 0
fi

# ----------------------- BUILD / INSTALL DRIVER -----------------------------

if [ -z "$driver_install_dir" ] ; then
  driver_install_dir=${DRIVER_DIRECTORY}
else
  exit_if_directory_inaccessible $driver_install_dir 101
fi

if [ -d $driver_install_dir ] ; then
  DRIVER_DIRECTORY=$driver_install_dir
fi

DRIVER_DIRECTORY=`get_absolute_path ${DRIVER_DIRECTORY}`

# if specifed, check whether pre-built driver exists and exit if it does

if [ $exit_if_driver_exists -eq 1 ] ; then
  if [ -r $driver_install_dir/${DRIVER_FILENAME} ] ; then
    echo ""
    echo "Found pre-built driver: $driver_install_dir/${DRIVER_FILENAME}"
    echo ""
    exit 0
  else
    show_preamble
    echo "Pre-built driver \"${DRIVER_FILENAME}\" was NOT found"
    echo "in directory \"$driver_install_dir\" ."
    echo ""
    echo -n "Proceed with building a driver for this kernel? (Yes/No) [Yes] "
    if [ $non_interactive -eq 1 ] ; then
      YESNO=y
    else
      read YESNO
    fi
    echo ""
    if [ "${YESNO}" = "N" -o "${YESNO}" = "n" -o "${YESNO}" = "no" -o "${YESNO}" = "No" ] ; then
      exit 100
    fi
  fi
else
  if [ $non_interactive -eq 0 ] ; then
    show_preamble
  fi
fi

# prompt for C compiler

OLD_CC=${CC}
NEW_CC=""
CURRENT_CC=`${WHICH} $c_compiler 2>&1`
if [ -z "${CURRENT_CC}" ] ; then
  CURRENT_CC=$c_compiler
fi
if [ $non_verbose -eq 0 -o $non_interactive -eq 0 ] ; then
  echo -n "C compiler to use: [ ${CURRENT_CC} ] "
fi
if [ $non_interactive -eq 0 ] ; then
  read NEW_CC
  echo ""
else
  if [ $non_verbose -eq 0 ] ; then
    echo ""
  fi
fi
if [ -z "${NEW_CC}" ] ; then
  NEW_CC=${CURRENT_CC}
fi
CHECK_CC=`${WHICH} "${NEW_CC}" 2>&1`
if [ -z ${CHECK_CC} -o -d ${CHECK_CC} ] ; then
  echo "ERROR: invalid or inaccessible C compiler \"${NEW_CC}\" !"
  repeat_or_exit 255
fi

export CC="${CHECK_CC}"

# prompt for make command

NEW_MAKE=""
CURRENT_MAKE=`${WHICH} $make_command 2>&1`
if [ -z "${CURRENT_MAKE}" ] ; then
  CURRENT_MAKE=$make_command
fi
if [ $non_verbose -eq 0 -o $non_interactive -eq 0 ] ; then
  echo -n "Make command to use: [ ${CURRENT_MAKE} ] "
fi
if [ $non_interactive -eq 0 ] ; then
  read NEW_MAKE
  echo ""
else
  if [ $non_verbose -eq 0 ] ; then
    echo ""
  fi
fi
if [ -z "${NEW_MAKE}" ] ; then
  NEW_MAKE=${CURRENT_MAKE}
fi
CHECK_MAKE=`${WHICH} "${NEW_MAKE}" 2>&1`
if [ -z ${CHECK_MAKE} -o -d ${CHECK_MAKE} ] ; then
  echo "ERROR: invalid or inaccessible make command \"${NEW_MAKE}\" !"
  repeat_or_exit 255
fi

export MAKE="${CHECK_MAKE}"

# ---------------------------------------------------------------------------

# prompt for kernel source directory

DEFAULT_KERNEL_SRC_DIR=${kernel_src_dir}

# check if kernel source directory contains "configured" kernel headers
# (e.g., at a minimum, "version.h" file and "asm" directory must exist);
# if not, then specify a bogus (non-existent) directory so subsequent
# search heuristics are used
if [ -r ${DEFAULT_KERNEL_SRC_DIR}/include/linux/version.h ] ; then
  if [ ! -d ${DEFAULT_KERNEL_SRC_DIR}/include/asm -o ! -L ${DEFAULT_KERNEL_SRC_DIR}/include/asm ] ; then
    DEFAULT_KERNEL_SRC_DIR=./bogus_directory$$
  fi
else
  DEFAULT_KERNEL_SRC_DIR=./bogus_directory$$
fi

# search heuristic for determining default kernel source directory
if [ ! -d ${DEFAULT_KERNEL_SRC_DIR} ] ; then
  DEFAULT_KERNEL_SRC_DIR=/lib/modules/${kernel_version}/build
  if [ ! -d ${DEFAULT_KERNEL_SRC_DIR} ] ; then
    DEFAULT_KERNEL_SRC_DIR=/lib/modules/${kernel_version}/source
    if [ ! -d ${DEFAULT_KERNEL_SRC_DIR} ] ; then
      if [ -n "$KERNEL_26X" ] ; then
        DEFAULT_KERNEL_SRC_DIR=/usr/src/linux-2.6
      else
        DEFAULT_KERNEL_SRC_DIR=/usr/src/linux-2.4
      fi
      if [ ! -d ${DEFAULT_KERNEL_SRC_DIR} ] ; then
        DEFAULT_KERNEL_SRC_DIR=/usr/src/linux
        if [ ! -d ${DEFAULT_KERNEL_SRC_DIR} ] ; then
          # punt ...
          DEFAULT_KERNEL_SRC_DIR=${kernel_src_dir}
        fi
      fi
    fi
  fi
fi

CURRENT_KERNEL_SRC_DIR=${KERNEL_SRC_DIR:-${DEFAULT_KERNEL_SRC_DIR}}

if [ $non_verbose -eq 0 -o $non_interactive -eq 0 ] ; then
  echo -n "Kernel source directory: [ ${CURRENT_KERNEL_SRC_DIR} ] "
fi
if [ $non_interactive -eq 0 ] ; then
  read KERNEL_SRC_DIR
else
  if [ $non_verbose -eq 0 ] ; then
    echo ""
  fi
fi
if [ -z "${KERNEL_SRC_DIR}" ] ; then
  KERNEL_SRC_DIR=${CURRENT_KERNEL_SRC_DIR}
fi
KERNEL_SRC_DIR=`get_absolute_path ${KERNEL_SRC_DIR}`
if [ ! -d ${KERNEL_SRC_DIR} ] || [ ! -x ${KERNEL_SRC_DIR} ] ; then
  echo "ERROR: invalid or inaccessible kernel source directory \"${KERNEL_SRC_DIR}\" !"
  repeat_or_exit 110
fi

# make the driver

# GU: update for "Red Hat" distros -- these distros back-port
# the tracepoint API from 2.6.38 into a custom 2.6.32 kernel.
# We need a (temporary) workaround for this. Do that here.

APWR_RED_HAT="0"

# check which distro
# taken from the 'boot-script'
if [ -e "/etc/issue" ]; then
    is_redhat=`cat /etc/issue | grep -i "red hat"`;
    if [ "$is_redhat" != "" ]; then
	APWR_RED_HAT=1 # HACK HACK HACK!!!
        echo "Using RedHat-specific hack for kernel version number..."
    fi
fi
if [ $APWR_RED_HAT -eq 0 ] && [ -e "/etc/redhat-release" ]; then
    is_redhat=`cat /etc/redhat-release| grep -i "red hat"`;
    if [ "$is_redhat" != "" ]; then
	APWR_RED_HAT=1 # HACK HACK HACK!!!
        echo "Using RedHat-specific hack for kernel version number..."
    fi
fi

WAKELOCK_SAMPLE="0"
wakelock_file=${KERNEL_BUILD_DIR}/source/include/trace/events/wakelock.h
if [ -f $wakelock_file ] ; then
  echo "\"$wakelock_file\" exists!"
  WAKELOCK_SAMPLE=1
else
  echo "\"$wakelock_file\" does NOT exist!"
fi

# GU: added 'APWR_KERNEL_VERSION' argument.
make_args="KERNEL_VERSION=$kernel_version KERNEL_SRC_DIR=$KERNEL_SRC_DIR PER_USER_MODE=$PER_USER_MODE WAKELOCK_SAMPLE=$WAKELOCK_SAMPLE APWR_RED_HAT=$APWR_RED_HAT $make_args"

if [ -x "${MAKE}" ] ; then
  ${MAKE} CC=$CC MAKE=$MAKE $make_args clean default
  ERR=$?
  if [ $ERR -ne 0 ] ; then
    repeat_or_exit 100
  fi
else
  echo "ERROR: unable to access make command \"${MAKE}\" !"
  repeat_or_exit 255
fi

echo ""

# where to install the driver once it is successfully built

if [ ! -w $driver_install_dir ] ; then
  echo "Warning: directory \"$driver_install_dir\" is not writable."
  echo ""
  driver_install_dir=.
  use_install_dir=0
fi

if [  $use_install_dir -eq 0 ] ; then
  echo -n "Directory to install ${DRIVER_NAME} driver: [ $driver_install_dir ] "
  if [ $non_interactive -eq 1 ] ; then
    NEW_DRIVER_DIRECTORY=$driver_install_dir
  else
    read NEW_DRIVER_DIRECTORY
  fi
  echo ""
  if [ -n "${NEW_DRIVER_DIRECTORY}" ] ; then
    driver_install_dir=${NEW_DRIVER_DIRECTORY}
  fi
  driver_install_dir=`get_absolute_path $driver_install_dir`
  if [ -d $driver_install_dir ] ; then
    if [ -w $driver_install_dir ] ; then
      DRIVER_DIRECTORY=$driver_install_dir
    else
      echo "Error: driver install directory \"$driver_install_dir\" is not writable!"
      repeat_or_exit 101
    fi
  else
    echo "Error: \"$driver_install_dir\" does not exist or is not a directory!"
    repeat_or_exit 101
  fi
fi

# install the previously built driver to specified location

${MAKE} $make_args INSTALL=${DRIVER_DIRECTORY} install

ERR=$?

if [ ${ERR} -ne 0 ] ; then
  repeat_or_exit 101
fi

# verify the driver exists, otherwise repeat or exit

#if [ ! -r ${DRIVER_DIRECTORY}/${DRIVER_FILENAME} ] ; then
#  repeat_or_exit 111  ???
#fi

# all done

echo ""
exit 0
