#!/bin/sh

function pauseandexit {
    echo Press any key to finish...
    read -n 1 -s
    echo
    exit $1
}
    

echo
echo ATTO Technology, Inc.
echo Linux Driver Install Script v1.2
echo

# Check for kernel source code availability

FULL_KERNEL_VERSION=`uname -r`
KDIR="/lib/modules/${FULL_KERNEL_VERSION}/build"

if [ ! -x ${KDIR} ]; then
    echo
    echo Error: Unable to find kernel source code directory
    echo
    echo Kernel source code was expected in ${KDIR}
    echo
    echo If the kernel source code is installed in a different
    echo location, build the driver manually from the src/
    echo directory specifying the correct kernel source directory
    echo on the make command line with the KDIR variable, eg:
    echo
    echo \# make install KDIR=/usr/src/kernel
    echo
    pauseandexit 1
fi

# Figure out what sort of driver we are building

DRIVER_NAME=`ls src/*core_x86.o | cut -d_ -f1 | cut -d/ -f2`

if [ "${DRIVER_NAME}" = "celerityfc" ]; then
    PRETTY_NAME="Celerity FC"
elif [ "${DRIVER_NAME}" = "express2" ]; then
    PRETTY_NAME="EPCI U320"
elif [ "${DRIVER_NAME}" = "esasraid" ]; then
    PRETTY_NAME="ESAS RAID"
elif [ "${DRIVER_NAME}" = "esashba" ]; then
    PRETTY_NAME="ESAS HBA"
elif [ "${DRIVER_NAME}" = "celerity8fc" ]; then
    PRETTY_NAME="Celerity FC 8GB"
else
    echo
    echo Error: Unable to detect driver name
    echo
    pauseandexit 1
fi

echo
echo Building and installing the ATTO ${PRETTY_NAME} driver, please wait...

# Build & install the modules

cd src

LOAD_MODULE="n"

if make -j clean install >make.log 2>make.err; then
    echo
    echo Successfully installed the ${PRETTY_NAME} driver
    if lsmod | grep ${DRIVER_NAME} > /dev/null 2>/dev/null; then
        echo
        echo An older version of ${DRIVER_NAME} appears to be loaded
        echo Would you like to try to unload the old version and load the new one?  [N/y]
        read -s -n 1 RESPONSE
        if [ "${RESPONSE}" = "y" -o "${RESPONSE}" = "Y" ]; then
            echo
            if ! rmmod ${DRIVER_NAME}; then
                echo Sorry, could not unload the old driver
                echo You may need to reboot to use the new driver
            else
                echo Old version of ${DRIVER_NAME} successfully unloaded
                LOAD_MODULE="y"
            fi
        fi
    else
        echo
        echo Would you like to load the driver now?  [N/y]
        read -s -n 1 LOAD_MODULE
    fi
    echo
    if [ "${LOAD_MODULE}" = "y" -o "${LOAD_MODULE}" = "Y" ]; then
        echo Please wait a moment while the driver loads...
        echo
        if ! modprobe ${DRIVER_NAME}; then
            echo Driver load failed!  Displaying relevant dmesg output:
            echo ---------
            dmesg | grep ${DRIVER_NAME} | tail -n 10
            echo ---------          
        else
            echo The ${DRIVER_NAME} driver has been loaded
        fi
    else
        echo You can load the driver using the command \"modprobe ${DRIVER_NAME}\"
    fi
    echo
else
    echo
    echo An error occurred while making the driver
    echo
    echo ---make output---
    cat make.log
    echo
    echo ---make errors---
    cat make.err
    echo ---------
    echo
fi

pauseandexit 0
