#!/bin/bash
#
# trace         This shell script takes care of starting and stopping
#               the trace driver, creating the tracer device if
#               necessary.
#
# chkconfig: 2345 80 20
# description: The Concurrent Trace Module

# Copyright 2002, Concurrent Computer Corporation, and distributed under
# the GNU GPL license version 2.

### BEGIN INIT INFO
# Provides: trace
# Required-Start: $local_fs $syslog
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: The Concurrent Trace Module
# Description: The Concurrent Trace Module
### END INIT INFO

# Source function library.
if [ -f /lib/lsb/init-functions ]; then
   . /lib/lsb/init-functions
else
   . /etc/init.d/functions
fi

# the below requires proc filesystem to be mounted

module=trace_driver

bye()
{
	echo
	exit $1
}

start()
{
	echo -n $"Starting trace: "

        if [ ! -d /proc/ccur ]; then
                bye 0
        fi

	# Try to load the trace_driver module.  Failure is fine at this stage,
	# as the driver might not even be in module form.

	modprobe $module >/dev/null 2>&1

	# Now we must create the /dev entries.  However, nothing need
	# be done if devfs is present and is mounted over /dev.

	mount | grep 'on /dev type devfs' >/dev/null && bye 0

	# Take a normal exit here if devfs has suppressed the traditional
	# character/block device interface.

	[ ! -r /proc/devices ] && exit 0
	[ $(wc -l </proc/devices 2>/dev/null) -le 5 ] && bye 0

	# make the traditional special devices in dev.
	# account for the major number being dynamically allocated.

	major=$(/bin/awk '$2=="tracer" {print $1}' /proc/devices)
	[ "$major" ] || bye 0
	/bin/rm -rf /dev/tracer
	/bin/mknod /dev/tracer c $major 0 || bye 1
	echo
}

stop() {
	echo -n $"Stopping trace: "
	if grep '^'$module'\>' /proc/modules >/dev/null; then
		/sbin/rmmod $module || bye 1
	fi
	/bin/rm -rf /dev/tracer
	echo
}

reload() {
	stop
	start
}

restart() {
	reload
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;

reload|restart)
	restart
	;;
*)
	echo $"Usage: $0 {start|stop|restart}"
	bye 1
esac
