#!/bin/sh # # Written by Craig Huff # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # ## Define a constant for the maximum amount of time difference, in seconds, ## that we want to allow between the current time and the time that zap2it ## suggested for the next database update. (5 seconds) margin=5 # ## Is there already an at-job to take care of this? # grep mythfilldatabase /var/spool/at/* >& /dev/null if [ "$?" != "0" ]; then #no at-job (yet) # ## Is it more or less time to get an update from zap2it? # ## First, get the time zap2it suggested as the next update time from the log ## and convert it into the number of seconds since the "epoch" nexttime=`cat /var/log/mythtv/mythfilldatabase.log.1 /var/log/mythtv/mythfilldatabase.log | grep NextSuggestedTime | tail -1 | awk '{print $NF}' | sed -e 's/T/ /'` epochtime=`date -u -d "${nexttime}" +%s` # ## Next, get the current time in seconds since the "epoch" thistime=`date +%s` # ## Subtract the larger time from the smaller to get a positive difference if [ ${epochtime} -gt ${thistime} ]; then difftime=`expr ${epochtime} - ${thistime}` # ## If the time has not already passed AND we are close enough (<= ${margin}) then fill the database if [ ${difftime} -le ${margin} ]; then /usr/bin/mythfilldatabase >> /var/log/mythtv/mythfilldatabase.log 2>&1 & else # Otherwise, it is too far in the future to run now; Schedule ourselves to run again at ${nexttime} echo "Current time is "`date +"%H%M %b %d"` echo Setting up an 'at-job' to run at `date -d "${nexttime} UTC" +"%H%M %b %d"` /usr/bin/at `date -d "${nexttime} UTC" +"%H%M %b %d"` >& /dev/null <<- EOF /usr/bin/mythfilldatabase >> /var/log/mythtv/mythfilldatabase.log 2>&1 & EOF fi else # We have already missed the fill time. Run now, unless at-job already gone but mythfilldatabase still working. [ "`pgrep mythfilldatabase`" == "" ] && /usr/bin/mythfilldatabase >> /var/log/mythtv/mythfilldatabase.log 2>&1 & fi fi