| 1 | #!/bin/bash |
| 2 | set -e |
| 3 | |
| 4 | zone="$1" |
| 5 | subdomain="$2" |
| 6 | interval_min="$3" |
| 7 | interval_avg="$4" |
| 8 | interval_mem="$5" |
| 9 | |
| 10 | now=`date +%s` |
| 11 | charge=0 |
| 12 | |
| 13 | case $subdomain in |
| 14 | '@') files=_ ;; |
| 15 | *) files=$subdomain ;; |
| 16 | esac |
| 17 | |
| 18 | if test -f $files,timings && read lastup charge <$files,timings |
| 19 | then |
| 20 | if [ $now -lt $[ $lastup + $interval_min ] ]; then |
| 21 | echo "wait $[ $lastup + $interval_min - $now ]" |
| 22 | echo >&2 "must wait at least $interval_min between updates" |
| 23 | exit 75 |
| 24 | fi |
| 25 | charge=$[ $charge + $interval_avg - ($now - $lastup) ] |
| 26 | if [ $charge -gt $interval_mem ]; then |
| 27 | echo "wait $[ $charge - $interval_mem ]" |
| 28 | echo >&2 "must wait on average $interval_avg between updates" |
| 29 | exit 75 |
| 30 | fi |
| 31 | if [ $charge -lt 0 ]; then charge=0; fi |
| 32 | fi |
| 33 | |
| 34 | sort >$files,new |
| 35 | |
| 36 | if test -f $files,data |
| 37 | then |
| 38 | set +e |
| 39 | diff >/dev/null $files,data $files,new |
| 40 | diff=$? |
| 41 | set -e |
| 42 | |
| 43 | if [ $diff = 0 ]; then echo 'unchanged'; exit 0; fi |
| 44 | if [ $diff != 1 ]; then exit 1; fi |
| 45 | fi |
| 46 | |
| 47 | echo $now $charge >$files,timings.new |
| 48 | mv -f $files,timings.new $files,timings |
| 49 | mv $files,new $files,data |
| 50 | |
| 51 | exec /usr/local/lib/userv/dyndns/install $zone |