#!/bin/bash -x
#
# shell> sudo apt-get install rddtool lftp
# shell> sudo crontab -e
#        */1 * * * * /data/froling.sh

DEVICE="/dev/ttyUSB0"
DEVICEOPT="57600 cs8 -parenb -cstopb -hupcl -echo"
LOGFILE="/data/logfile.txt"

FTPURL="bashy.tipido.net"
FTPLOGIN="bashy"
FTPPASS=""
FTPDIR="htdocs"

RRDTOOLDB="/data/rrdtool_db"
PUBLIC="/data/public_html"

VARS=(
	'1:0'
	'2:°C'
	'3:°C'
	'4:%'
	'5:%'
	'6:%'
	'7:%'
	'8:%'
	'9:%'
	'10:U/min'
	'11:°C'
	'20:°C'
	'21:°C'
	'22:%'
	'23:°C'
	'24:°C'
	'26:1'
	'28:°C'
	'30:h'
	'99:1'
     )

stty -F $DEVICE sane
stty -F $DEVICE $DEVICEOPT
cat $DEVICE ${DEVICEOPT// /,} > $LOGFILE &

PID=$!

count=0
until [ $(grep -c '^\$' $LOGFILE) -ge 2 ] ; do
    sleep 1s
    count=$[count+1]
    if [ $count = 40 ] ; then
	echo "no data record found  ..."
	break
    fi
done

kill $PID

count=0
while read x ; do
    if [ "${x/% *}"  = '$' ] ; then
	count=$[count+1]
    fi
    case $count in
	 1) [ "$x" ] && dmp=$dmp"$x\n" ;;
	 2) break ;;
    esac
done < $LOGFILE

echo -e $dmp > $LOGFILE

SED="sed -e \"s/>timestamp</>$(date "+%d.%m.%y\&nbsp\&nbsp\&nbsp%X")</\""

if [ -e /sys/bus/w1/devices/w1_bus_master1/10-0008028fdcff/w1_slave ] ; then
    p="$(grep 't=' /sys/bus/w1/devices/w1_bus_master1/10-0008028fdcff/w1_slave | \
    awk -F '=' '{ printf "%3.1f\n",$2/940 }') °C"
    SED=$SED" -e \"s/>puffermitte-oben</>$p</\""
fi

if [ -e /sys/bus/w1/devices/w1_bus_master1/10-0008028ff102/w1_slave ] ; then
    p="$(grep 't=' /sys/bus/w1/devices/w1_bus_master1/10-0008028ff102/w1_slave | \
    awk -F '=' '{ printf "%3.1f\n",$2/900 }') °C"
    SED=$SED" -e \"s/>puffermitte-unten</>$p</\""
fi

for x in "${VARS[@]}" ; do
    IFS=":" y=( $x )
    IFS=";" z=( $(grep -m1 -ai ";$y;[0-9]" $LOGFILE) )
    if [ -z "${y[1]/[0-9]/}" ] ; then
	value=${z[${y[1]}]/000}
	value=${value/$ }
	value=${value%% }
	SED=$SED" -e \"s/>ID_$y</>${value/\//\\/}</\""
    else
	value=$(echo ${z[1]} ${z[3]} | awk '{ if (14=="'"$y"'") { printf("%3.1f\n",$1/$2) } else { print $1/$2 } }')
	if [ "$value" = nan -o -z "$value" ] ; then
	    value=$(rrdtool info $RRDTOOLDB/ID_$y.rrd | grep last_ds | cut -d'"' -f2)
	fi
	SED=$SED" -e \"s/>ID_$y</>$value ${y[1]/\//\\/}</\""
	if [ -e "$RRDTOOLDB/ID_$y.rrd" ] ; then
	    rrdtool update "$RRDTOOLDB/ID_$y.rrd" N:$value
	else
	    rrdtool create "$RRDTOOLDB/ID_$y.rrd" --step 60 \
	    DS:ID_$y:GAUGE:600:U:U \
	    RRA:AVERAGE:0.5:1:2880 \
	    RRA:AVERAGE:0.5:60:700 \
	    RRA:AVERAGE:0.5:240:720 \
	    RRA:AVERAGE:0.5:1440:730 \
	    RRA:MAX:0.5:1:2880 \
	    RRA:MAX:0.5:60:700 \
	    RRA:MAX:0.5:240:720 \
	    RRA:MAX:0.5:1440:730 \
	    RRA:MIN:0.5:1:2880 \
	    RRA:MIN:0.5:60:700 \
	    RRA:MIN:0.5:240:720 \
	    RRA:MIN:0.5:1440:730
	fi
    fi
    export ID_$y="$value"
done

for i in ID_{10,22,26,27} ; do
    eval y=\$${i}
    case $y in
	 0) x=off ;;
	 *) x=on ;;
    esac
    SED=$SED" -e \"s/\\\"$i.gif\\\"/\\\"$i-$x.gif\\\"/\""
done

eval $SED $PUBLIC/template.html > $PUBLIC/main.html

if [ "$FTPPASS" ] ; then
lftp <<EOF
set ftp:ssl-allow no
set ftp:passive-mode true
set ftp:list-options -a
set cmd:save-cwd-history no
set cmd:save-rl-history no
set xfer:log no
set ftp:use-mdtm off
set net:timeout 10
open -u $FTPLOGIN,$FTPPASS $FTPURL
mirror --reverse --only-newer $PUBLIC/ /$FTPDIR
EOF
fi
