Update to GPLv3+; update copyright notices everywhere.
[userv-utils] / dyndns / update
1 #!/bin/bash
2
3 # Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
4 # Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
5 # Copyright 1999,2003
6 # Chancellor Masters and Scholars of the University of Cambridge
7 # Copyright 2010 Tony Finch <fanf@dotat.at>
8 #
9 # This is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with userv-utils; if not, see http://www.gnu.org/licenses/.
21
22 set -e
23
24 zone="$1"
25 subdomain="$2"
26 interval_min="$3"
27 interval_avg="$4"
28 interval_mem="$5"
29
30 now=`date +%s`
31 charge=0
32
33 case $subdomain in
34 '@') files=_ ;;
35 *) files=$subdomain ;;
36 esac
37
38 if test -f $files,timings && read lastup charge <$files,timings
39 then
40 if [ $now -lt $[ $lastup + $interval_min ] ]; then
41 echo "wait $[ $lastup + $interval_min - $now ]"
42 echo >&2 "must wait at least $interval_min between updates"
43 exit 75
44 fi
45 charge=$[ $charge + $interval_avg - ($now - $lastup) ]
46 if [ $charge -gt $interval_mem ]; then
47 echo "wait $[ $charge - $interval_mem ]"
48 echo >&2 "must wait on average $interval_avg between updates"
49 exit 75
50 fi
51 if [ $charge -lt 0 ]; then charge=0; fi
52 fi
53
54 sort >$files,new
55
56 if test -f $files,data
57 then
58 set +e
59 diff >/dev/null $files,data $files,new
60 diff=$?
61 set -e
62
63 if [ $diff = 0 ]; then echo 'unchanged'; exit 0; fi
64 if [ $diff != 1 ]; then exit 1; fi
65 fi
66
67 echo $now $charge >$files,timings.new
68 mv -f $files,timings.new $files,timings
69 mv $files,new $files,data
70
71 exec /usr/share/userv/dyndns/install $zone