1#! @SCRIPT_SH@
2#	$Id$
3#
4# HylaFAX Facsimile Software
5#
6# Copyright (c) 1994-1996 Sam Leffler
7# Copyright (c) 1994-1996 Silicon Graphics, Inc.
8# HylaFAX is a trademark of Silicon Graphics
9#
10# Permission to use, copy, modify, distribute, and sell this software and
11# its documentation for any purpose is hereby granted without fee, provided
12# that (i) the above copyright notices and this permission notice appear in
13# all copies of the software and related documentation, and (ii) the names of
14# Sam Leffler and Silicon Graphics may not be used in any advertising or
15# publicity relating to the software without the specific, prior written
16# permission of Sam Leffler and Silicon Graphics.
17#
18# THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
19# EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
20# WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
21#
22# IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
23# ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
24# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
25# WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
26# LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27# OF THIS SOFTWARE.
28#
29
30#
31# wedged deviceID device
32#
33# Do something when a modem looks irretrievably wedged.
34#
35if [ $# != 2 ]; then
36    echo "Usage: $0 deviceID device"
37    exit 1
38fi
39
40test -f etc/setup.cache || {
41    SPOOL=`pwd`
42    cat<<EOF
43
44FATAL ERROR: $SPOOL/etc/setup.cache is missing!
45
46The file $SPOOL/etc/setup.cache is not present.  This
47probably means the machine has not been setup using the faxsetup(@MANNUM1_8@)
48command.  Read the documentation on setting up HylaFAX before you
49startup a server system.
50
51EOF
52    exit 1
53}
54. etc/setup.cache
55
56#
57# Redirect errors to a tty, if possible, rather than
58# dev-nulling them or allowing them to creep into
59# the mail.
60#
61if $TTYCMD >/dev/null 2>&1; then
62    ERRORSTO=`$TTYCMD`
63else
64    ERRORSTO=/dev/null
65fi
66#
67# Command line params
68#
69devID=$1
70device=$2
71
72#
73# Variables customizable through etc/FaxDispatch
74#
75TOADDR=FaxMaster
76FROMADDR=fax
77WEDGED_EMAIL_INTERVAL=5		# minutes: at most 1 wedged email every X mins
78WEDGED_DISABLE_FAXGETTY=	# if set, faxgetty disabled from inittab
79
80if [ -f etc/FaxDispatch ]; then
81    . etc/FaxDispatch
82fi
83
84#
85# Internal variables
86#
87
88# unix secs since epoch for systems without GNU date +%s extension
89unixtime() {
90    TZ=GMT date "+%Y %j %H %M %S" | $SED 's/ 0*/ /g' | {
91    read year yday hour min sec
92    expr  $sec + $min \* 60 + $hour \* 3600 \
93	+ \(  \( $year - 1969 \) / 4 \
94	    - \( $year / 100 - 19 \) \
95	    + \( $year / 400 - 4  \) \
96	    + $yday - 1 \
97	   \) \* 86400 \
98	+ \( $year - 1970 \) \* 31536000
99    }
100}
101
102tty=`basename $device`
103wedged_last_time=0		# seconds: unix timestamp
104wedged_interval=0		# minutes
105wedged_log_file="tmp/${devID}_last_wedged_email"
106wedged_current_time=`date +%s`	# seconds: unix timestamp
107# If it's not a number (no GNU date), fallback to internal function
108wedged_current_time=`expr "$wedged_current_time" + 0 2>/dev/null || unixtime`
109
110
111#
112# Read last time 'wedged modem' email was sent
113#
114if [ -r $wedged_log_file ]; then
115    # read stored timestamp
116    read wedged_last_time < $wedged_log_file 2>/dev/null
117    # Set it to 0 if it's not a number
118    wedged_last_time=`expr "$wedged_last_time" + 0 2>/dev/null || echo 0`
119    # shouldn't happen, just in case...
120    if [ $wedged_last_time -gt $wedged_current_time ]; then
121	wedged_last_time=0
122    fi
123fi
124
125#
126# Minutes since last 'wedged modem' email was sent
127#
128wedged_interval=`expr \( $wedged_current_time - $wedged_last_time \) / 60`
129
130#
131# Send 'wedged modem' email if either is true:
132# 1. there's no log file (email was never sent before or someone deleted it)
133# 2. email was sent longer than WEDGED_EMAIL_INTERVAL minutes ago
134# Cases like 'modem was wedged 1 year ago, and is now again wedged for the
135# first time', fall into #1 if someone deleted the log file, into #2 if
136# log file was left in place.
137#
138if [ ! -r $wedged_log_file \
139	-o $wedged_interval -gt $WEDGED_EMAIL_INTERVAL ]; then
140
141    #
142    # Write current timestamp into logfile under tmp/
143    #
144    if [ $wedged_current_time -gt 0 ]; then
145	$RM -f $wedged_log_file # symlink? :-)
146	$CAT<<EOF > $wedged_log_file
147$wedged_current_time
148
149This is a temp file written and read by bin/wedged to rate-limit
150the emails it sends about the wedged status of device $device.
151
152The first line contains a timestamp for when the last email was sent.
153
154This file is never deleted automatically, there's no need to do it
155and it may be useful to know when/if a device had last a problem.
156However, you can safely delete it at any time if you wish, it will
157be recreated when needed.
158
159last modified: `date`
160
161EOF
162    fi
163
164    #
165    # Send 'modem is wedged' email
166    #
167    ($CAT<<EOF
168To: $TOADDR
169From: The HylaFAX Receive Agent <$FROMADDR>
170Subject: modem on $device appears wedged
171
172The HylaFAX software thinks that there is a problem with the modem
173on device $device that needs attention; repeated attempts to
174initialize the modem have failed.
175
176Consult the server trace logs for more information on what is happening.
177
178You will be notified again after $WEDGED_EMAIL_INTERVAL minutes if the problem persists.
179
180EOF
181    if [ -x etc/resetmodem ]; then
182	echo "An attempt to reset the modem has been made."
183	etc/resetmodem $device
184    fi
185
186    #
187    # Disable faxgetty
188    # NB: this is for an System V-style system.
189    #
190    if [ -f /etc/inittab ] && [ -n "$WEDGED_DISABLE_FAXGETTY" ]; then
191	ed - /etc/inittab<<EOF
192/^[^#].*:respawn:.*faxgetty .*$tty/s/respawn/off/
193w
194q
195EOF
196	#
197	# ed doesn't appear to have consistent exit
198	# status under SysV-style systems (does under BSD);
199	# this means checking if the above succeeded is
200	# problematic.
201	#
202	if [ $? -ne 0 ] && /bin/kill -1 1; then
203	    cat<<EOF
204
205The $tty entry in /etc/inittab that spawns faxgetty on $device has
206been disabled.  After you have figured out what is wrong you may
207want to restart this process.
208EOF
209	fi
210    fi
211
212    ) 2>$ERRORSTO | $SENDMAIL -f$FROMADDR -oi $TOADDR
213fi
214
215exit 0
216