makehtml bash script
#!/bin/bash
#
# present last 15 entries
# & four hour update version
# don't delete coffee.txt and tomup at end
# rev.: 8/17/2018
#
# Organize the data created by raspian/docker-hassio's
# notify into an html page for the VPS
# requires hasser's pub key be in
# mover's directory on the VPS
#
# cut header from recording file, and
# trim to one entry per hour in coffee.txt
fix_c() {
# cut the first two lines if there's coffee.txt
if [ -e coffee.txt ] ; then
tail +3 coffee.txt > coffee.txt2
# sort unique on date/hour (before first :)
sort -t ':' -k 1,1 -u coffee.txt2 -o coffee.txt2
else
# if there's no coffee create empty file
touch coffee.txt2
fi
fix_m
}
# repeat for tomup.txt
fix_m() {
if [ -e tomup.txt ] ; then
tail +3 tomup.txt > tomup.txt2
sort -t ':' -k 1,1 -u tomup.txt2 -o tomup.txt2
else
touch tomup.txt2
fi
combine_sort
}
#
# if not empty add coffee to tomup and put in time order
combine_sort() {
# if coffee isn't empty add it to tomup.txt
if [ -s coffee.txt2 ] ; then
cat coffee.txt2 >> tomup.txt2
fi
# sort in-place
sort tomup.txt2 -o tomup.txt2
trim_15
}
#
# trim tomup.txt2 to last 15 entries
trim_15() {
tail -n 15 tomup.txt2 > tomup.txt2.tmp
mv -f tomup.txt2.tmp tomup.txt2
trim_datestamp
}
# loop tomup.txt2 stripping date & time
# and add text
trim_datestamp() {
# only if tom2.txt isn't empty
if [ -s tomup.txt2 ] ; then
# step through tomup.txt2
while read dt ; do
# copy line to variable
s1=$(echo $dt)
# if it's an M work it
if [[ $dt = *"M" ]]; then
# strip the seconds and M
s2=$(echo $dt | sed 's/\..*M//g')
# convert the date to Pacific and format
dt2=$(date +%a\ %b\ %d\ %I:%M\ %p --date='TZ="UTC" '$s2'')
# append it to tdate.txt
echo "$dt2 Tom is moving" >> tdate.txt
# if it's a C
else
if [[ $dt = *"C" ]]; then
s2=$(echo $dt | sed 's/\..*C//g')
dt2=$(date +%a\ %b\ %d\ %I:%M\ %p --date='TZ="UTC" '$s2'')
echo "$dt2 Coffee is cooking" >> tdate.txt
fi
fi
done <tomup.txt2
fi
trim_dates
}
# trim time for desired display
trim_dates() {
# if tdate.txt exists, work it
if [ -e tdate.txt ] ; then
# add spaces to fourth then third space position
sed -i 's/ /\ \ /5' tdate.txt
# replace 0s with space for alignment
sed -i 's/ 0/ \ /g' tdate.txt
else
# otherwise create it with some text
echo "Nothing to report" > tdate.txt
fi
build_html
}
# build web page
build_html() {
# add some html for paragraphs
sed -i ':a;N;$!ba;s/\n/\n<\/p><p>\n/g' tdate.txt
# merge the html head, body, foot
cat $s_Loc/html/move_head.html >> tomup.html
# include an updated line
echo "Updated on $(date +%a\ %b\ %d)</p><p>" >> tomup.html
cat tdate.txt >> tomup.html
cat $s_Loc/html/notes.txt >> tomup.html
cat $s_Loc/html/move_foot.html >> tomup.html
send_website
}
# send to the web site
send_website() {
# rsync -i $haFl -e "ssh -p 99922" /home/homeassistant/.homeassistant/tomup.html mover@999.999.999.999:/home/mover/tomup.html >/dev/null 2>&1
# %%%%%%%%%%%%%%% test area
#rsync -e "ssh -i /usr/share/hassio/homeassistant/hafiles/tscripts/hasser -p 58287" $s_Loc/data/tomup.html syncer@192.168.999.999:/mnt/share/hasser/tomup.html >/dev/null 2>&1
# rsync -i $haFl -e "ssh -p 99922" /home/homeassistant/.homeassistant/tomup.html mover@999.999.999.999:/home/mover/tomup2.html >/dev/null 2>&1
# %%%%%%%%%%%%%%%
rsync -e "ssh -i /usr/share/hassio/homeassistant/hafiles/tscripts/hasser -p 99922" $s_Loc/data/tomup.html mover@999.999.999.999:/home/mover/tomup.html >/dev/null 2>&1
sleep 1
clean_up
}
# finish cleanup
#
#This needs a way to trim the 2 .txt files weekly?
#possibly a rotate thing
#
clean_up() {
rm -f coffee.txt2
# rm -f coffee.txt
rm -f tomup.txt2
rm -f tdate.txt
# rm -f tomup.txt
# *****rm -f tomup.html
mv tomup.html $s_Loc/archive_files/tomup.html-$(date +%F_%R)
# we are done
}
# start with variables and cd to homeassistant dir
dt=""
dt2=""
dy=$(date +%Y)
dz=$(date +%Z)
# haFl=$s_Loc/tscripts/hasser
# change to /hafiles directory
s_Loc=/usr/share/hassio/homeassistant/hafiles
# s_Loc=hafiles
# %%%%%%%%%%%%%%% test area
# s_Loc=/home/tom2/Documents/active/hassio5/usr/share/hassio/homeassistant/hafiles
# %%%%%%%%%%%%%%%
cd $s_Loc/data
# this is part of a test # echo "here" > here
# goto fix_c
fix_c