Only send a mail when there is at least one interesting date, added
[infodrom/calendar] / mail-calendar
1 #! /bin/sh
2
3 #   mail-calendar - send information about historical dates
4 #   Copyright (c) 2002,3  Martin Schulze <joey@infodrom.org>
5 #
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software
18 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19
20 #   http://cvs.infodrom.org/calendar/mail-calendar?cvsroot=infodrom
21 #   :pserver:anonymous@cvs.infodrom.org:/var/cvs/infodrom co calendar/mail-calendar
22 #  
23 #   $Id$
24
25 opt_subject="Calendar for"
26 opt_dateform="%B %d"
27 opt_to=$LOGNAME
28 opt_advance=0
29
30 usage()
31 {
32     echo "mail-calendar [-h|--help] [-l|--lang locale] [-t|--to addr] [-a|--advance]"
33 }
34
35 # Parse arguments
36 while [ $# -gt 0 ]
37 do
38     case $1 in
39     -h|--help)
40         usage
41         exit
42         ;;
43     -l|--lang)
44         if [ $# -gt 1 ]
45         then
46             opt_lang=$2
47             shift
48         fi
49         ;;
50     -t|--to)
51         if [ $# -gt 1 ]
52         then
53             opt_to=$2
54             shift
55         fi
56         ;;
57     -a|--advance)
58         if [ $# -gt 1 ]
59         then
60             opt_advance=$2
61             shift
62         fi
63         ;;
64     esac
65     shift
66 done
67
68 # Add support for non-english languages/locales
69 case $opt_lang in
70 de_DE)
71         opt_subject="Kalender für den"
72         opt_dateform="%d. %B"
73 esac
74
75 secs=$[ $opt_advance * 86400 ]
76
77 c_date=`date -d now+${secs}seconds +%d.%m`
78 s_date=`LANG=$opt_lang date -d now+${secs}seconds "+$opt_dateform"`
79
80 tmp=`tempfile`
81 trap 'rm -f $tmp' INT EXIT
82
83 calendar -l 0 -w 0 -t $c_date > $tmp
84
85 if [ -s $tmp ]
86 then
87     (
88         echo "Subject: $opt_subject $s_date"
89         echo "To: $opt_to"
90         echo
91         calendar -l 0 -w 0 -t $c_date
92     ) | /usr/sbin/sendmail "$opt_to"
93 fi