#!/bin/sh
#
#
# chkconfig: 2345 49 88
# description: Set hostname

WITHOUT_RC_COMPAT=1

PROPAGATOR_IMAGE_DIR=/image
EVMS_CONF_FILE=/etc/evms.conf
RETVAL=0

# Source function library.
. /etc/init.d/functions

# find block device name corresponding to given block device name
find_device()
{
	find /sys/devices/ -path "*/$1" |head -n1|
		sed -n 's|.*block/\([^/]\+\).*|\1|p'
}

tune_evms()
{
    # exclude source devices
    local exclude=
    local dev=
    local mpoint=
    local fstype=
    local opts=
    local dummy=
    while read dev mpoint fstype opts dummy; do
	[ "$mpoint" = '/' -o "$mpoint" = "$PROPAGATOR_IMAGE_DIR" ] || continue
	dev="$(readlink -e "$dev")"
	[ -z "$dev" -o -n "${dev%%/dev/*}" ] || exclude="$exclude $(find_device ${dev##*/})"
    done </proc/mounts

    # exclude ide cdrom devices
    for n in /proc/ide/hd*/media; do
	[ -f "$n" ] || continue
	if [ "$(cat $n)" = cdrom ]; then
		exclude="$exclude $(echo "$n" |cut -d/ -f4)"
	fi
    done

    # exclude scsi cdrom devices
    for n in /dev/sr[0-9]*; do
	[ -b "$n" ] || continue
	exclude="$exclude ${n##*/}"
    done

    #exclude zram
    exclude="$exclude zram*"

    [ ! -s "$EVMS_CONF_FILE" ] ||
	sed -i "0,/^sysfs_devices /{p;d}; s/^\([[:space:]]*\)exclude[[:space:]]*=.*/\1exclude = [$exclude loop* ]/" \
		"$EVMS_CONF_FILE"

    [ ! -f /proc/sys/dev/raid/speed_limit_min ] ||
	cat /proc/sys/dev/raid/speed_limit_min >/proc/sys/dev/raid/speed_limit_max
}

start()
{
    local msg="Tuning evms config"
    echo -n "$msg "

    tune_evms
    RETVAL=$?

    [ $RETVAL -eq 0 ] && success "$msg" || failure "$msg"
    echo
    return $RETVAL
}

stop()
{
	true
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop|condstop|status)
		;;
	restart|reload|condrestart|condreload)
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
