#!/bin/sh
# FILE LICENSE TAG: LGPL
### BEGIN INIT INFO
# Provides: coi
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Intel(R) COI
# Description: Intel(R) Compiler Offload Infrastructure
### END INIT INFO

#Possible coi.conf file locations
coi_config_file=/etc/sysconfig/coi.conf
coi_altconfig_file=/etc/coi.conf

#The defaults that *can* be overridden by a coi.conf file.
#Simply copy all or some of these variables into a coi.conf
#specified in one of the above paths to get custom settings.

coiexec=/usr/bin/coi_daemon   #Full path of coi_daemon to launch at boot

coiparams='--coiuser=micuser' #default parameters at boot
                              # this defalults to low security with all
                              # not requiring authentication
                              # this line can contain any number of
                              # parameters, see coi_daemon --help for
                              # complete list.

fdmax=10240                   #max file descriptors per COI process
                              # linux default is 1024

remount_root_size=85%         #% of file system coi can use for buffers


#Internal variables, do not change
coi_status="offline"
coi_exit_code=4

start()
{
        echo "coi: Starting coi_daemon... "
        if [ `pidof coi_daemon` ]; then
            echo "[ ERROR ] coi_daemon is already running ..."
            coi_return_code=0
            return
        fi

        if [ ! -f $coiexec ]; then
            echo "    [ ERROR ] coi_daemon is not installed"
            coi_return_code=3
            return
        fi

        if [ -f $coi_config_file ]; then
            source $coi_config_file
        else
            if [ -f $coi_altconfig_file ]; then
                source $coi_altconfig_file
            else
                echo "     Using default values"
            fi
        fi

        # This block will find physical card side RAM
        # and subtract 512MB from it and set that as the maximum
        # for the tmpfs root partition to use, this is nice
        # as it expands and grows automatically with the installed
        # ram on the device
        #reserved=$((512 * 1024))
        #totalsize=`sed -ne 's/^MemTotal: *\([0-9]\+\) kB$/\1/p' /proc/meminfo`
        #tmpfs=$(($totalsize - $reserved))k
        #mount / -o remount,size=$tmpfs

        # Setting to 85% to allow some room for the kernel to avoid panics
        mount / -o remount,size=$remount_root_size

        # Check and increase ulimit if necessary
        currentfdmax=`ulimit -n`
        if [ $currentfdmax -lt $fdmax ]; then
            echo "     Increasing ulimit -n to $fdmax for COI daemon"
            ulimit -n $fdmax
        else
            echo "     ulimit -n $currentfdmax is equal or larger than defaults, doing nothing"
        fi

        $coiexec $coiparams &
        status
        if [ $coi_return_code == 0 ]; then
            echo "[   OK  ] coi_daemon started... "
        else
            echo "[ ERROR ] coi_daemon start failed..."
        fi
}

stop()
{
    echo "coi: Stopping COI daemon"
    if [ `pidof coi_daemon` ]; then
        kill -9 `pidof coi_daemon`
        coi_return_code=3
    else
        echo "coi_daemon is not running"
        coi_return_code=3
    fi
}

status()
{
    if [ `pidof coi_daemon` ]; then
        coi_status="running pid=`pidof coi_daemon`"
        coi_return_code=0
    else
        coi_status="offline"
        coi_return_code=3
    fi 
}


case "$1" in
    start)
        start
        exit $coi_return_code;
        ;;

    stop)
        stop
        exit $coi_return_code;
        ;;

    status)
        status
        echo "coi_daemon is $coi_status"
        exit $coi_return_code;
        ;;
    force-reload)
        stop
        start
        exit $coi_return_code;
        ;;
    restart)
        stop
        start
        exit $coi_return_code;
        ;;

    *)
        echo "Usage:"
        echo "coi start | stop | restart | force-reload | status"
        exit 4
esac
