#!/bin/sh
# Copyright (c) 2015, Intel Corporation
# 
# 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.

# ---------------------------------------------
#  Function:    SetForegroundRed
#  Arguments:   None
#  Description: Sets foreground colour 
# ---------------------------------------------
SetForegroundRed()
{
   printf "\033[31;1m"
}

# ---------------------------------------------
#  Function:    SetForegroundGreen
#  Arguments:   None
#  Description: Sets foreground colour 
# ---------------------------------------------
SetForegroundGreen()
{
   printf "\033[32;1m"
}

# ---------------------------------------------
#  Function:    SetForegroundDefault
#  Arguments:   None
#  Description: Sets foreground colour 
# ---------------------------------------------
SetForegroundDefault()
{
   printf "\033[0m"
}


export DRIVER_PACKAGE_FILE_NAME=iqvfreebsd.tar
export DRIVER_FILE_NAME=iqvfreebsd.ko
export DRIVER_DESTINATION_DIR=/boot/kernel
export DRIVER_SOURCE_DIR=/usr/local/iqvfreebsd
export DRIVER_MAKEFILE_FILE_NAME=nalfreebsddriver.mak

if [ "$1" = "uninstall" ]; then 
     ###################################
    # if compiled driver exist
    # remove it
    if [ -e $DRIVER_DESTINATION_DIR/$DRIVER_FILE_NAME ]; then
        printf "Removing old driver file..."
        rm $DRIVER_DESTINATION_DIR/$DRIVER_FILE_NAME
        SetForegroundGreen
        printf "OK\n"
        SetForegroundDefault
    else 
        SetForegroundRed
        echo ".--------------------------------------------------------------------------------------."
        echo "| ERROR: iQV driver not installed, so cannot be removed!                                "
        echo "| File name: $DRIVER_PACKAGE_FILE_NAME                                                  "
        echo "'--------------------------------------------------------------------------------------'"
        SetForegroundDefault
    fi
    #
    ###################################
elif [ ! -e ./$DRIVER_PACKAGE_FILE_NAME ]; then
    SetForegroundRed
    echo ".--------------------------------------------------------------------------------------."
    echo "| ERROR: iQV freebsd package file not found!                                            "
    echo "| File name: $DRIVER_PACKAGE_FILE_NAME                                                  "
    echo "'--------------------------------------------------------------------------------------'"
    SetForegroundDefault
    exit 1
else 
    
    ###################################
    # if sources of driver directory 
    # exist, remove it
    if [ -d $DRIVER_SOURCE_DIR ]; then
        printf "Removing old driver sources..."
        rm -R $DRIVER_SOURCE_DIR
        SetForegroundGreen
        printf "OK\n"
        SetForegroundDefault
    fi
    #
    ###################################
    
    ###################################
    # if compiled driver exist
    # remove it
    if [ -e $DRIVER_SOURCE_DIR/$DRIVER_FILE_NAME ]; then
        printf "Removing old driver file..."
        rm $DRIVER_DESTINATION_DIR/$DRIVER_FILE_NAME
        SetForegroundGreen
        printf "OK\n"
        SetForegroundDefault
    fi
    #
    ###################################
    
    ###################################
    # Unzipping driver sources
    printf "Unzipping driver sources..."
    
    # create source path 
    mkdir -p $DRIVER_SOURCE_DIR
    
    # unzip 
    export SAVED_PATH="$PWD"
    cd $DRIVER_SOURCE_DIR
    tar xf "$SAVED_PATH"/$DRIVER_PACKAGE_FILE_NAME
    cd "$SAVED_PATH"
    
    # check if success
    if [ -e $DRIVER_SOURCE_DIR/$DRIVER_MAKEFILE_FILE_NAME ]; then
        SetForegroundGreen
        printf "OK\n"
        SetForegroundDefault
    else
        SetForegroundRed
        printf "ERROR\n"
        echo ".--------------------------------------------------------------------------------------."
        echo "| ERROR: Makefile file not found                                                        "
        echo "'--------------------------------------------------------------------------------------'"
        SetForegroundDefault
        exit 1
    fi
    #
    ###################################
    
    ###################################
    # Compile the driver
    if [ -e $DRIVER_SOURCE_DIR/$DRIVER_MAKEFILE_FILE_NAME ]; then
        echo "Compile the driver..."

        export SAVED_PATH="$PWD"
        cd $DRIVER_SOURCE_DIR
        
        make -f $DRIVER_MAKEFILE_FILE_NAME
        
        cd "$SAVED_PATH"
        
        if [ ! -e $DRIVER_SOURCE_DIR/$DRIVER_FILE_NAME ]; then
            SetForegroundRed
            echo ".--------------------------------------------------------------------------------------."
            echo "| ERROR: File $DRIVER_SOURCE_DIR/$DRIVER_FILE_NAME not found!                           "
            echo "'--------------------------------------------------------------------------------------'"
            SetForegroundDefault
            exit 1
        else
            if [ ! -d $DRIVER_DESTINATION_DIR ]; then
                mkdir -p $DRIVER_DESTINATION_DIR
            fi
            
            if [ ! -d $DRIVER_DESTINATION_DIR ]; then
                SetForegroundRed
                echo ".----------------------------------------------------------------------------------------."
                echo "| ERROR: Destination directory $DRIVER_DESTINATION_DIR not exists and cannot be created!  "
                echo "'----------------------------------------------------------------------------------------'"
                SetForegroundDefault
                exit 1
            else
                
                echo "Copying driver to system..."
                cp -f $DRIVER_SOURCE_DIR/$DRIVER_FILE_NAME $DRIVER_DESTINATION_DIR/$DRIVER_FILE_NAME
                
                cd $DRIVER_SOURCE_DIR
                make -f $DRIVER_MAKEFILE_FILE_NAME clean
                cd "$SAVED_PATH"
                
                if [ -e $DRIVER_DESTINATION_DIR/$DRIVER_FILE_NAME ]; then
                    SetForegroundGreen
                    echo ".----------------------------------------------------------------------------------------."
                    echo "|                                     SUCCESS                                            |"
                    echo "'----------------------------------------------------------------------------------------'"
                    SetForegroundDefault
                else
                    SetForegroundRed
                    echo ".----------------------------------------------------------------------------------------."
                    echo "| ERROR: Diver file not copied!                                                          |"
                    echo "'----------------------------------------------------------------------------------------'"
                    SetForegroundDefault
                    exit 1
                fi
            fi 
        fi
    fi  
fi


