#!/bin/sh
# This file is for cron job

# Make sure there is only one cgminer running
C=`pidof cgminer | wc -w`
if [ "$C" != "1" ]; then
    killall -s 9 cgminer
    sleep 1
    /etc/init.d/cgminer restart
    exit 0;
fi

# Make sure the devices staill active
B=`cgminer-api devs | grep "^   \[Last Valid Work\]"`
if [ "$?" != "0" ]; then
    killall -s 9 cgminer
    sleep 1
    /etc/init.d/cgminer restart
    exit 0;
fi

A=`cat /tmp/cm.log`
echo -n "$B" > /tmp/cm.log
if [ "$A" == "$B" ]; then
    killall -s 9 cgminer
    sleep 1
    /etc/init.d/cgminer restart
    exit 0;
fi