1#! /bin/sh
2#
3#  @configure_input@
4#
5#  $Id: daily.in 0.12 2000/03/24 00:01:02 tom Exp $
6#
7#  daily.in:  Uses `daily[.awk|.pl]' for processing Gcal resource file lines
8#               used for time management of fixed dates stored in the fixed
9#               `DATE-PART HH1:MM1,HH2:MM2,%n,%t  FIXED-DATE-TEXT' format.
10#               `HH1:MM1' is the time (HOUR:MINUTE) the fixed date takes place.
11#               `HH2:MM2' is the time (HOUR:MINUTE) the fixed date is warned
12#               in advance.  The valid range of HOUR is 00...23, and the
13#               valid range of MINUTE is 00...59.  For example
14#                 `0 08:15,00:30,%n,%t  Dentist'
15#               creates a today's fixed date message of 30 minutes
16#               starting at 07:45 until 08:15.
17#
18#               Usage: daily --help | --version
19#               Usage: daily [--debug] [--mail[=ADDRESS]]
20#                            [RESOURCE-FILE...] [more Gcal options...]
21#
22#               `--debug':           Turn on shell debugging.
23#               `--mail[=ADDRESS]':  If any fixed date messages are created,
24#                                      eMail them to the specified ADDRESS.
25#                                      If no ADDRESS is given, eMail them
26#                                      to $MAILTO, $USER or $LOGNAME.
27#
28#  UN*X solution.
29#
30#  Needs `test', `echo', `eval', `set', `sed', `date', `mail', `gcal'
31#  and `perl' or `awk'  for processing!
32#
33#  Returns 0 if one or more fixed date is active (returned by AWK/Perl script
34#            if neither `--help' nor `--version' option was specified).
35#  Returns 1 in case no fixed date is active (returned by AWK/Perl script).
36#  Returns 2 if neither Perl nor AWK are present for further processing.
37#
38#  Copyright (c) 2000  Thomas Esken      <esken@uni-muenster.de>
39#                      Im Hagenfeld 84
40#                      D-48147 M"unster
41#                      GERMANY
42#
43#  This software doesn't claim completeness, correctness or usability.
44#  On principle I will not be liable for ANY damages or losses (implicit
45#  or explicit), which result from using or handling my software.
46#  If you use this software, you agree without any exception to this
47#  agreement, which binds you LEGALLY !!
48#
49#  This program is free software; you can redistribute it and/or modify
50#  it under the terms of the `GNU General Public License' as published by
51#  the `Free Software Foundation'; either version 3, or (at your option)
52#  any later version.
53#
54#  You should have received a copy of the `GNU General Public License'
55#  along with this program; if not, write to the:
56#
57#
58#
59
60#
61# Basically used programs.
62#
63Sed=sed
64Set=set
65Test=test
66Echo=echo
67
68#
69# Basically used texts.
70#
71packagedatamiscdir=/usr/local/share/gcal/misc/daily
72#
73PACKAGE=@PACKAGE@
74VERSION=@VERSION@
75transform=@program_transform_name@
76#
77mydefaultname=daily
78#
79myname=`$Echo "$0" | $Sed -e 's,.*/,,' -e "$transform"`
80myversion=0.12
81myinternalname="$myname ($PACKAGE $VERSION) $myversion"
82#
83gcal_resource_file_suffix=.rc
84gcal_resource_file=$mydefaultname""$gcal_resource_file_suffix
85info1="$myname: Time management of fixed dates, which are stored"
86info2="$myname: in a Gcal resource file by using the fixed format"
87info3="$myname: \`DATE-PART HH1:MM1,HH2:MM2,%n,%t  FIXED-DATE-TEXT'."
88info4="$myname:"
89usage1="usage: $myname  --help | --version"
90usage2="usage: $myname  [--debug] [--mail[=ADDRESS]] [RESOURCE-FILE...] [more Gcal options..]"
91usage3="usage: $myname  without RESOURCE-FILE uses file "\
92"\`$packagedatamiscdir/$gcal_resource_file' by default"
93
94#
95# Some constant values (EXIT_SUCCESS(==0) and EXIT_FAILURE(==1)
96#   are also returned by the processed AWK/Perl script.
97#
98EXIT_SUCCESS=0
99EXIT_FAILURE=1
100EXIT_FATAL=2
101
102#
103# The used programs.
104#
105Eval=eval
106Date=date
107ThisMail=mail
108ThisGcal=/usr/local/bin/gcal
109#
110Awk=@AWK@
111Awk_script=$mydefaultname"".awk
112#
113Perl=@PERL@
114Perl_script=$mydefaultname"".pl
115
116#
117# Let's check for command line arguments.
118#
119debug=no
120mail=no
121opt_mail=""
122previous=""
123for option in $*
124do
125  #
126  # If the previous option needs an argument, assign it.
127  #
128  if $Test -n "$previous";
129  then
130    $Eval "$previous=\$option"
131    previous=""
132    continue
133  fi
134
135  case "$option" in
136    -*=)  optarg="###error###" ;;
137    -*=*) optarg=`$Echo "$option" | $Sed 's/[-_a-zA-Z0-9]*=//'` ;;
138    *)    optarg="" ;;
139  esac
140
141  #
142  # Manage the options.
143  #
144  case "$option" in
145    -help=* | -hel=* | -he=* | -h=* | -help | -hel | -he | -h | \
146    --help=* | --hel=* | --he=* | --h=* | --help | --hel | --he | --h)
147      if $Test -n "$optarg";
148      then
149        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
150        exit $EXIT_FAILURE
151      fi
152      $Echo "$info1"
153      $Echo "$info2"
154      $Echo "$info3"
155      $Echo "$info4"
156      $Echo "$usage1"
157      $Echo "$usage2"
158      $Echo "$usage3"
159      exit $EXIT_SUCCESS ;;
160
161    -version=* | -versio=* | -versi=* | -vers=* | -ver=* | -ve=* | -v=* | \
162    -version | -versio | -versi | -vers | -ver | -ve | -v | \
163    --version=* | --versio=* | --versi=* | --vers=* | --ver=* | --ve=* | --v=* | \
164    --version | --versio | --versi | --vers | --ver | --ve | --v)
165      if $Test -n "$optarg";
166      then
167        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
168        exit $EXIT_FAILURE
169      fi
170      $Echo "$myinternalname"
171      exit $EXIT_SUCCESS ;;
172
173    -debug=* | -debu=* | -deb=* | -de=* | -d=* | \
174    -debug | -debu | -deb | -de | -d | \
175    --debug=* | --debu=* | --deb=* | --de=* | --d=* | \
176    --debug | --debu | --deb | --de | --d)
177      if $Test -n "$optarg";
178      then
179        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
180        exit $EXIT_FAILURE
181      fi
182      debug=yes
183      shift ;;
184
185    -mail=* | -mai=* | -ma=* | -m=* | -mail | -mai | -ma | -m | \
186    --mail=* | --mai=* | --ma=* | --m=* | --mail | --mai | --ma | --m)
187      if $Test "$optarg" = "###error###";
188      then
189        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=//g'`' requires an argument" 1>&2
190        exit $EXIT_FAILURE
191      fi
192      opt_mail="$optarg"
193      mail=yes
194      shift ;;
195  esac
196done
197
198#
199# Enable tracing.
200#
201if $Test "$debug" = yes;
202then
203  $Set -x
204fi
205
206#
207# Let's start processing now.
208#
209if $Test -n "$Awk" || $Test -n "$Perl";
210then
211  ARGS=""
212  CMDS=""
213  FILS=""
214  for arg in $*
215  do
216    case "$arg" in
217      -*) ARGS="$ARGS $arg" ;;
218      /*) ARGS="$ARGS $arg" ;;
219      @*) ARGS="$ARGS $arg" ;;
220      %*) ARGS="$ARGS $arg" ;;
221      [0-9*]*) CMDS="$CMDS $arg" ;;
222      *) if $Test -z "$FILS";
223         then
224           FILS="$arg"
225         else
226           FILS="$FILS+$arg"
227         fi
228    esac
229  done
230  if $Test -z "$FILS";
231  then
232    FILS=$packagedatamiscdir/$gcal_resource_file
233  fi
234  if $Test "$mail" = no;
235  then
236    ARGS="$ARGS -Hyes"
237  fi
238  #
239  # We prefer the use of Perl.
240  #
241  if $Test -n "$Perl";
242  then
243    result=`$ThisGcal $ARGS -QUax -f $FILS $CMDS | \
244            $Perl $packagedatamiscdir/$Perl_script`
245  else
246    result=`$ThisGcal $ARGS -QUax -f $FILS $CMDS | \
247            $Awk -f $packagedatamiscdir/$Awk_script`
248  fi
249  status=$?
250  if $Test "$status" -eq "$EXIT_FAILURE";
251  then
252    $Echo "$myname: no active fixed dates actually" 1>&2
253  else
254    #
255    # Detect the proper eMail address.
256    #
257    if $Test "$mail" = yes;
258    then
259      if $Test -n "$opt_mail";
260      then
261        MailAddr="$opt_mail"
262      else
263        if $Test -n "$MAILTO";
264        then
265          MailAddr="$MAILTO"
266        else
267          if $Test -n "$USER";
268          then
269            MailAddr="$USER"
270          else
271            if $TEST -n "$LOGNAME";
272            then
273              MailAddr="$LOGNAME"
274            else
275              $Echo "$myname: warning: cannot send mail, address unknown" 1>&2
276              $Echo "$result" 1>&2
277              exit $EXIT_FAILURE
278            fi
279          fi
280        fi
281      fi
282      if $Test -n "$MAILPROG";
283      then
284        ThisMail="$MAILPROG"
285      fi
286      if $Test "$debug" = no;
287      then
288        $Echo "$result" | $ThisMail -s "Mail from \`$myname' (`$Date`)" $MailAddr
289      fi
290      $Echo "$myname: active fixed dates sent by $ThisMail to \`$MailAddr'"
291    else
292      $Echo "$result"
293    fi
294  fi
295  exit $status
296else
297  $Echo "$myinternalname: error: neither \`perl' nor \`awk' available" 1>&2
298  exit $EXIT_FATAL
299fi
300