1#! /bin/sh
2#
3#  @configure_input@
4#
5#  $Id: ddiff.in 0.06 2000/03/24 00:00:06 tom Exp $
6#
7#  ddiff.in:  Uses `ddiff1[.awk|.pl]' and `ddiff2[.awk|.pl]' for creating
8#               a Gcal location resource file which contains the day/night
9#               lengths and differences for all days of the year YYYY for
10#               a definite location.
11#               A Gcal location response file is a Gcal response file
12#               consisting of one line, which passes the location name
13#               LOC, its country code CC, and the ISO-6709-co-ordinate
14#               COORD via text variables to Gcal.  The line template of
15#               a Gcal location response file is: -r$l=CC-LOC:$c=COORD
16#
17#               Usage: ddiff --help | --version
18#               Usage: ddiff [--debug] [--precise]
19#                            [--year=YYYY] LOCATION-RESPONSE-FILE
20#               Usage: ddiff [--debug] [--precise]
21#                            [--year=YYYY] CC LOCATION-NAME ISO-COORDINATE
22#
23#               `--debug':      Turn on shell debugging.
24#               `--precise':    Use more precise time values.
25#               `--year=YYYY':  Create the Gcal location resource file for the
26#                                 year YYYY, otherwise for the actual year.
27#
28#  UN*X solution.
29#
30#  Needs `test', `echo', `expr', `eval', `set', `sed', `cat', `rm', `gcal'
31#  and `perl' or `awk' for processing!
32#
33#  Returns 0 if processing was successfully.
34#  Returns 1 in case an error occured while processing.
35#  Returns 2 if neither Perl nor AWK are present for further processing.
36#
37#  Copyright (c) 2000  Thomas Esken      <esken@uni-muenster.de>
38#                      Im Hagenfeld 84
39#                      D-48147 M"unster
40#                      GERMANY
41#
42#  This software doesn't claim completeness, correctness or usability.
43#  On principle I will not be liable for ANY damages or losses (implicit
44#  or explicit), which result from using or handling my software.
45#  If you use this software, you agree without any exception to this
46#  agreement, which binds you LEGALLY !!
47#
48#  This program is free software; you can redistribute it and/or modify
49#  it under the terms of the `GNU General Public License' as published by
50#  the `Free Software Foundation'; either version 3, or (at your option)
51#  any later version.
52#
53#  You should have received a copy of the `GNU General Public License'
54#  along with this program; if not, write to the:
55#
56#
57#
58
59#
60# Basically used programs.
61#
62Rm=rm
63Cat=cat
64Sed=sed
65Set=set
66Test=test
67Echo=echo
68Expr=expr
69
70#
71# Basically used texts.
72#
73packagedatamiscdir=/usr/local/share/gcal/misc/ddiff
74#
75PACKAGE=@PACKAGE@
76VERSION=@VERSION@
77transform=@program_transform_name@
78#
79mydefaultname=ddiff
80#
81myname=`$Echo "$0" | $Sed -e 's,.*/,,' -e "$transform"`
82myversion=0.06
83myinternalname="$myname ($PACKAGE $VERSION) $myversion"
84#
85gcal_resource_file_suffix=.rc
86#
87info1="$myname: Creates a Gcal location resource file which contains the"
88info2="$myname: day/night lengths and differences for all days of the year"
89info3="$myname: YYYY for a definite location."
90info4="$myname: By definition, a Gcal location response file is a text file of"
91info5="$myname: one line with a   \`-r\$l=CC-LOCATION:\$c=COORDINATE'   contents."
92info6="$myname:"
93usage1="usage: $myname  --help | --version"
94usage2="usage: $myname  [--debug] [--precise] [--year=YYYY] LOCATION-RESPONSE-FILE"
95usage3="usage: $myname  [--debug] [--precise] [--year=YYYY] CC LOCATION-NAME ISO-COORDINATE"
96
97#
98# Some constant values (EXIT_SUCCESS(==0) and EXIT_FATAL(==2)
99#   are also returned by the processed AWK/Perl script.
100#
101EXIT_SUCCESS=0
102EXIT_FAILURE=1
103EXIT_FATAL=2
104
105#
106# The used programs.
107#
108Eval=eval
109ThisGcal=/usr/local/bin/gcal
110#
111Awk=@AWK@
112Awk_script1=$mydefaultname""1.awk
113Awk_script2=$mydefaultname""2.awk
114#
115Perl=@PERL@
116Perl_script1=$mydefaultname""1.pl
117Perl_script2=$mydefaultname""2.pl
118
119#
120# Let's check for command line arguments.
121#
122debug=no
123precise=""
124opt_year=""
125commands=""
126previous=""
127for option in $*
128do
129  #
130  # If the previous option needs an argument, assign it.
131  #
132  if $Test -n "$previous";
133  then
134    $Eval "$previous=\$option"
135    previous=""
136    continue
137  fi
138
139  case "$option" in
140    -*=)  optarg="###error###" ;;
141    -*=*) optarg=`$Echo "$option" | $Sed 's/[-_a-zA-Z0-9]*=//'` ;;
142    *)    optarg="" ;;
143  esac
144
145  #
146  # Manage the options.
147  #
148  case "$option" in
149    -help=* | -hel=* | -he=* | -h=* | -help | -hel | -he | -h | \
150    --help=* | --hel=* | --he=* | --h=* | --help | --hel | --he | --h)
151      if $Test -n "$optarg";
152      then
153        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
154        exit $EXIT_FAILURE
155      fi
156      $Echo "$info1"
157      $Echo "$info2"
158      $Echo "$info3"
159      $Echo "$info4"
160      $Echo "$info5"
161      $Echo "$info6"
162      $Echo "$usage1"
163      $Echo "$usage2"
164      $Echo "$usage3"
165      exit $EXIT_SUCCESS ;;
166
167    -version=* | -versio=* | -versi=* | -vers=* | -ver=* | -ve=* | -v=* | \
168    -version | -versio | -versi | -vers | -ver | -ve | -v | \
169    --version=* | --versio=* | --versi=* | --vers=* | --ver=* | --ve=* | --v=* | \
170    --version | --versio | --versi | --vers | --ver | --ve | --v)
171      if $Test -n "$optarg";
172      then
173        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
174        exit $EXIT_FAILURE
175      fi
176      $Echo "$myinternalname"
177      exit $EXIT_SUCCESS ;;
178
179    -debug=* | -debu=* | -deb=* | -de=* | -d=* | \
180    -debug | -debu | -deb | -de | -d | \
181    --debug=* | --debu=* | --deb=* | --de=* | --d=* | \
182    --debug | --debu | --deb | --de | --d)
183      if $Test -n "$optarg";
184      then
185        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
186        exit $EXIT_FAILURE
187      fi
188      debug=yes
189      shift ;;
190
191    -precise=* | -precis=* | -preci=* | -prec=* | -pre=* | -pr=* | -p=* | \
192    -precise | -precis | -preci | -prec | -pre | -pr | -p | \
193    --precise=* | --precis=* | --preci=* | --prec=* | --pre=* | --pr=* | --p=* | \
194    --precise | --precis | --preci | --prec | --pre | --pr | --p)
195      if $Test -n "$optarg";
196      then
197        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=.*//'`' doesn't allow an argument" 1>&2
198        exit $EXIT_FAILURE
199      fi
200      precise="--precise"
201      shift ;;
202
203    -year | -yea | -ye | -y | --year | --yea | --ye | --y)
204      previous=year ;;
205    -year=* | -yea=* | -ye=* | -y=* | --year=* | --yea=* | --ye=* | --y=*)
206      if $Test -z "$optarg" || $Test "$optarg" = "###error###";
207      then
208        $Echo "$myname: error: option \``$Echo $option | $Sed -e 's/=//g'`' requires an argument" 1>&2
209        exit $EXIT_FAILURE
210      fi
211      opt_year="$optarg"
212      shift ;;
213
214    -* | /*)
215      $Echo "$myname: error: $option: invalid option, use --help to show usage" 1>&2
216      exit $EXIT_FAILURE ;;
217
218    *)
219      if $Test -z "$commands";
220      then
221        commands="$option"
222      else
223        commands="$commands $option"
224      fi
225  esac
226done
227
228if $Test -n "$previous";
229then
230  $Echo "$myname: error: missing argument to --$previous" 1>&2
231  exit $EXIT_FAILURE
232fi
233
234#
235# Get the number of commands given and assign them to the local variables.
236#
237cmd_number=0
238cmd_1=""
239cmd_2=""
240cmd_3=""
241for cmd in $commands
242do
243  if $Test -z "$cmd_1";
244  then
245    cmd_1="$cmd"
246  else
247    if $Test -z "$cmd_2";
248    then
249      cmd_2="$cmd"
250    else
251      if $Test -z "$cmd_3";
252      then
253        cmd_3="$cmd"
254      fi
255    fi
256  fi
257  cmd_number=`$Expr $cmd_number + 1`
258done
259
260#
261# Enable tracing.
262#
263if $Test "$debug" = yes;
264then
265  $Set -x
266fi
267
268#
269# Let's start processing now.
270#
271if $Test -n "$Awk" || $Test -n "$Perl";
272then
273  if $Test "$cmd_number" -eq 0 || $Test "$cmd_number" -eq 2 || $Test "$cmd_number" -gt 3;
274  then
275    $Echo "$info1"
276    $Echo "$info2"
277    $Echo "$info3"
278    $Echo "$info4"
279    $Echo "$info5"
280    $Echo "$info6"
281    $Echo "$usage1"
282    $Echo "$usage2"
283    $Echo "$usage3"
284    exit $EXIT_FAILURE
285  else
286    gcal_year=""
287    if $Test -n "$opt_year";
288    then
289      arg=`$Echo "$opt_year" | $Sed -e 's/[0-9]//g'`
290      if $Test -n "$arg";
291      then
292        $Echo "$myname: error: invalid option argument \`$opt_year' specified" 1>&2
293        $Echo "$myname: use --help to show usage" 1>&2
294        exit $EXIT_FAILURE
295      else
296        gcal_year="-u $opt_year""+""$opt_year"
297      fi
298    fi
299    if $Test "$cmd_number" -eq 1;
300    then
301      if $Test -s "$cmd_1";
302      then
303        outfile="$cmd_1""$gcal_resource_file_suffix"
304      else
305        $Echo "$myname: error: LOCATION-RESPONSE-FILE \`$cmd_1' is missing" 1>&2
306        exit $EXIT_FAILURE
307      fi
308      gcal_option="@""$cmd_1"
309    else
310      gcal_option="-r\$l=$cmd_1"-"$cmd_2":\$c="$cmd_3"
311      outfile=`$Echo "$cmd_1"-"$cmd_2""$gcal_resource_file_suffix" | \
312               $Sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
313    fi
314    #
315    if $Test -n "$Perl";
316    then
317      if $Test ! -s "$packagedatamiscdir/$Perl_script1";
318      then
319        $Echo "$myname: error: file \`$packagedatamiscdir/$Perl_script1' is missing" 1>&2
320        exit $EXIT_FAILURE
321      fi
322      if $Test ! -s "$packagedatamiscdir/$Perl_script2";
323      then
324        $Echo "$myname: error: file \`$packagedatamiscdir/$Perl_script2' is missing" 1>&2
325        exit $EXIT_FAILURE
326      fi
327    else
328      if $Test ! -s "$packagedatamiscdir/$Awk_script1";
329      then
330        $Echo "$myname: error: file \`$packagedatamiscdir/$Awk_script1' is missing" 1>&2
331        exit $EXIT_FAILURE
332      fi
333      if $Test ! -s "$packagedatamiscdir/$Awk_script2";
334      then
335        $Echo "$myname: error: file \`$packagedatamiscdir/$Awk_script2' is missing" 1>&2
336        exit $EXIT_FAILURE
337      fi
338    fi
339    #
340    # Let's set some default directories first.
341    #
342    tmpdir=/tmp
343    #
344    # Respect a $TMPDIR or a $TMP environment variable.
345    #
346    if $Test -n "$TMPDIR";
347    then
348      tmpdir="$TMPDIR"
349    else
350      if $Test -n "$TMP";
351      then
352        tmpdir="$TMP"
353      fi
354    fi
355    #
356    tmpdir=`$Echo "$tmpdir" | $Sed -e 's,/\$,,'`
357    #
358    tmpfile1="$tmpdir"/1-"$$"
359    tmpfile2="$tmpdir"/2-"$$"
360    tmpfile3="$tmpdir"/3-"$$"
361    #
362    if $Test "$debug" = no;
363    then
364      trap "$Rm -f $outfile $tmpfile1 $tmpfile2 $tmpfile3;exit $EXIT_FAILURE" 1 2 15
365    fi
366    #
367    $Echo "$myname: creating the Gcal location resource file \`$outfile', please wait..."
368    $ThisGcal -f/dev/null -QUx '-#0 -r$y=%:04*Y' > $tmpfile1
369    $ThisGcal $gcal_option \
370      @$tmpfile1 \
371      '--date-format=%>04*Y%>02*M%>02*D%1%2' \
372      '-#0*d1#999 $l %u$c d %z$c n $c %>04*Y$y@t -%>02*M$y@t -%>02*D$y@t  %t %=' \
373      -f/dev/null -Qxy -Hno $precise $gcal_year > $tmpfile2
374    #
375    # We prefer the use of Perl.
376    #
377    if $Test -n "$Perl";
378    then
379      $Perl $packagedatamiscdir/$Perl_script1 $tmpfile2 > $tmpfile3
380      $Cat $packagedatamiscdir/$Perl_script2 >> $tmpfile3
381      $Perl -- $tmpfile3 -a$outfile $tmpfile2 > $outfile
382    else
383      $Awk -f $packagedatamiscdir/$Awk_script1 $tmpfile2 > $tmpfile3
384      $Cat $packagedatamiscdir/$Awk_script2 >> $tmpfile3
385      $Awk -f $tmpfile3 -- -a$outfile $tmpfile2 > $outfile
386    fi
387    status=$?
388    if $Test "$debug" = no;
389    then
390      $Rm -f $tmpfile1 $tmpfile2 $tmpfile3
391    fi
392    if $Test "$status" -eq "$EXIT_FATAL";
393    then
394      if $Test -n "$Perl";
395      then
396        $Echo "$myinternalname: error: invalid option to \`$Perl' given" 1>&2
397      else
398        $Echo "$myinternalname: error: invalid option to \`$Awk' given" 1>&2
399      fi
400      if $Test "$debug" = no;
401      then
402        $Rm -f $outfile
403      fi
404      exit $status
405    fi
406  fi
407  exit $EXIT_SUCCESS
408else
409  $Echo "$myinternalname: error: neither \`perl' nor \`awk' available" 1>&2
410  exit $EXIT_FATAL
411fi
412