Release 1.1.1.
[rsync-backup] / lib.sh.in
1 ### -*-bash-*-
2 ###
3 ### Common utilities for rsync-backup scripts
4 ###
5 ### (c) 2014 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the `rsync-backup' program.
11 ###
12 ### rsync-backup is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### rsync-backup is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with rsync-backup; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 ###--------------------------------------------------------------------------
27 ### Configuration.
28
29 VERSION=@VERSION@
30 pkgdatadir=@pkgdatadir@
31 mntbkpdir=@mntbkpdir@
32 logdir=@logdir@
33 fshashdir=@fshashdir@
34 conf=@sysconfdir@/rsync-backup.conf
35
36 INDEXDB=@pkglocalstatedir@/index.db
37
38 SNAPDIR=$mntbkpdir/snap
39 STOREDIR=$mntbkpdir/store
40 METADIR=$mntbkpdir/meta
41
42 config () {
43 echo
44 cat <<EOF
45 conf = $conf
46 mntbkpdir = $mntbkpdir
47 fshashdir = $fshashdir
48 logdir = $logdir
49 EOF
50 }
51
52 ###--------------------------------------------------------------------------
53 ### Date hacking.
54
55 parsedate () {
56 date=$1
57 ## Parse an ISO8601 DATE, and set YEAR, MONTH, DAY appropriately (and
58 ## without leading zeros).
59
60 ## Extract the components of the date and trim leading zeros (which will
61 ## cause things to be interpreted as octal and fail).
62 year=${date%%-*} rest=${date#*-}; month=${rest%%-*} day=${rest#*-}
63 year=${year#0} month=${month#0} day=${day#0}
64 }
65
66 julian () {
67 date=$1
68 ## Convert an ISO8601 DATE to a Julian Day Number.
69
70 parsedate $date
71
72 ## The actual calculation: convert a (proleptic) Gregorian calendar date
73 ## into a Julian day number. This is taken from Wikipedia's page
74 ## http://en.wikipedia.org/wiki/Julian_day#Calculation but the commentary
75 ## is mine. The epoch is 4713BC-01-01 (proleptic) Julian, or 4714BC-11-24
76 ## proleptic Gregorian.
77
78 ## If the MONTH is January or February then set a = 1, otherwise set a = 0.
79 a=$(( (14 - $month)/12 ))
80
81 ## Compute a year offset relative to 4799BC-03-01. This puts the leap day
82 ## as the very last day in a year, which is very convenient. The offset
83 ## here is sufficient to make all y values positive (within the range of
84 ## the JDN calendar), and is a multiple of 400, which is the Gregorian
85 ## cycle length.
86 y=$(( $year + 4800 - $a ))
87
88 ## Compute the offset month number in that year. These months count from
89 ## zero, not one.
90 m=$(( $month + 12*$a - 3 ))
91
92 ## Now for the main event. The (153 m + 2)/5 term is a surprising but
93 ## correct trick for obtaining the number of days in the first m months of
94 ## the (shifted) year). The magic offset 32045 is what you get when you
95 ## plug the proper JDN epoch (year = -4713, month = 11, day = 24) into the
96 ## above machinery.
97 jdn=$(( $day + (153*$m + 2)/5 + 365*$y + $y/4 - $y/100 + $y/400 - 32045 ))
98
99 echo $jdn
100 }
101
102 ###----- That's all, folks --------------------------------------------------