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