#!/bin/sh
#
# Startup script for the Audible Alarm
#
# author: Kirk Lawson <lklawson@heapy.com> 
#         Horms <horms@vergenet.net>
#
# description: sets an audiable alarm running by beeping at a set interval
# processname: alarm
# config: /etc/AudibleAlarm/AudibleAlarm.conf - not yet implemented
#
# Licence: GPL

prefix=/usr
exec_prefix=/usr
PIDFILE=/var/run/heartbeat-bell
#For testing
#PIDFILE=/tmp/heartbeat-bell

# Source function library.
. /etc/ha.d/shellfuncs

# What host are we running on?
us=`uname -n`

audiablealarm_start () {
	ha_log "info: $0: Starting"
    	if [ -f $PIDFILE ]; then
        	PID=`head -n 1 $PIDFILE`
		ha_log "info: $0: Appears to already be running, killing [$PID]"
		kill $PID > /dev/null
	fi
	while [ 1 ]; do 
		sleep 1  #Sleep first, incase we bail out
		echo -ne "\a" > /dev/console 
		# Uncomment this line to cause floppy drive light
		# to flash (requires fdutils package).
		# /usr/bin/floppycontrol --pollstate > /dev/null
	done&
	if ! echo $! >  $PIDFILE; then
		ha_log "info: $0: Could not write to pid file \"$PIDFILE\", bailing"
		kill $!
		exit 1
	fi
}

audiablealarm_stop () {
	ha_log "info: $0: Shutting down"
  	if [ -f $PIDFILE ]; then
		PID=`head -n 1 $PIDFILE`
		ha_log "info: $0: Appears to already be running, killing [$PID]"
		kill $PID > /dev/null
		rm -f $PIDFILE
	fi
}

# Get last argument we are passed.
for arg in "$@"
  do
    lastarg=$arg
  done


# See how we were called.
case "$lastarg" in
   start)
	for arg in "$@"
 	  do
	    if [ "$us" = "$arg" ]; then
	      # We should not start because we are on a host
	      # listed in our argument list.
              exit 0
	    fi
	  done
	audiablealarm_start
	;;
  stop)
	audiablealarm_stop
	;;
  restart)
	$0 stop
	$0 start
	;;
  status)
  	if [ -f $PIDFILE ]; then
		echo running
	else
		echo stopped
	fi
	;;

  *)
	echo "Usage: [node1 node2 ... ] {start|stop|restart|status}"
	echo "  The node list is an optional space delimited"
	echo "  list of hosts that should never sound the alarm."
	echo "$Id: AudibleAlarm.in,v 1.8 2003/10/29 16:27:18 lars Exp $";
	exit 1
esac

exit 0
