1#! /bin/sh
2#
3#  misc/srss/srss.  Generated from srss.in by configure.
4#
5#  $Id: srss.in 0.09 2000/03/24 00:00:09 tom Exp $
6#
7#  srss.in:  Uses `srss[.awk|.pl]' for processing the Gcal `srss.rc' resource
8#              file to display the times at which sunrise/sunset has happened
9#              in the past respectively will happen in the future, based on
10#              the actual local time.  If a Gcal resource file is specified
11#              as program option, this resource file is used instead of the
12#              default resource file `srss.rc'.
13#
14#              Usage: srss --help | --version
15#              Usage: srss [--debug] [--mail[=ADDRESS]]
16#                          [--no-pager] [--mode=rise|set] [--sort=abs|rel]
17#                          [RESOURCE-FILE] [more Gcal options...]
18#
19#              `--debug':          Turn on shell debugging.
20#              `--mail[=ADDRESS]': EMail the result to the specified ADDRESS.
21#                                    If no ADDRESS is given, eMail it to
22#                                    $MAILTO, $USER or $LOGNAME.
23#              `--no-pager':       Disable the use of a pager program.
24#              `--mode=rise':      Display the sunrise local times of the
25#                                    locations.
26#              `--mode=set':       Display the sunset local times of the
27#                                    locations.  If this option is not given,
28#                                    `--mode=rise' is used by default.
29#              `--sort=abs':       The sort order of the past times (indicated
30#                                    by a `-' sign) respectively future times
31#                                    (indicated by a `+' sign) is absolute.
32#              `--sort=rel':       The sort order of the past times (indicated
33#                                    by a `-' sign) respectively future times
34#                                    (indicated by a `+' sign) is relative.
35#                                    If this option is not given, `--mode=abs'
36#                                    is used by default.
37#
38#  UN*X solution.
39#
40#  Needs `test', `echo', `eval', `set', `sed', `date', `mail', `sort',
41#  `gcal' and `perl' or `awk' for processing!
42#
43#  Returns 0 if any fixed dates are created (returned by AWK/Perl script
44#            if neither `--help' nor `--version' option was specified).
45#  Returns 1 in case no fixed dates are created (returned by AWK/Perl script).
46#  Returns 2 if neither Perl nor AWK are present for further processing.
47#
48#  Copyright (c) 2000  Thomas Esken      <esken@uni-muenster.de>
49#                      Im Hagenfeld 84
50#                      D-48147 M"unster
51#                      GERMANY
52#
53#  This software doesn't claim completeness, correctness or usability.
54#  On principle I will not be liable for ANY damages or losses (implicit
55#  or explicit), which result from using or handling my software.
56#  If you use this software, you agree without any exception to this
57#  agreement, which binds you LEGALLY !!
58#
59#  This program is free software; you can redistribute it and/or modify
60#  it under the terms of the `GNU General Public License' as published by
61#  the `Free Software Foundation'; either version 3, or (at your option)
62#  any later version.
63#
64#  You should have received a copy of the `GNU General Public License'
65#  along with this program; if not, write to the:
66#
67#
68#
69
70#
71# Basically used programs.
72#
73Sed=sed
74Set=set
75Test=test
76Echo=echo
77Sort=sort
78
79#
80# Basically used texts.
81#
82packagedatamiscdir=.
83#
84PACKAGE=gcal
85VERSION=4.1
86transform=s,x,x,
87#
88mydefaultname=srss
89#
90myname=`$Echo "$0" | $Sed -e 's,.*/,,' -e "$transform"`
91myversion=0.09
92myinternalname="$myname ($PACKAGE $VERSION) $myversion"
93#
94param1=rise
95param2=set
96param3=abs
97param4=rel
98#
99gcal_resource_file_suffix=.rc
100gcal_resource_file=$mydefaultname""$gcal_resource_file_suffix
101#
102info1="$myname: Displays the times at which sun""$param1/sun""$param2 has happened in the"
103info2="$myname: past respectively will happen in the future, based on the actual"
104info3="$myname: local time for several geographic locations around the world."
105info4="$myname:"
106usage1="usage: $myname  --help | --version"
107usage2="usage: $myname  [--debug] [--mail[=ADDRESS]] [--no-pager] [--mode=$param1|$param2]"
108usage3="usage:            [--sort=$param3|$param4] [RESOURCE-FILE] [more Gcal options...]"
109usage4="usage: $myname  without \`--mode=$param1|$param2' option uses \`--mode=$param1' by default"
110usage5="usage: $myname  without \`--sort=$param3|$param4'  option uses \`--sort=$param3'  by default"
111usage6="usage: $myname  without RESOURCE-FILE uses file "\
112"\`$packagedatamiscdir/$gcal_resource_file' by default"
113
114#
115# Some constant values.
116#
117EXIT_SUCCESS=0
118EXIT_FAILURE=1
119EXIT_FATAL=2
120
121#
122# The used programs.
123#
124Eval=eval
125Date=date
126ThisMail=mail
127ThisGcal=../../src/gcal
128Pager1=less
129Pager2=more
130Pager3=
131#
132Awk=gawk
133Awk_script=$mydefaultname"".awk
134#
135Perl=perl
136Perl_script=$mydefaultname"".pl
137
138#
139# Let's check for command line arguments.
140#
141debug=no
142pager=yes
143mail=no
144opt_mail=""
145opt_mode=""
146opt_sort=""
147previous=""
148for option in $*
149do
150  #
151  # If the previous option needs an argument, assign it.
152  #
153  if $Test -n "$previous";
154  then
155    $Eval "$previous=\$option"
156    previous=""
157    continue
158  fi
159
160  case "$option" in
161    -*=)  optarg="###error###" ;;
162    -*=*) optarg=`$Echo "$option" | $Sed 's/[-_a-zA-Z0-9]*=//'` ;;
163    *)    optarg="" ;;
164  esac
165
166  #
167  # Manage the options.
168  #
169  case "$option" in
170    -help=* | -hel=* | -he=* | -h=* | -help | -hel |-he | -h | \
171    --help=* | --hel=* | --he=* | --h=* | --help | --hel | --he | --h)
172      if $Test -n "$optarg";
173      then
174        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
175        exit $EXIT_FAILURE
176      fi
177      $Echo "$info1"
178      $Echo "$info2"
179      $Echo "$info3"
180      $Echo "$info4"
181      $Echo "$usage1"
182      $Echo "$usage2"
183      $Echo "$usage3"
184      $Echo "$usage4"
185      $Echo "$usage5"
186      $Echo "$usage6"
187      exit $EXIT_SUCCESS ;;
188
189    -version=* | -versio=* | -versi=* | -vers=* | -ver=* | -ve=* | -v=* | \
190    -version | -versio | -versi | -vers | -ver | -ve | -v | \
191    --version=* | --versio=* | --versi=* | --vers=* | --ver=* | --ve=* | --v=* | \
192    --version | --versio | --versi | --vers | --ver | --ve | --v)
193      if $Test -n "$optarg";
194      then
195        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
196        exit $EXIT_FAILURE
197      fi
198      $Echo "$myinternalname"
199      exit $EXIT_SUCCESS ;;
200
201    -debug=* | -debu=* | -deb=* | -de=* | -d=* | \
202    -debug | -debu | -deb | -de | -d | \
203    --debug=* | --debu=* | --deb=* | --de=* | --d=* | \
204    --debug | --debu | --deb | --de | --d)
205      if $Test -n "$optarg";
206      then
207        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
208        exit $EXIT_FAILURE
209      fi
210      debug=yes
211      shift ;;
212
213    -no-pager=* | -no-page | -no-pag=* | -no-pa=* | -no-p=* | -no-=* | -no=* | -n=* | \
214    -no-pager | -no-page | -no-pag | -no-pa | -no-p | -no- | -no | -n | \
215    --no-pager=* | --no-page=* | --no-pag=* | --no-pa=* | --no-p=* | --no-=* | --no=* | --n=* | \
216    --no-pager | --no-page | --no-pag | --no-pa | --no-p | --no- | --no | --n)
217      if $Test -n "$optarg";
218      then
219        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
220        exit $EXIT_FAILURE
221      fi
222      pager=no
223      shift ;;
224
225    -mail | -mai | -ma | --mail | --mai | --ma | \
226    -mail=* | -mai=* | -ma=* | --mail=* | --mai=* | --ma=*)
227      if $Test "$optarg" = "###error###";
228      then
229        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=//g'`' requires an argument" 1>&2
230        exit $EXIT_FAILURE
231      fi
232      opt_mail="$optarg"
233      mail=yes
234      shift ;;
235
236    -mode | -mod | -mo | --mode | --mod | --mo)
237      previous=mode ;;
238    -mode=* | -mod=* | -mo=* | --mode=* | --mod=* | --mo=*)
239      if $Test -z "$optarg" || $Test "$optarg" = "###error###";
240      then
241        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=//g'`' requires an argument" 1>&2
242        exit $EXIT_FAILURE
243      fi
244      opt_mode="$optarg"
245      shift ;;
246
247    -sort | -sor | -so | -s | --sort | --sor | --so | --s)
248      previous=sort ;;
249    -sort=* | -sor=* | -so=* | -s=* | --sort=* | --sor=* | --so=* | --s=*)
250      if $Test -z "$optarg" || $Test "$optarg" = "###error###";
251      then
252        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=//g'`' requires an argument" 1>&2
253        exit $EXIT_FAILURE
254      fi
255      opt_sort="$optarg"
256      shift ;;
257  esac
258done
259
260if $Test -n "$previous";
261then
262  $Echo "$myname: error: missing argument to --$previous" 1>&2
263  exit $EXIT_FAILURE
264fi
265
266#
267# Enable tracing.
268#
269if $Test "$debug" = yes;
270then
271  $Set -x
272fi
273
274#
275# Let's start processing now.
276#
277if $Test -n "$Awk" || $Test -n "$Perl";
278then
279  SCRIPT_ARGS=""
280  if $Test -n "$opt_mode";
281  then
282    arg=`$Echo "$opt_mode" | $Sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
283    if $Test "$arg" = "$param1" || $Test "$arg" = "$param2";
284    then
285      SCRIPT_ARGS="-a""$arg"
286    else
287      $Echo "$myname: error: invalid option argument \`$opt_mode' specified" 1>&2
288      $Echo "$myname: use --help to show usage" 1>&2
289      exit $EXIT_FAILURE
290    fi
291  fi
292  if $Test -n "$opt_sort";
293  then
294    arg=`$Echo "$opt_sort" | $Sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
295    if $Test "$arg" = "$param3" || $Test "$arg" = "$param4";
296    then
297      SCRIPT_ARGS="$SCRIPT_ARGS -b""$arg"
298    else
299      $Echo "$myname: error: invalid option argument \`$opt_sort' specified" 1>&2
300      $Echo "$myname: use --help to show usage" 1>&2
301      exit $EXIT_FAILURE
302    fi
303  fi
304  ARGS=""
305  CMDS=""
306  FILS=""
307  for arg in $*
308  do
309    case "$arg" in
310      -*) ARGS="$ARGS $arg" ;;
311      /*) ARGS="$ARGS $arg" ;;
312      @*) ARGS="$ARGS $arg" ;;
313      %*) ARGS="$ARGS $arg" ;;
314      [0-9*]*) CMDS="$CMDS $arg" ;;
315      *) if $Test -z "$FILS";
316         then
317           FILS="$arg"
318         else
319           FILS="$FILS+$arg"
320         fi
321    esac
322  done
323  if $Test -z "$FILS";
324  then
325    FILS=$packagedatamiscdir/$gcal_resource_file
326  fi
327  if $Test "$mail" = no;
328  then
329    ARGS="$ARGS -Hyes"
330  fi
331  #
332  # We prefer the use of Perl.
333  #
334  if $Test -n "$Perl";
335  then
336    result=`$ThisGcal $ARGS -QUx -f $FILS $CMDS | \
337            $Perl -- $packagedatamiscdir/$Perl_script $SCRIPT_ARGS`
338  else
339    result=`$ThisGcal $ARGS -QUx -f $FILS $CMDS | \
340            $Awk -f $packagedatamiscdir/$Awk_script $SCRIPT_ARGS`
341  fi
342  status=$?
343  if $Test "$status" -eq "$EXIT_SUCCESS";
344  then
345    if $Test "$mail" = yes;
346    then
347      #
348      # Detect the proper eMail address.
349      #
350      if $Test -n "$opt_mail";
351      then
352        MailAddr="$opt_mail"
353      else
354        if $Test -n "$MAILTO";
355        then
356          MailAddr="$MAILTO"
357        else
358          if $Test -n "$USER";
359          then
360            MailAddr="$USER"
361          else
362            if $Test -n "$LOGNAME";
363            then
364              MailAddr="$LOGNAME"
365            else
366              $Echo "$myname: warning: cannot send mail, address unknown" 1>&2
367              $Echo "$result" | $Sort | $Sed -e 's/^\(.......\)//' 1>&2
368              exit $EXIT_FAILURE
369            fi
370          fi
371        fi
372      fi
373      #
374      # EMail the result.
375      #
376      if $Test -n "$MAILPROG";
377      then
378        ThisMail="$MAILPROG"
379      fi
380      if $Test "$debug" = no;
381      then
382        $Echo "$result" | $Sort | $Sed -e 's/^\(.......\)//' | \
383          $ThisMail -s "Mail from \`$myname' (`$Date`)" $MailAddr
384      fi
385      $Echo "$myname: result sent by $ThisMail to \`$MailAddr'"
386    else
387      #
388      # Detect a proper pager.
389      #
390      ThisPager=""
391      if $Test "$pager" = yes;
392      then
393        if $Test -n "$Pager1";
394        then
395          ThisPager="$Pager1"
396        else
397          if $Test -n "$Pager2";
398          then
399            ThisPager="$Pager2"
400          else
401            if $Test -n "$Pager3";
402            then
403              ThisPager="$Pager3"
404            fi
405          fi
406        fi
407      fi
408      #
409      # Display the result.
410      #
411      if $Test -n "$ThisPager";
412      then
413        $Echo "$result" | $Sort | $Sed -e 's/^\(.......\)//' | $ThisPager
414      else
415        $Echo "$result" | $Sort | $Sed -e 's/^\(.......\)//'
416      fi
417    fi
418  else
419    if $Test "$status" -eq "$EXIT_FAILURE";
420    then
421      $Echo "$myname: error: no sunrise/sunset times found" 1>&2
422    else
423      if $Test "$status" -eq "$EXIT_FATAL";
424      then
425        if $Test -n "$Perl";
426        then
427          $Echo "$myinternalname: error: invalid option \`$SCRIPT_ARGS' to \`$Perl' given" 1>&2
428        else
429          $Echo "$myinternalname: error: invalid option \`$SCRIPT_ARGS' to \`$Awk' given" 1>&2
430        fi
431      fi
432    fi
433  fi
434  exit $status
435else
436  $Echo "$myinternalname: error: neither \`perl' nor \`awk' available" 1>&2
437  exit $EXIT_FATAL
438fi
439