#!/bin/sh

#
# File: sep5
#
# Description: script to load sep5 driver at boot time
#
# Version: 1.5
#
# Copyright(C) 2008-2018 Intel Corporation.  All Rights Reserved.
# 
#     This file is part of SEP Development Kit
# 
#     SEP Development Kit is free software; you can redistribute it
#     and/or modify it under the terms of the GNU General Public License
#     version 2 as published by the Free Software Foundation.
# 
#     SEP Development Kit 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 SEP Development Kit; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# 
#     As a special exception, you may use this file as part of a free software
#     library without restriction.  Specifically, if other files instantiate
#     templates or use macros or inline functions from this file, or you compile
#     this file and link it with other files to produce an executable, this
#     file does not by itself cause the resulting executable to be covered by
#     the GNU General Public License.  This exception does not however
#     invalidate any other reasons why the executable file might be covered by
#     the GNU General Public License.
#

#
# chkconfig: 2345 30 40
#
### BEGIN INIT INFO
# Provides: sep5
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0
# Short-Description: loads/unloads the sep5 driver at boot/shutdown time
# Description: loads/unloads the sep5 driver at boot/shutdown time
### END INIT INFO
# name of the driver

DRIVER_NAME=sep5

# location where the pre-built drivers are installed

DRIVER_DIR=/opt/intel/vtune_profiler_2020.3.0.612611/sepdk/src/.

# name of driver load/unload and build scripts

DRIVER_NOT_FOUND_ERROR_CODE=235

INSMOD_SCRIPT=insmod-sep

RMMOD_SCRIPT=rmmod-sep

BUILD_SCRIPT=build-driver

BUILD_OPTIONS="--make-command=/bin/make --c-compiler=/bin/gcc  -ni"


# source the function library, if it exists

[ -r /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions

# check whether directories and scripts are available

if [ ! -d ${DRIVER_DIR} ] ; then
  echo "Unable to access the sep5 driver directory \"${DRIVER_DIR}\" !"
  exit 101
fi

if [ ! -f ${DRIVER_DIR}/${INSMOD_SCRIPT} ] ; then
  echo "The sep5 load script \"${DRIVER_DIR}/${INSMOD_SCRIPT}\" does not exist!"
  exit 102
fi

if [ ! -f ${DRIVER_DIR}/${RMMOD_SCRIPT} ] ; then
  echo "The sep5 unload script \"${DRIVER_DIR}/${RMMOD_SCRIPT}\" does not exist!"
  exit 103
fi

check_build_script() {
    if [ ! -f ${DRIVER_DIR}/${BUILD_SCRIPT} ] ; then
        echo "The sep5 build script \"${DRIVER_DIR}/${BUILD_SCRIPT}\" does not exist!"
        exit 106
    fi
}

# define function for drivers rebuild

build_driver() {
    check_build_script
    (cd ${DRIVER_DIR} && ./${BUILD_SCRIPT} ${BUILD_OPTIONS})
    RETVAL=$?
    return $RETVAL
}

# define function to insmod the driver

insmod() {
    (cd ${DRIVER_DIR} && ./${INSMOD_SCRIPT} -g vtune -p 660 )
    return $?
}

# define function to load the driver

start() {
    echo "Loading the sep5 driver: "
    insmod
    RETVAL=$?

    if [ ${RETVAL} -ne 0 ]; then
        if [ ${RETVAL} -eq ${DRIVER_NOT_FOUND_ERROR_CODE} ]; then
            build_driver
            insmod
            RETVAL=$?
        else
            if [ -f ${DRIVER_DIR}/${BUILD_SCRIPT} ] ; then
                DRIVER_FILENAME=`${DRIVER_DIR}/${BUILD_SCRIPT} --print-driver-name`
            else
                DRIVER_FILENAME=non_existent_file_name
            fi
            if ! [ -f ${DRIVER_DIR}/${DRIVER_FILENAME} ]; then
                echo "Driver for current kernel is not found. Trying to build driver..."
                build_driver
                insmod
                RETVAL=$?
            fi
        fi
    fi

    return $RETVAL
}

# define function to unload the driver

stop() {
    echo "Unloading sep5 driver: "
    (cd ${DRIVER_DIR} && ./${RMMOD_SCRIPT})
    RETVAL=$?
    return $RETVAL
}

# define function to query whether driver is loaded

status() {
    (cd ${DRIVER_DIR} && ./${INSMOD_SCRIPT} -q)
    ERR=$?
    if [ $ERR -eq 0 ] ; then
      RETVAL=0
    else
      RETVAL=3
    fi
    return $RETVAL
}

# parse command-line options and execute

RETVAL=0

case "$1" in
	start)
	    start
	    ;;
	stop)
	    stop
	    ;;
	restart)
	    stop
	    start
	    ;;
	status)
	    status
	    ;;
	*)
	    echo "Usage: $0 {start|stop|restart|status}"
	    exit 1
esac

exit $RETVAL
