#!/bin/bash -
#===============================================================================
#
#         FILE: cronjob_comments
#
#        USAGE: ./cronjob_comments
#
#  DESCRIPTION: Runs 'scrape_comments' every so often through Cron. This
#               returns the number of comments awaiting approval.
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: This version is for running on Pi Zero 1, it just lights the
#        	Blinkt and doesn't run the pop-up code
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.2
#      CREATED: 2016-07-07 10:43:51
#     REVISION: 2016-09-11 22:10:33
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}

#
# Directories and files
#
BASEDIR="$HOME/HPR/Community_News"
LOGS="$BASEDIR/logs"
LOGFILE="$LOGS/$SCRIPT.log"

#
# LED number on the Blinkt
#
LED=1

#
# Simple sanity check
#
SCRAPER="$BASEDIR/scrape_comments"
if [[ ! -e $SCRAPER ]]; then
    echo "$SCRAPER was not found"
    exit 1
fi

#
# Capture output and result. The script returns the number of comments and
# prints a message which we log
#
message="$($SCRAPER)"
result=$?
echo "$(date +%Y%m%d%H%M%S) $message" >> "$LOGFILE"

if [[ $result -gt 0 ]]; then
    #
    # Send stuff to the local Blinkt!, pixel $LED
    #
    mosquitto_pub -t pimoroni/blinkt -m "rgb,$LED,255,255,0"
else
    #
    # Turn the pixel off, there are no comments
    #
    mosquitto_pub -t pimoroni/blinkt -m "rgb,$LED,0,0,0"
fi

# vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21

