#!/bin/bash
#
# (c) Bernhard Walle <bwalle@suse.de>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; You may only use version 2 of the License, you have no option to
# use any other version.
#
# 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., 675 Mass
# Ave, Cambridge, MA 02139, USA.
#

KDUMP_SHARED=/usr/share/kdump-helpers/kdump-shared_functions
KDUMP_CONFIG=/etc/sysconfig/kdump
MAKEDUMPFILE=makedumpfile
KDUMP_URL_PARSER=kdump-url_parser
GET_KERNEL_VERSION=get_kernel_version
COPY_PROGRESS=kdump-copy_progress
MOUNTPOINT=/mnt
VMCORE=/proc/vmcore

###############################################################################
# Prints debugging output if KDUMP_VERBOSE & 8 is true.
function print_debug
{
    if [ $(($KDUMP_VERBOSE & 8)) -gt 0 ] ; then
        echo $*
    fi
}

###############################################################################
# Checks the configuration
function check_config
{
    if [ ! -r $KDUMP_CONFIG ] ;then
        echo "Kdump configuration file doesn't exist"
        exit 1
    fi
}

###############################################################################
# Checks the available binaries
function check_binaries
{
    if ! which $KDUMP_URL_PARSER >/dev/null 2>&1 ; then
        echo "kdump-url_parser is needed, install kdump-helpers"
        exit 1
    fi
}


###############################################################################
# get vmcore size in MB
get_mem_size()
{
    s=`stat -c '%s' /proc/vmcore`
    expr \( $s + 1048575 \) / 1048576
}

###############################################################################
# purge old dump directories if the number of directories
# exceeds $KDUMP_KEEP_OLD_DUMPS
purge_old_dumps()
{
    local keep_old_dumps=$KDUMP_KEEP_OLD_DUMPS

    # -2 (delete all dumps) means logic 0 here
    if [ $keep_old_dumps -eq -2 ] ; then
        keep_old_dumps=0
    fi

    dirs=`ls -d $KDUMP_SAVEDIR/*-*-*-*:* 2>/dev/null | sort`
    numdirs=`echo $dirs | wc -w`
    for d in $dirs; do
        if [ $numdirs -le $keep_old_dumps ]; then
            break;
        fi
        echo " Expiring old dump $d"
        rm -rf $d
        numdirs=`expr $numdirs - 1`
    done
}

###############################################################################
# get the free disk space of the given directory in MB
parse_rest_size()
{
    test -d "$1" || mkdir -p "$1"
    hdread=""
    df -P "$1" | while read fs bl us av rest; do
	test "$hdread" && expr $av / 1024
	hdread="$fs"
    done
}

###############################################################################
# Main

if [ ! -r "$KDUMP_SHARED" ] ; then
    echo "$KDUMP_SHARED missing"
    exit 1
fi
source $KDUMP_SHARED

if ! source_config ; then
    exit 1
fi

check_binaries


protocol=$($KDUMP_URL_PARSER --protocol $KDUMP_SAVEDIR)
if [ -z "$protocol" ] ;then
    echo "Invalid protocol in $KDUMP_SAVEDIR, exiting"
    exit 1
fi

KDUMP_SAVEDIR=$($KDUMP_URL_PARSER --path $KDUMP_SAVEDIR)

if [ "$protocol" = "file" ]  ; then
	#
	# clean old stuff
	dumpsize=`get_mem_size`
	if [ -z "$dumpsize" -o "$dumpsize" = 0 ]; then
	    echo "Zero size vmcore"
        exit 1
	fi

    # special value 0 is used for "do nothing" and -1 is reserved
	if [ $KDUMP_KEEP_OLD_DUMPS -gt 0 \
            -o $KDUMP_KEEP_OLD_DUMPS -lt -1 ]; then
	    purge_old_dumps
	fi

	if [ $KDUMP_FREE_DISK_SIZE -gt 0 ]; then
	    restsize=`parse_rest_size "$KDUMP_SAVEDIR"`
	    needsize=`expr $dumpsize + $KDUMP_FREE_DISK_SIZE`
	    if [ $restsize -lt $needsize ]; then
		echo " No enough space left on dump device ($restsize MB)"
        exit 1
	    fi
	fi
fi



# vim: set sw=4 ts=4 et:
