#!/bin/bash
set -e
set -x

sleep 15m # if something gets bad, give time to operator to login until this script wrecks anything

declare -A IRQ_NUMBER
declare -A DEV_BY_IRQ
declare -A IRQ_COUNT

CPU_COUNT=`head -1 /proc/interrupts | wc -w`

pushd /sys/bus/pci/drivers/solo6x10/
for DEV in *:*:*.*
do
    IRQNO=`cat $DEV/irq`
    IRQ_NUMBER[$DEV]=$IRQNO
    DEV_BY_IRQ[$IRQNO]=$DEV
    IRQ_COUNT[$IRQNO]=0
done

while true
do
    for IRQNO in "${IRQ_NUMBER[@]}"
    do
        NEW_IRQ_COUNT=`egrep "^[ ]*$IRQNO:" /proc/interrupts | awk '{ printf" 0"; for (i=2;i<2+'$CPU_COUNT';i++) printf" + %s",$i; print"" }' | bc`
        logger "IRQ $IRQNO, DEV ${DEV_BY_IRQ[$IRQNO]}: interrupt count: previous ${IRQ_COUNT[$IRQNO]}, now $NEW_IRQ_COUNT"
        if [[ $NEW_IRQ_COUNT == ${IRQ_COUNT[$IRQNO]} ]]
        then
            logger "Card gave no more interrupts, possibly stalled (or consuming app is not running)"
	    sleep 60
            service bluecherry stop || true
            rmmod solo6x10
            echo 1 > /sys/bus/pci/devices/${DEV_BY_IRQ[$IRQNO]}/reset
            modprobe solo6x10
            service bluecherry start
        fi
        IRQ_COUNT[$IRQNO]=$NEW_IRQ_COUNT
    done
    sleep 60
done
