# !/bin/bash

# (c) LJ, 2nd October 2004. www.ljay.org.uk
# Feel free to use/distribute for free.
# Note for the litigious: Comes with ABSOLUTELY NO WARRANTY.  Use at your own risk.
# Designed to run on UK Series 1 TiVos, but probably works on other *nix boxes.

# Returns exit code 0 if currently in GMT or 1 if in BST.
# Indicates BST from 1am on the last Sunday of March until just before 1am on the last Sunday of October.

# Transfer to the TiVo in binary mode and chmod +x
# You can test with: /var/hack/summerzone.sh && echo Currently GMT || echo Currently BST

# e.g. To run 'dailytask' at 8am UK time from crontab, add the following two lines:
# 0 8 * 1-3,10-12 * /var/hack/summerzone.sh && /path/to/dailytask
# 0 7 * 3-10 * /var/hack/summerzone.sh || /path/to/dailytask

mth=`date +%-m`
if (( $mth < 3 )); then
   exit 0;
fi
if (( $mth > 10 )); then
   exit 0;
fi
if (( $mth > 3 && $mth < 10 )); then
   exit 1;
fi
if (( $mth == 3 )); then
   yr=`date +%Y`
   ldm=`date --date=03/31/$yr +%w`
   lsun=$((31 - $ldm))
   day=`date +%-d`
   if (( $day < $lsun )); then
      exit 0;
   fi
   if (( $day > $lsun )); then
      exit 1;
   else
      hr=`date +%-H`
      if (( $hr < 1 )); then
         exit 0;
      else
         exit 1;
      fi
   fi
fi
# We're in October
yr=`date +%Y`
ldm=`date --date=10/31/$yr +%w`
lsun=$((31 - $ldm))
day=`date +%-d`
if (( $day < $lsun )); then
   exit 1;
fi
if (( $day > $lsun )); then
   exit 0;
else
   hr=`date +%-H`
   if (( $hr < 1 )); then
      exit 1;
   else
      exit 0;
   fi
fi
