#!/bin/bash
#
# 2002.07.21 Marcin Orlowski <carlos@wfmh.org.pl>
#
# v1.0 - initial release
#
# this script is expected to change drive parameters on 
# given bus at system boot, using hdparm tool.
# It was designed to be used on machines that boots with
# DMA mode off for disk drivers (no matter how you configure
# your kernel). Since doing hdparm ... by hand everytime
# your machine goes up may be annoying a bit, here you got
# the servant for this.
# Script uses (and therefore requires) /proc to determine
# presence of given device (so it safe to stay with
# default options trying to play with all four devices).
# Because I've experienced serious problems with my CD-RW
# device when hdparm was used agains it (it freeze the
# system when used) I added a small check to ensure only
# 'disk' media are affected. It's easy to disable it anyway.
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=hd-dma
DESC=hd-dma
HDPARM=/sbin/hdparm
DMAMODE=1
ENTRY_SEPARATOR=""

test -f ${HDPARM} || exit 0

function turnDMA()
{
local BUS
local DRIVE
BUS=$1
DRIVE=$2

if [ -d /proc/ide/${DRIVE} ] ; then

	echo -n ${ENTRY_SEPARATOR}${DRIVE}

	DRIVETYPE=`cat /proc/${BUS}/${DRIVE}/media`

	if [ ${DRIVETYPE} = 'disk' ]; then
		DRIVE=$1
	#	${HDPARM} -qd ${DMAMODE} /dev/${DRIVE}
		ENTRY_SEPARATOR=", "
		echo -n ": On"
	else
		echo -n ": ${DRIVETYPE} skipped"
	fi
fi
}



set -e

case "$1" in
  start|restart|force-reload)
	echo -n "Turning DMA mode ON for devices: "
	turnDMA ide hda
	turnDMA ide hdb
	turnDMA ide hdc
	turnDMA ide hdd
	echo "."
	;;
  stop)
   # we're NOT going to turn the DMA off on system shutdown.
	# fix it by hand if you want to warm you CPU at bed-time ;)
	;;
  *)
	N=/etc/init.d/${NAME}
	echo "Usage: ${N} {start}" >&2
	exit 1
	;;
esac

exit 0
