#!/bin/sh
#
# This file is part of sp-error-visualizer
#
# Copyright (C) 2007 Nokia Corporation.
#
# Contact: Eero Tamminen <eero.tamminen@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

# A simple start/stop initscript for the sp-error-visualizer tool 
# with the default syslog matching rule configuration

if test -f /etc/default/sp-error-visualizer; then
	    . /etc/default/sp-error-visualizer
else
	echo "No default configuration for error visualizer."
	echo "*ALL* syslog events will be displayed!"
fi

PIDFILE="/var/run/sp-error-visualizer.pid"

get_and_store_visualizer_pid() {

# As we start the error-visualizer in an async fashion,
# pidof might initially return an empty PID. On the other
# hand, waiting always a fixed amount of time is ugly,
# so let's check first whether we have a valid value already and
# try again once if necessary.

    VISUALIZER_PID=$(pidof -s sp-error-visualizer)
    
    if [ "$VISUALIZER_PID" -lt 2 ]; then
	sleep 3
	VISUALIZER_PID=$(pidof -s sp-error-visualizer)
    fi
    
    if [ "$VISUALIZER_PID" -lt 2 ]; then
	echo "Couldn't start sp-error-visualizer!"
    else
	echo $VISUALIZER_PID > /var/run/sp-error-visualizer.pid
    fi
}

case "$1" in
  start)
  	if test -f /var/run/sp-error-visualizer.pid; then
		echo "sp-error-visualizer already running."
	else
        	echo "Setting up the graphical display of errors"

		if [ -e /dev/log ]; then
		echo "Syslog daemon is present, looking for the logfile..."

		# Check for alternative syslog locations

		if [ -e /var/ftd-log/syslog ]; then
		    LOGFILE="/var/ftd-log/syslog"
		elif [ -e /var/log/syslog ]; then
		    LOGFILE="/var/log/syslog"
		else
		    echo "No logfiles in expected locations!"
		    echo "Aborting, check your syslog configuration."
		    exit 1
		fi

		echo "Log found from $LOGFILE, piping it to the visualizer"
		run-standalone.sh /usr/bin/sp-error-visualizer $VISUALIZER_OPTS -m $LOGFILE &
		sleep 1
		else
			echo "No syslog file, using syslog socket"
			run-standalone.sh sp-error-visualizer -s $VISUALIZER_OPTS &
			# a flag to indicate that syslog socket was
			# created by us
			touch /var/run/sp-error-visualizer-syslog-sock
		fi
			get_and_store_visualizer_pid
	fi
        ;;
  stop)
        echo "Shutting down the graphical display of errors"

	if [ -e /var/run/sp-error-visualizer.pid ]; then
	    kill $(cat /var/run/sp-error-visualizer.pid)
	    rm /var/run/sp-error-visualizer.pid
	    echo "sp-error-visualizer stopped."
	    
	    if [ -e /var/run/sp-error-visualizer-syslog-sock ]; then
		rm /dev/log
		rm /var/run/sp-error-visualizer-syslog-sock
	    fi
	else
	    echo "The sp-error-visualizer was not running."
	fi
        ;;
    restart)
	echo "Restarting the sp-error-visualizer"
	/etc/init.d/sp-error-visualizer stop
	/etc/init.d/sp-error-visualizer start
	;;
    force-reload)
	# As the sp-error-visualizer does not currently support
	# runtime configuration reloading, this action is equivalent
	# of restarting the service for now.

	echo "Force-reloading the sp-error-visualizer"
	/etc/init.d/sp-error-visualizer restart
	;;
  *)
        N=/etc/init.d/sp-error-visualizer
        echo "Usage: $N {start|stop|restart|force-reload)" >&2
        exit 1
        ;;
esac
