#!/bin/bash
if [ -e /dev/ilid ]; then
    . /tmp/cmdline

    #if [ "$HWTYPE" = "AtomBox" ]; then
        if [ ! -f /tmp/noled ] ;then echo "led 0 1830000" | tee /tmp/led | /usr/bin/confTool /dev/ilid 0.a/X >/dev/null 2>&1; fi
    #fi

    # kill all processes currently accessing ILID device
    if [ -e /dev/ilid ]; then
        if [ $(lsof -l /dev/ilid | wc -l) -gt 0 ];then
            /usr/bin/omgstop
        fi
        if [ $(lsof -l /dev/ilid | wc -l) -gt 0 ];then
            echo "stop following processes using ilid"
            lsof /dev/ilid
            PROCESSES=$(lsof -F p /dev/ilid | sed "s/^p//")
            if [ -n "${PROCESSES}" ]; then
                kill ${PROCESSES}
            fi
            sleep 5
        fi
        if [ $(lsof -l /dev/ilid | wc -l) -gt 0 ];then
            echo "stop following processes using ilid"
            lsof /dev/ilid
            PROCESSES=$(lsof -F p /dev/ilid | sed "s/^p//")
            if [ -n "${PROCESSES}" ]; then
                kill -1 ${PROCESSES}
            fi
            sleep 5
        fi
        if [ $(lsof -l /dev/ilid | wc -l) -gt 0 ];then
            PROCESSES=$(lsof -F p /dev/ilid | sed "s/^p//")
            if [ -n "${PROCESSES}" ]; then
                kill -9 ${PROCESSES}
            fi
        fi
    fi

    for i in ilidloop hfc_4s8s tdmdsp falc56 tdm_dma tdmilid ilidmdm ilid26 tdm_spi tdm_i2c tdm_xbar tdmFpga cfgspc pci2slot; do

            if  lsmod | grep -q ${i} ; then
                    if ! rmmod ${i} ; then
                            echo "please reboot to solve this issue!"
                    fi
            fi
    done
fi
exit 0
