#!/bin/sh
#
#
# Copyright (C) 2008 Intel Corporation
#
# This software and the related documents are Intel copyrighted materials, and your use of them
# is governed by the express license under which they were provided to you ("License"). Unless
# the License provides otherwise, you may not use, modify, copy, publish, distribute, disclose
# or transmit this software or the related documents without Intel's prior written permission.
#
# This software and the related documents are provided as is, with no express or implied
# warranties, other than those that are expressly stated in the License.
#

#
# File: build-driver
#
# Description: script to build the SEP and PAX drivers in FreeBSD
#
# Version: 1.0
#

# 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"
TR="tr"

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

# 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` ;
  ret_val=$?
  if [ ${ret_val} -ne 0 ] ; 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 -----------------------------------

# basic name of driver
SEP_DRIVER_NAME=sep
PAX_DRIVER_NAME=pax

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

SCRIPT=$0
SCRIPT_ARGS="$*"
SCRIPT_DIR=`dirname "$SCRIPT"`
PLATFORM=`${UNAME} -m`
KERNEL_VERSION=`${UNAME} -r`
MACHINE_TYPE=`${UNAME} -m`
DRIVER_DIRECTORY=$PWD
SEP_DIR="sep"
PAX_DIR="pax"
DRIVER_SOURCE_DIRECTORY=$PWD

# ------------------------------ 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 "    --kernel-src-dir=path"
  echo "      \"path\" directory of the configured kernel source tree;"
  echo "      default kernel src path (/usr/src) will be used"
  echo "      if the user specified path is not valid"
  echo ""
  echo "    --c-compiler=c_compiler"
  echo "      \"c_compiler\" is the C compiler used to compile the kernel;"
  echo "      this defaults to \"cc\""
  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 ""
  exit 0
}

get_first() {
  echo $1
}

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
}

exit_if_wrong_kernel_path()
{
  if [ $use_kernel_src_dir -eq 1 ] ; then
    echo ""
    echo "ERROR: Improper Linux kernel source directory \"$kernel_src_dir\" is specified ... exiting."
    echo ""
    exit 254
  fi
}


non_interactive=0
print_driver_name=0
use_install_dir=0
exit_if_driver_exists=0
non_verbose=0
use_kernel_src_dir=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
       ;;
    --install-dir=*)
       driver_install_dir=`echo $1 | sed s?^--install-dir=??g`
       use_install_dir=1
       ;;
    --kernel-src-dir=*)
       kernel_src_dir=`echo $1 | sed s?^--kernel-src-dir=??g`
       use_kernel_src_dir=1
       ;;
    --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`
       ;;
    --exit-if-driver-exists)
       exit_if_driver_exists=1
       ;;
    --non-verbose)
       non_verbose=1
       ;;
    *)
       echo ""
       echo "Invalid option: \"$1\""
       print_usage_and_exit
       ;;
  esac
  shift
done

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

if [ -z "${c_compiler}" ] ; then
  c_compiler=cc
else
  exit_if_file_inaccessible $(get_first $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 check if devtoolset is installed

check_devtoolset_presence()
{
  if [ ! -d ${DEFAULT_DEVTOOLSET_BASE} ] ; then
    return
  fi

  DEVTOOLSET=`ls ${DEFAULT_DEVTOOLSET_BASE} | ${GREP} "devtoolset" | ${TR} '\n' ' ' | ${CUT} -d ' ' -f1`
  if [ -n "${DEVTOOLSET}" ] ; then
    devtoolset_installed="YES"
  fi
  DEFAULT_DEVTOOLSET_BIN_PATH="${DEFAULT_DEVTOOLSET_BASE}/${DEVTOOLSET}/root/usr/bin"

  # check if gcc and make exists
  if [ ! -f "${DEFAULT_DEVTOOLSET_BIN_PATH}/gcc" -o ! -f "${DEFAULT_DEVTOOLSET_BIN_PATH}/make" ] ; then
    devtoolset_installed="NO"
  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 and platform is supported

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

unset ARCH

if [ "${PLATFORM}" = "amd64" ] ; 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

# 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

EXT="ko"

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

SEP_DRIVER_FILENAME=${SEP_DRIVER_NAME}.${EXT}
PAX_DRIVER_FILENAME=${PAX_DRIVER_NAME}.${EXT}

if [ $print_driver_name -eq 1 ] ; then
  echo "${SEP_DRIVER_FILENAME}"
  echo "${PAX_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

# search heuristic for determining default kernel source directory
validity_kernel_src=0
DEFAULT_KERNEL_SRC_DIR=/usr/src
if [ $use_kernel_src_dir -eq 1 ] ; then
  if [ -d ${kernel_src_dir} ] ; then
    if [ -d ${kernel_src_dir}/Makefile ] ; then
      if [ -d ${kernel_src_dir}/bin ] ; then
        if [ -d ${kernel_src_dir}/include ] ; then
          if [ -d ${kernel_src_dir}/sbin ] ; then
            if [ -d ${kernel_src_dir}/sys ] ; then
              if [ -d ${kernel_src_dir}/tools ] ; then
                if [ -d ${kernel_src_dir}/usr.bin ] ; then
                  validity_kernel_src=1
                fi
              fi
            fi
          fi
        fi
      fi
    fi
  fi

  if [ $validity_kernel_src -eq 0 ] ; then
    echo "Invalid kernel path was specified, using default kernel path: " ${DEFAULT_KERNEL_SRC_DIR}
  else
    DEFAULT_KERNEL_SRC_DIR=${kernel_src_dir}
  fi
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/${SEP_DIR}/${SEP_DRIVER_FILENAME} ] ; then
    echo ""
    echo "Found pre-built driver: $driver_install_dir/${SEP_DIR}/${SEP_DRIVER_FILENAME}"
    echo ""
    exit 0
  elif [ -r $driver_install_dir/${PAX_DIR}/${PAX_DRIVER_FILENAME} ] ; then
    echo ""
    echo "Found pre-built driver: $driver_install_dir/${PAX_DIR}/${PAX_DRIVER_FILENAME}"
    echo ""
    exit 0
  else
    show_preamble
    echo "Pre-built driver \"${SEP_DRIVER_FILENAME}\" and \"${PAX_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="$c_compiler"
CC_FILE=`${WHICH} $(get_first $c_compiler) 2>/dev/null`
if [ -z "${CC_FILE}" ] ; then
  CURRENT_CC=$c_compiler
fi
if [ $non_verbose -eq 0 -o $non_interactive -eq 0 ] ; then
  echo -n "C compiler to use: [ ${CC_FILE} ] "
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
CC_FILE=`${WHICH} $(get_first ${NEW_CC}) 2>/dev/null`
if [ -z "${CC_FILE}" -o -d "${CC_FILE}" ] ; then
  echo "ERROR: invalid or inaccessible C compiler \"${NEW_CC}\" !"
  repeat_or_exit 255
fi

export CC="${CC_FILE}"

# 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

if [ $non_verbose -eq 0 -o $non_interactive -eq 0 ] ; then
  echo -n "Kernel source directory: [ ${DEFAULT_KERNEL_SRC_DIR} ] "
  if [ $non_verbose -eq 0 ] ; then
    echo ""
  fi
fi

# make the driver

make_args="KERNEL_SRC_DIR=$KERNEL_SRC_DIR $make_args"

if [ -x "${MAKE}" ] ; then
  ${MAKE} CC="$CC" MAKE=$MAKE $make_args clean
  ${MAKE} CC="$CC" MAKE=$MAKE $make_args
  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

echo "${DRIVER_DIRECTORY}"
if [ $use_install_dir -eq 1 ] ; then
  # install the previously built driver to specified location
  make_args="$make_args INSTALL=${DRIVER_DIRECTORY} install"
  ${MAKE} $make_args
fi


ERR=$?

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

# all done

echo ""
exit 0
