#!/bin/sh

# Actions for Cherokee-Panic:
#   mail   - mail the error. You need a working MTA in the system.
#   stdout - just print it
#
if test "x$CHEROKEE_PANIC_OUTPUT" != "x"; then
    action="$CHEROKEE_PANIC_OUTPUT"
else
    if [ -x /usr/sbin/sendmail ]; then
	   action=mail
    else
	   action=stdout
    fi
fi

# Redirect stderr to stdout
exec 2>&1

# Redirect all output to our mail command
(
     # Check the OS
     os=`uname`

	# We must be given a pid to look at
	if [ x$1 = x ]; then
	    echo "$0 called with no arguments."
	    exit 1
	else
	    pid=$1
	fi

	# Check for a second argument
	if [ -n "$2" ]; then
	    action="$2"
	fi

	# Mail header
	if [ x$action = xmail ]; then
	    echo "To: root"
	    echo "Subject: Segfault in Cherokee"
	    echo
	fi

	if [ $os = "Linux" ]; then 
	    BINARYNAME=`readlink "/proc/$pid/exe"`
	    known_os=yes
	elif [ $os = "SunOS" ]; then
	    BINARYNAME=`perl -e "print readlink(\"/proc/$pid/path/a.out\");"`
	    known_os=yes
	fi

	# Generic header for the report
	echo "The Cherokee 'panic action' script, $0,"
	echo "was called for pid $pid ($BINARYNAME)."
	echo
	
     # Ensure /proc/$pid exists if it should
	if test "x${known_os}" = "xyes"; then
	    if [ ! -d "/proc/$pid" ]; then
		   echo "$0: No such process: $pid"
		   exit 1
	    fi
	    if test "x${BINARYNAME}" = "x"; then 
		   echo "This means there was a problem with the program, such as a segfault."
		   echo "However, the executable could not be found for process $pid."
		   echo "It may have died unexpectedly, or you may not have permission to"
		   echo "debug the process."
		   exit 1
	    fi
	fi

	# Check the debugger
	gdb=`which gdb`
	dbx=`which dbx`

	if [ x$gdb != x ]; then
	     debugger=$gdb
	elif [ x$dbx != x ]; then
	     debugger=$dbx
	else
	     # No debugger
	     echo "This means there was a problem with the program, such as a segfault."
		echo "However, gdb was not found on your system, so the error could not be"
		echo "debugged.  Please install the gdb package so that debugging information is"
		echo "available the next time such a problem occurs."
		exit 1
	fi

	echo "Below is a backtrace for this process generated with gdb, which shows"
	echo "the state of the program at the time the error occured.  You are"
	echo "encouraged to submit this information as a bug report in the Cherokee"
	echo "bug traq system:  http://bugs.cherokee-project.com"
	echo
	echo "Operating System: `uname -a`"
	echo "Debugger: $debugger"
	echo

	# Get the backtrace
	if [ x$debugger = x$gdb ]; then 
	    
	    # Use /dev/shm as temp dir
	    test -d /dev/shm &&    \
		   TMPDIR=/dev/shm || \
		   TMPDIR=/var/tmp

	     tmp_cmd=`mktemp "$TMPDIR/chrk.XXXXXX"` || (
		    echo "ERROR: Couldn't create temp file in ${TMPDIR}"
		    exit 1
		)

		( echo "set height 0"
		  echo "print cherokee_version"
		  echo "thread apply all bt full"
		  echo "quit" ) >> $tmp_cmd

		if [ x$BINARYNAME = x ]; then
		    $gdb -x $tmp_cmd -batch --pid=$pid
		else
		    $gdb -x $tmp_cmd -batch "$BINARYNAME" "$pid"
		fi

		rm -f $tmp_cmd

	elif [ x$debugger = x$dbx ]; then
	     #Solaris
	     
	     echo "Related processes were:"
	     /usr/bin/ptree $pid
		echo

		echo "Stack(s) were:"
		/usr/bin/pstack $pid
		echo

		echo "Flags were:"
		/usr/bin/pflags $pid
		echo
		
		echo "Credentials were:"
		/usr/bin/pcred $pid
		echo

		echo "Libraries used were:"
		/usr/bin/pldd $pid
		echo

		echo "Signal-handler settings were:"
		/usr/bin/psig $pid
		echo

		echo "Files and devices in use were:"
		/usr/bin/pfiles $pid
		echo

		echo "Directory in use was:"
		/usr/bin/pwdx $pid
		echo
		
		echo "Backtrace"
	     $dbx -c "print (char *)cherokee_version; where -v -l ; quit" "$BINARYNAME" "$pid"
		echo
	fi

) | ( 
    case "$action" in
	   mail)
		  /usr/sbin/sendmail -t
		  ;;
	   stdout)
		  cat -
		  ;;
	   *)
		  echo "ERROR: Wrong action: \"$action\""
		  exit 1
    esac;
)
