#!/bin/sh
# Set up rich core at bootup

# This file is part of sp-rich-core
#
# Copyright (C) 2006-2008 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


case "$1" in
  start|restart|force-reload)

	# Do not execute the script if we don't have the core dumper installed
	# (i.e. the package has been removed but not purged)

	test -x /usr/sbin/rich-core-dumper || exit 0

	# backup current core_pattern
	cat /proc/sys/kernel/core_pattern > /tmp/.core_pattern
	
	osso-product-info > /tmp/osso-product-info
		
	# setup new core_pattern
	echo '|/usr/sbin/rich-core-dumper --pid=%p --signal=%s --name=%e' > /proc/sys/kernel/core_pattern
	
	# Currently 'sysinfo-tool' returns an error when run early in the upstart right after rcS.
	# Thus commented out as it is sufficient to use 'osso-product-info'.
	# If it is still needed it would be better not to just check if executable exists, 
	# but to check its return value in the following way:
	# 		sysinfo-tool --get /device/sw-release-ver
	#  		if [ $? -eq 0 ]; then
	#			#success
	#		else
	#			#failure
	#		fi
	
	#if [ -e /usr/bin/sysinfo-tool ]; then
	#	sysinfo-tool --get /device/sw-release-ver > /tmp/osso_software_version
	#else
		release=$(grep OSSO_VERSION /tmp/osso-product-info)
		release=${release#*\'}
		release=${release%\'}
		echo $release > /tmp/osso_software_version
	#fi	
	;;

  stop)
  	# restore the backup-ed core_pattern only if it contains "rich-core-dumper" 
  	# keyword as it might have already been changed by someone else
	grep -q "rich-core-dumper" /proc/sys/kernel/core_pattern
  	if [ $? -eq 0 ]; then
		cat /tmp/.core_pattern > /proc/sys/kernel/core_pattern
	fi
  		
	test -e /tmp/osso-product-info && rm /tmp/osso-product-info
	test -e /tmp/osso_software_version && rm /tmp/osso_software_version
	test -e /tmp/.core_pattern && rm /tmp/.core_pattern
	;;
  
  *)
  	N=/etc/init.d/rich-core
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
