#!/bin/bash
#--------------------------------------------------------------------------
# |
# File: acpi-wakeup |
# Type: BASH script |
# Summary: Script to set the BIOS wakeup time |
# Description: A shell script designed to set the wakeup time of the |
# motherboard BIOS, using the /proc/acpi/alarm interface. This script |
# is designed to replace nvram-wakeup, for use with mythtv. |
# Usage: |
# acpi-backup [-v] --settime XXXXXXXXXXXXXXXXX |
# Author: |
# Designed and written by Michelle Dupuis on July 18, 2007 |
# Michelle can be reached at support@ocgca |
# This script and other related tools are avaiable for download |
# at www.generationd.com |
#--------------------------------------------------------------------------
# |
# DATE VER AUTHOR DESCRIPTION |
# Jul 24 2007 1.3 M Dupuis Improved timezone handling |
# Jul 21 2007 1.2 M Dupuis Fixed GMT error on mythfilldatabase time|
# Jul 20 2007 1.1 M Dupuis Added checking of mythfilldatabase log |
# for zap2it requested check time |
# Jul 18 2007 1.0 M Dupuis Original coding |
#--------------------------------------------------------------------------
VERSION="acpi-wakeup V1.3 - by Michelle Dupuis - support@ocg.ca"
LOGFILE=/home/mythtv/wakeuplog
# set color names
# normal
BK="\O33[0;30m" # black
BL="\033[0;34m" # blue
GR="\033[0;32m" # green
CY="\033[0;36m" # cyan
RD="\033[0;31m" # red
PL="\033[0;35m" # purple
BR="\033[0;33m" # brown
GY="\033[1;30m" # grey
# enhanced
eGY="\033[0;37m" # light gray
eBL="\033[1;34m" # light blue
eGR="\033[1;32m" # light green
eCY="\033[1;36m" # light cyan
eRD="\033[1;31m" # light red
ePL="\033[1;35m" # light purple
eYW="\033[1;33m" # yellow
eWT="\033[1;37m" # white
# reset to teminal default
NRML="\033[0;0m" # normal term color
#exit
PARAMERROR=
if [ $# -lt 2 ] ; then
PARAMERROR="Too few parameters."
elif [ $# -gt 3 ] ; then
PARAMERROR="Too many parameters."
else
if [ $# -eq 2 ] ; then
PFLAG=$1
PEPOCH=$2
PVERBOSE=""
else
PFLAG=$2
PEPOCH=$3
PVERBOSE=$1
fi
# Get the fill time from the log (as suggested by zap)
# FILLTIME=$(cat /var/log/mythfilldatabase.log | grep NextSuggestedTime | tail -1 | awk '{print $NF}' | sed 's/T/ /' | cut -f 2 -d\ )
# Get the fill time from the settings database
FILLTIME=$(mysql --user="mythtv" --password="mythtv" -e 'SELECT data FROM settings where value="MythFillSuggestedRunTime"' mythconverg | tail -1 | cut -f 2 -dT)
if [ -z $FILLTIME ] ; then
# [ "$PVERBOSE" = "-v" ] && echo " MythFillDatabase recommended start time not found."
FILLDATE="2029-12-30"
FILLTIME="23:59:59"
else
FILLDATE=$(date "+%Y-%m-%d")
fi
FILLTZ=$(date "+%Z")
# Convert the date, time, and timezone to seconds since 1970 (i.e. epoch)
FILLEPOCH=$(date -d "${FILLDATE} ${FILLTIME} ${FILLTZ}" "+%s")
# Start 300 seconds early for startup time
# FILLEPOCH=$((FILLEPOCH-300))
# Get NOW epoch
NOW_EPOCH=$(date "+%s")
# If wakeup time has passed, add 24 hours
[ $NOW_EPOCH -gt $FILLEPOCH ] && FILLEPOCH=$((FILLEPOCH+86400))
FILLTIME=$(date -u -d "1970-01-01 ${FILLEPOCH} sec GMT" "+%Y-%m-%d %H:%M:%S %Z")
if [ $NOW_EPOCH -gt $PEPOCH ]; then
PARAMERROR="Passed time (in epoch format) has already passed."
else
#For testing, next line sets input time to July 18 2007, 4:39pm
#PEPOCH="1184791140"
NEW_TIME=$(date -u -d "1970-01-01 ${PEPOCH} sec GMT" "+%Y-%m-%d %H:%M:%S %Z")
fi
if [ -z "$PARAMERROR" ] ; then
if [ $PFLAG != "--settime" ] ; then
PARAMERROR="Invalid parameter format."
else
if [ "$PVERBOSE" = "-v" ] ; then
echo -e $eBL$VERSION$NRML
echo " Mythfilldatabase epoch: [$FILLEPOCH]"
echo " Mythfilldatabase time: [$FILLTIME]"
echo " Passed epoch: [$PEPOCH]"
echo " Passed time: [$NEW_TIME]"
echo $VERSION >> $LOGFILE
echo " "$(date) >> $LOGFILE
echo " Mythfilldatabase epoch: [$FILLEPOCH]" >> $LOGFILE
echo " Mythfilldatabase time: [$FILLTIME]" >> $LOGFILE
echo " Passed epoch: [$PEPOCH]" >> $LOGFILE
echo " Passed time: [$NEW_TIME]" >> $LOGFILE
elif [ -n "$PVERBOSE" ] ; then
PARAMERROR="Unexpected final parameter encountered."
fi
fi
fi
fi
if [ -n "$PARAMERROR" ] ; then
echo -e $eBL$VERSION$NRML
echo " Description: used to set the motherboard wakeup time using the /proc/acpi/alarm interface."
echo " Also checks mythfilldatabase log for suggested wakeup time, and uses earliest."
echo ""
echo " Error: $PARAMERROR"
echo ""
echo " Usage: acpi-wakeup [ -v ] --settime XXXXXXXX"
echo ""
else
if [ $FILLEPOCH -lt $PEPOCH ] ; then
[ "$PVERBOSE" = "-v" ] && echo " Using mythfilldatabase time since it comes first."
echo " Using mythfilldatabase time since it comes first." >> $LOGFILE
NEW_TIME=$FILLTIME
else
[ "$PVERBOSE" = "-v" ] && echo " Using passed time since it comes first."
echo " Using passed time since it comes first." >> $LOGFILE
fi
ACPITIME=$(cat /proc/acpi/alarm)
if [ "$ACPITIME" == "$NEW_TIME" ] ; then
[ "$PVERBOSE" = "-v" ] && echo " ACPI alarm already set correctly: [$ACPITIME]"
echo " ACPI alarm already set correctly: [$FILLTIME]" >> $LOGFILE
else
#echo "ACPITIME: [$ACPITIME]"
#echo "NEWTIME: [$NEW_TIME]"
WAKEUPTIME=$(date -d "${NEW_TIME}" "+%Y-%m-%d %H:%M:%S")
echo "$WAKEUPTIME" > /proc/acpi/alarm
if [ "$PVERBOSE" = "-v" ] ; then
echo " Confirmed ACPI wakeup alarm set to: ["$(cat /proc/acpi/alarm)" ${FILLTZ}]"
echo " Confirmed ACPI wakeup alarm set to: ["$(cat /proc/acpi/alarm)" ${FILLTZ}]" >> $LOGFILE
fi
fi
fi
echo "" >> $LOGFILE