#!/bin/bash -x
COUNTER=0
while test $COUNTER -lt 1; do  #run forever #this seems to expect a file named 1 in the directory. no idea why. EDIT: that was with counter < 1. must use -lt
        #if load gt 1, then load == 1
        LOAD=$(cat /proc/loadavg | cut -c 1-4)
        FIRSTVAL=$(echo $LOAD | cut -c 1) #check if 1 or higher
        test $FIRSTVAL == 1;  #is equal to 1?
                if test $? == 0; then #return value
                FINALVAL=47
                fi
        test $FIRSTVAL > 1;
                if test $? == 0; then  #if test is succesful, returns zero, so we are looking for a zero
                FINALVAL=47
                fi
        test $FIRSTVAL < 1;
                if test $? == 0; then  #i know this isn't efficient. could combine if else. dont care.
                FINALVAL=$(echo $LOAD | cut -c 3-4)
                fi
        echo $FINALVAL
        #otherwise load x 47 == VAL
        DIVIDEBYTWO=$(expr $FINALVAL / 2) #must be space between / and numbers
#       if test $DIVIDEBYTWO > 50; then #hmmm... needs -gt not >
#               DIVIDEBYTWO=50;
#               fi
#       echo "DIVIDEBY TWO IS: $DIVIDEBYTWO"
#seems to need a space after the first funct. parenthesis, and the call to function
#seems to not like function named gopher, so set to go4. perhaps character limit.
        echo "(defun go4()   (with-i2c (str 96)     (write-byte #x40 str)     (write-byte $DIVIDEBYTWO str)     (write-byte #x00 str))) (go4)" > /dev/ttyUS>
#       echo " (go4)" > /dev/ttyUSB0
        sleep 5
done

