# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler DiskCacheOptions
AddConfigHelp "DisableWriteCacheOn <drive> [...]" "On some hardware the power is cut off before the disk has flushed its own hardware cache. List the devices that contain swap partitions (eg, /dev/hda) to disable the write cache before suspending."

DiskCacheDisable() {
    local i
    for i in $DISKCACHE_DISABLEDON ; do
	if ! which hdparm > /dev/null ; then
	    vecho 0 "hdparm utility is not in PATH! Is it installed? Can not disable write cache."
	    return 1
	fi
	vecho 1 "$EXE: Disabling disk cache on $i"
	hdparm -W 0 $i > /dev/null 2>&1
    done
    return 0
}

DiskCacheEnable() {
    local i
    for i in $DISKCACHE_DISABLEDON ; do
	command -v hdparm > /dev/null || return 1
	vecho 1 "$EXE: Enabling disk cache on $i"
	hdparm -W 1 $i > /dev/null 2>&1
    done
    return 0
}

DiskCacheOptions() {
    case $1 in
	disablewritecacheon)
	    if [ -z "$DISKCACHE_DISABLEDON" ] ; then
		AddSuspendHook 93 DiskCacheDisable
		AddResumeHook 93 DiskCacheEnable
	    fi
	    DISKCACHE_DISABLEDON="$DISKCACHE_DISABLEDON $2"
	    ;;
	*)
	    return 1
    esac
    return 0
}

# $Id: diskcache 1220 2009-04-01 02:06:57Z nigel $
