chiark / gitweb /
dump control command
[inn-innduct.git] / backends / actmerge.in
1 #! /bin/sh
2 # fixscript will replace this line with code to load innshellvars
3
4 # @(#) $Id: actmerge.in 2674 1999-11-15 06:28:29Z rra $
5 # @(#) Under RCS control in /usr/local/news/src/inn/local/RCS/actmerge.sh,v
6 #
7 # actmerge - merge two active files
8 #
9 # usage:
10 #       actmerge [-s] ign1 ign2 host1 host2 
11 #
12 #       -s      - write status on stderr even if no fatal error
13 #       ign1    - ignore file for host1
14 #       ign2    - ignore file for host2
15 #       host1   - 1st active file or host
16 #       host2   - 2nd active file or host
17 #
18 # The merge of two active files are sent to stdout.  The status is
19 # written to stderr.
20
21 # By: Landon Curt Noll          chongo@toad.com         (chongo was here /\../\)
22 #
23 # Copyright (c) Landon Curt Noll, 1996.
24 # All rights reserved.
25 #
26 # Permission to use and modify is hereby granted so long as this 
27 # notice remains.  Use at your own risk.  No warranty is implied.
28
29 # preset vars
30 #
31
32 # Our lock file
33 LOCK=${LOCKS}/LOCK.actmerge
34 # where actsync is located
35 ACTSYNC=${PATHBIN}/actsync
36 # exit value of actsync if unable to get an active file
37 NOSYNC=127
38 # args used by actsync a fetch of an active file
39 FETCH="-b 0 -d 0 -g 0 -o aK -p 0 -q 12 -s 0 -t 0 -v 2"
40 # args used to merge two active files
41 MERGE="-b 0 -d 0 -g 0 -m -o aK -p 0 -q 12 -s 0 -t 0 -v 3"
42 # unless -q
43 QUIET=true
44
45 # parse args
46 #
47 if [ $# -gt 1 ]; then
48     if [ X"-s" = X"$1" ]; then
49         QUIET=
50         shift
51     fi
52 fi
53 if [ $# -ne 4 ]; then
54     echo "usage: $0 ign1 ign2 host1 host2" 1>&2
55     exit 1
56 fi
57 ign1="$1"
58 if [ ! -s "$ign1" ]; then
59     echo "$0: host1 ignore file not found or empty: $ign1" 1>&2
60     exit 2
61 fi
62 ign2="$2"
63 if [ ! -s "$ign2" ]; then
64     echo "$0: host2 ignore file not found or empty: $ign2" 1>&2
65     exit 3
66 fi
67 host1="$3"
68 host2="$4"
69
70
71 # Lock out others
72 #
73 trap 'rm -f ${LOCK}; exit 1' 0 1 2 3 15
74 shlock -p $$ -f ${LOCK} || {
75     echo "$0: Locked by `cat ${LOCK}`" 1>&2
76     exit 4
77 }
78
79 # setup
80 #
81 tmp="$TMPDIR/.merge$$"
82 act1="$TMPDIR/.act1$$"
83 act2="$TMPDIR/.act2$$"
84 trap "rm -f $tmp ${LOCK} $act1 $act2; exit" 0 1 2 3 15
85 rm -f "$tmp"
86 touch "$tmp"
87 chmod 0600 "$tmp"
88 rm -f "$act1"
89 touch "$act1"
90 chmod 0600 "$act1"
91 rm -f "$act2"
92 touch "$act2"
93 chmod 0600 "$act2"
94
95 # try to fetch the first active file
96 #
97 echo "=-= fetching $host1" >>$tmp
98 eval "$ACTSYNC -i $ign1 $FETCH /dev/null $host1 > $act1 2>>$tmp"
99 status=$?
100 if [ "$status" -ne 0 ]; then
101
102     # We failed on our first try, so we will trice knock 3 times after
103     # waiting 5 minutes.
104     #
105     for loop in 1 2 3; do
106
107         # wait 5 minutes
108         sleep 300
109
110         # try #1
111         eval "$ACTSYNC -i $ign1 $FETCH /dev/null $host1 > $act1 2>>$tmp"
112         status=$?
113         if [ "$status" -eq "$NOSYNC" ]; then
114             break;
115         fi
116
117         # try #2
118         eval "$ACTSYNC -i $ign1 $FETCH /dev/null $host1 > $act1 2>>$tmp"
119         status=$?
120         if [ "$status" -eq "$NOSYNC" ]; then
121             break;
122         fi
123
124         # try #3
125         eval "$ACTSYNC -i $ign1 $FETCH /dev/null $host1 > $act1 2>>$tmp"
126         status=$?
127         if [ "$status" -eq "$NOSYNC" ]; then
128             break;
129         fi
130     done
131
132     # give up
133     #
134     if [ "$status" -ne 0 ]; then
135         echo "=-= `date` merge $host1 $host2 exit $status" 1>&2
136         sed -e 's/^/    /' < "$tmp" 1>&2
137         exit "$status"
138     fi
139 fi
140 if [ ! -s "$act1" ]; then
141     echo "$0: host1 active file not found or empty: $act1" 1>&2
142     exit 5
143 fi
144
145 # try to fetch the second active file
146 #
147 echo "=-= fetching $host2" >>$tmp
148 eval "$ACTSYNC -i $ign2 $FETCH /dev/null $host2 > $act2 2>>$tmp"
149 status=$?
150 if [ "$status" -ne 0 ]; then
151
152     # We failed on our first try, so we will trice knock 3 times after
153     # waiting 5 minutes.
154     #
155     for loop in 1 2 3; do
156
157         # wait 5 minutes
158         sleep 300
159
160         # try #1
161         eval "$ACTSYNC -i $ign2 $FETCH /dev/null $host2 > $act2 2>>$tmp"
162         status=$?
163         if [ "$status" -eq "$NOSYNC" ]; then
164             break;
165         fi
166
167         # try #2
168         eval "$ACTSYNC -i $ign2 $FETCH /dev/null $host2 > $act2 2>>$tmp"
169         status=$?
170         if [ "$status" -eq "$NOSYNC" ]; then
171             break;
172         fi
173
174         # try #3
175         eval "$ACTSYNC -i $ign2 $FETCH /dev/null $host2 > $act2 2>>$tmp"
176         status=$?
177         if [ "$status" -eq "$NOSYNC" ]; then
178             break;
179         fi
180     done
181
182     # give up
183     #
184     if [ "$status" -ne 0 ]; then
185         echo "=-= `date` merge $host1 $host2 exit $status" 1>&2
186         sed -e 's/^/    /' < "$tmp" 1>&2
187         exit "$status"
188     fi
189 fi
190 if [ ! -s "$act2" ]; then
191     echo "$0: host2 active file not found or empty: $act2" 1>&2
192     exit 6
193 fi
194
195 # merge the 2 active files to stdout
196 #
197 echo "=-= merging $host1 and $host2" >>$tmp
198 eval "$ACTSYNC $MERGE $act1 $act2" 2>>$tmp
199 status=$?
200 if [ "$status" -ne 0 ]; then
201     echo "=-= `date` merge $host1 $host2 exit $status" 1>&2
202     sed -e 's/^/    /' < "$tmp" 1>&2
203     exit "$status"
204 fi
205
206 # if not -q, send status to stderr
207 #
208 if [ -z "$QUIET" ]; then
209     echo "=-= `date` merge $host1 $host2 successful" 1>&2
210     sed -e 's/^/    /' < "$tmp" 1>&2
211 fi
212
213 # all done
214 #
215 rm -f "${LOCK}"
216 exit 0