#!/bin/bash
#
# op_dump_25 is a script to dump data for OProfile
#
# Copyright 2002
# Read the file COPYING
#
# Authors: Will Cohen

grep oprofilefs /proc/filesystems >/dev/null
if [ "$?" -eq 0 ]; then
	KERNEL_SUPPORT=yes
fi

DIR="/var/lib/oprofile"
if test "$KERNEL_SUPPORT" = "yes"; then
	MOUNT="/dev/oprofile"
else
	MOUNT="/proc/sys/dev/oprofile"
fi

if [ ! -w $MOUNT/dump ]; then
	echo "Cannot initiate dump." >& 2
	exit 1
fi

#make sure that the daemon is running
if test -e "$DIR/lock"; then
	OPROFILED_PID=`cat $DIR/lock`
	ps -p $OPROFILED_PID | grep $OPROFILED_PID > /dev/null
	if [ "$?" -ne 0 ]; then
		echo "No daemon running" >& 2
		exit 1
	fi
else
	echo "No daemon running" >& 2
	exit 1
fi

if test "$KERNEL_SUPPORT" = "yes"; then
	# find current time
	TMPFILE=`mktemp /tmp/oprofile.XXXXXX` || exit 1
	echo 1 > $MOUNT/dump
	# loop until there is a file to check
	while [ ! -e "$DIR/complete_dump" ]
	do
		sleep 1;
	done
	# loop until modification data of $MOUNT/dump after TMPFILE
	while [ "$TMPFILE" -nt "$DIR/complete_dump" ]
	do
		sleep 1;
	done
	rm $TMPFILE
else

	echo 1 > $MOUNT/dump
fi
