#!/bin/sh
# @file corefiles_control
# 
# Copyright (c) 2009 Palm, Inc. or its subsidiaries.
# All rights reserved.
# 
# @brief  Control coredump/minicore facility
# 
# @author Daniel Gunn
# 
# @version  1.0  2009-10-22  <>  Initial creation

PROGNAME=$0


. /etc/default/corefile_utils.sh

# figure out whats already setup
if coredump_enabled; then
    CD=1
else
    CD=0
fi
if minicore_enabled; then
    MC=1
else
    MC=0
    MCC=0
fi
MCC=$(minicore_timeout)

#echo MC $MC MCC $MCC CD $CD

usage() {
echo "Usage: $PROGNAME [ -c | -C ] [ -m | [ -M [ -t timeout ] ] ]"
echo ""
echo "Enable/Disable the generation of full corefiles. If enabled, dying"
echo "processes will generate core files in $CORE_FILE_DIR."
echo ""
echo "Enable/Disable the minicore mechanism; minicores will be triggered to"
echo "generate a report through rdxd."
echo ""
echo "Full cores do not pass through rdxd, only remain on the local"
echo "device, and are not cleaned up automatically."
echo ""
echo "  $PROGNAME     " 
echo "    [-c]              # disable corefiles"
echo "    [-C]              # enable corefiles"
echo "    [-m]              # disable minicores"
echo "    [-M]              # enable minicores"
echo "    [-t time]         # set timeout for minicores to 'time' seconds"
}

if [ $# -eq 0 ]; then
    usage
    exit 2
fi

while getopts "cCmMt:" OPTION $@
do
    case $OPTION in
        c) CD=0 ;;
        C) CD=1 ;;
        m) MC=0 ;;
        M) MC=1 ;;
        t) MCC=${OPTARG} ;;
        ?) usage
        exit 2
        ;;
    esac
done

rootfs_open -t
if [ $CD -eq 1 ]; then
    echo "Enabling corefiles. Please look for cores in $CORE_FILE_DIR."
    touch $COREDUMP_MARK
else 
    if [ -n "$COREDUMP_MARK" -a -e "$COREDUMP_MARK" ]; then
        echo "Disabling corefiles."
        rm -rf $COREDUMP_MARK
    fi
fi
if [ $MC -eq 1 ]; then
    echo "Enabling minicores with timeout $MCC"
    echo $MCC > $MINICORE_MARK
else
    echo "Disabling minicores."
    echo 0 > $MINICORE_MARK
fi
echo "System must reboot for changes to take effect.."
