#!/bin/bash
# A script to simplify the instalation of EnhanceIO.
#
# Courtesy of T. "Luna" Ericson aka. CPT Gray Wolf and FSG,
# and distributed under the GPL along with EnhanceIO.

# Get the script command security prefix to esure no malicious
# scripts are executed by mistake.
if [ -d /usr/bin ]
    then
    BPREFIX='/usr/bin/'
    SBPREFIX='/usr/sbin/'
else
    BPREFIX='/bin/'
    SBPREFIX='/sbin/'
fi


# Ensure that we're running as root.
if [ $(${BPREFIX}id -u) -ne 0 ]
    then
    ${BPREFIX}echo "INSTALL-EIO: You need to be root to do that" >&2
    exit 1
fi


# This is where any important preset variables will go.
PYTHON_VERSION=$(${BPREFIX}python -c 'import platform; python_version=platform.python_version(); print (python_version)')
SOURCE_DIR='./Driver/enhanceio/'
KERNEL_VERSION=''
PY_VERSION_CHECK=1; MAKE_MODS=1; INSTALL_MODS=1; CLEANUP=1; INSTALL_CLI=1; LOAD_MODS=1



# Use a complicated regex to check the installed python version.
_CheckPythonVersion ()
{
    if [[ "$PYTHON_VERSION" =~ ^2\.(6\.([6-9]|[1-9][0-9]+)|([7-9]|[1-9][0-9]+)\.[0-9]+)(rc[0-9]+)?$ ]]
        then 
        return 0
    fi
    return 1
}

# Install the command line interface.
_InstallCLI ()
{
    if [ -e ./CLI/eio_cli ] && [ -e ./CLI/eio_cli.8 ]
        then
        (${BPREFIX}install -o root -g root -m 700 -D ./CLI/eio_cli /sbin/eio_cli > /dev/null 2>&1) &&
        (${BPREFIX}install -o root -g root -m 644 -D ./CLI/eio_cli.8 /usr/share/man/man8/eio_cli.8 > /dev/null 2>&1) &&
        return 0
        return 2
    fi
    return 1
}

# Run make and build the EnhanceIO modules.
_RunMake ()
{
    if ! [ -z "$1" ]
        then
        (cd ${SOURCE_DIR} && ${BPREFIX}make KERNEL_SOURCE_VERSION=${1} > /dev/null) &&
        return 0
    else
        (cd ${SOURCE_DIR} && ${BPREFIX}make > /dev/null) &&
        return 0
    fi
    return 1
}

# Run make install for the EnhanceIO modules.
_RunInstall ()
{ 
    if ! [ -z "$1" ]
        then
        (cd ${SOURCE_DIR} && ${BPREFIX}make install KERNEL_SOURCE_VERSION=${1} > /dev/null) &&
        return 0
    else
        (cd ${SOURCE_DIR} && ${BPREFIX}make install > /dev/null) &&
        return 0
    fi
    return 1
}

# Cleanup files after make.
_RunCleanup ()
{
    ${BPREFIX}rm -f "${SOURCE_DIR}*.?(k)o"
    ${BPREFIX}rm -f "${SOURCE_DIR}*.mod.c"
    ${BPREFIX}rm -f "${SOURCE_DIR}.*.cmd"
    ${BPREFIX}rm -f "${SOURCE_DIR}*.symvers"
    ${BPREFIX}rm -f "${SOURCE_DIR}*.order"
    ${BPREFIX}rm -fr "${SOURCE_DIR}.tmp*"
    return
}

# Load the EnhanceIO modules.
_LoadMods ()
{
    ${SBPREFIX}modprobe enhanceio &&
    ${SBPREFIX}modprobe enhanceio_rand &&
    ${SBPREFIX}modprobe enhanceio_fifo &&
    ${SBPREFIX}modprobe enhanceio_lru &&
    return 0
    return 1
}

#Display help options.
_DisplayHelp ()
{
    ${BPREFIX}echo -e "EnhanceIO Installation Script\n"
    ${BPREFIX}echo -e "Command options:\n"
    ${BPREFIX}echo "      cleanup           Remove files leftover from build process."
    ${BPREFIX}echo "      install-cli       Install the eio_cli utility, and it's manual."
    ${BPREFIX}echo "      --help            Display help."
    ${BPREFIX}echo "   -k --kernel-version  Chose what kernel version to compile for. Requires the"
    ${BPREFIX}echo "                        target kernel headers to be installed."
    ${BPREFIX}echo "   -m --no-build        Don't build the kernel modules."
    ${BPREFIX}echo "   -i --no-install      Don't install kernel modules."
    ${BPREFIX}echo "   -p --no-py-check     Don't check the installed python version. Has no effect"
    ${BPREFIX}echo "                        if --kernel-version or --no-cli are set."
    ${BPREFIX}echo "   -c --no-cli          Don't install eio_cli utility or manual. Has no effect"
    ${BPREFIX}echo "                        if --kernel-version is set."
    ${BPREFIX}echo "   -l --no-load         Don't load kernel modules after installation. Has no"
    ${BPREFIX}echo "                        effect if --kernel-version is set."
    ${BPREFIX}echo -e "   -r --no-cleanup      Don't run cleanup after building kernel modules.\n"
    return
}


# Setup some shortcuts.
if [ $# -eq 1 ]
    then
    case "$1" in
        cleanup)
        _RunCleanup
        ${BPREFIX}echo "INSTALL-EIO: Cleaning up leftover build files"
        exit 0
        ;;
        install-cli)
        _CheckPythonVersion &&
        _InstallCLI &&
        exit 0
        ${BPREFIX}echo "INSTALL-EIO: Failed to install eio_cli!" >&2
        ;;
        --help)
        _DisplayHelp
        exit 0
        ;;
    esac
fi

# Main program.
while ! [ -z "$1" ]
    do
    case "$1" in
      -k|--kernel-version)
        shift
        if ! [[ "$1" =~ ^(3\.([7-9]|[1-9][0-9]+)|([4-9]|[1-9][0-9]+)\.[0-9]+)....*$ ]]
            then
            ${BPREFIX}echo "INSTALL-EIO: Specified kernel is invalid or too old!" >&2
            exit 1
        else
            KERNEL_VERSION="$1"
            LOAD_MODS=0
            PY_VERSION_CHECK=0
            INSTALL_CLI=0
            ${BPREFIX}echo "INSTALL-EIO: Modules will be build and installed for kernel $1"
        fi
        ;;
      -r|--no-clean)
        CLEANUP=0
        ;;
      -p|--no-py-check)
        PY_VERSION_CHECK=0
        ;;
      -c|--no-cli)
        PY_VERSION_CHECK=0
        INSTALL_CLI=0
        ;;
      -i|--no-install)
        INSTALL_MODS=0
        ;;
      -m|--no-build)
        MAKE_MODS=0
        ;;
      -l|--no-load)
        LOAD_MODS=0
        ;;
      *)
        ${BPREFIX}echo "INSTALL-EIO: Argument not understood \"$1\"" >&2
        ${BPREFIX}echo "INSTALL-EIO: Try running \"Install-EIO --help\""
        exit 1
        ;;
    esac
    shift
done

if [ $MAKE_MODS -eq 1 ]
    then
    ${BPREFIX}echo "INSTALL-EIO: Building kernel modules"
    _RunMake $KERNEL_VERSION
    if [ $? -eq 1 ]
        then
        ${BPREFIX}echo "INSTALL-EIO: Failed to build modules!" >&2
        ${BPREFIX}echo "INSTALL-EIO: Try running \"Install-EIO cleanup\" to remove any leftover files"
        exit 1
    fi
fi
if [ $INSTALL_MODS -eq 1 ]
    then
    ${BPREFIX}echo "INSTALL-EIO: Installing modules"
    _RunInstall $KERNEL_VERSION
    if [ $? -eq 1 ]
        then
        ${BPREFIX}echo "INSTALL-EIO: Failed to install modules!" >&2
        exit 1
    fi
fi
if [ $PY_VERSION_CHECK -eq 1 ]
    then
    ${BPREFIX}echo "INSTALL-EIO: Checking Python version"
    _CheckPythonVersion
    if [ $? -eq 1 ]
        then
        ${BPREFIX}echo "INSTALL-EIO: Python version is $PYTHON_VERSION, eio_cli requires python2 version 2.6.6 or higher!" >&2
        ${BPREFIX}echo "INSTALL-EIO: eio_cli will not be installed" >&2
        INSTALL_CLI=0
    else
        ${BPREFIX}echo "INSTALL-EIO: Python version is $PYTHON_VERSION"
    fi
fi
if [ $INSTALL_CLI -eq 1 ]
    then
    ${BPREFIX}echo "INSTALL-EIO: Installing eio_cli"
    _InstallCLI
    if [ $? -eq 1 ]
        then
        ${BPREFIX}echo "INSTALL-EIO: Could not fild eio_cli files!" >&2
        ${BPREFIX}echo "INSTALL-EIO: Failed to  install eio_cli!" >2&
    elif [ $? -eq 2 ]
        then
        ${BPREFIX}echo "INSTALL-EIO: Failed to  install eio_cli!" >2&
    else
        ${BPREFIX}echo "INSTALL-EIO: Installed eio_cli, see \"man eio_cli\" for useage"
    fi
fi
if [ $LOAD_MODS -eq 1 ]
    then
    ${BPREFIX}echo "INSTALL-EIO: Loading EnhanceIO modules"
    _LoadMods
    if [ $? -eq 1 ]
        then
        ${BPREFIX}echo "INSTALL-EIO: Failed to load kernel modules!" >&2
    else
        ${BPREFIX}echo "INSTALL-EIO: Loaded EnhanceIO kernel modules"
    fi
fi
if [ $CLEANUP -eq 1 ]
    then
    ${BPREFIX}echo "INSTALL-EIO: Cleaning up build files..."
    _RunCleanup
fi
${BPREFIX}echo "INSTALL-EIO: Complete!"
exit 0
