#!/bin/bash
#
# Creates and installs the appropriate script to turn your computers
#  ultradma on on bootup.  The final script uses /sbin/hdparm
#
# Written by Troy Dawson November 18, 2000
# modified by Troy Dawson January 15, 2001
#   it now turns the dma on with a -d 1 -c 1 -S 0 -k1 (added the -c)
#
#************************
# Variables
#************************
TEMPFULL="/tmp/cudma.full"
TEMP1="/tmp/cudma1"
TEMP2="/tmp/cudma2"
DATE="$(date)"

#************************
# Setup
#************************
rm -f $TEMPFULL
rm -f $TEMP1
rm -f $TEMP2
#************************
# Build the part of the script that varies
#************************
ls -1 /proc/ide/ | grep hd | {
	while read drive
	do
		MEDIA="$(cat /proc/ide/${drive}/media)"
		if [ "$MEDIA" = "disk" ] ; then
			echo "	/sbin/hdparm -c \$VALUE -d \$VALUE -S 0 -k 1 /dev/${drive}" >> $TEMP1
			echo "	echo \"  /dev/${drive} \$(/sbin/hdparm /dev/${drive} | grep using_dma)\" " >> $TEMP2
		fi
	done
}

#************************
# Build Top Part
#************************

cat <<EOF>$TEMPFULL
#!/bin/bash
#
# Ultradma script
#
# chkconfig: 345 10 10
# description: ultradma turns dma for you hard drives
#
EOF


#************************
# Build Middle Part
#************************

echo "#  Created by create_ultradma on $DATE" >> $TEMPFULL
echo " " >> $TEMPFULL
echo "dma() {" >> $TEMPFULL

cat $TEMP1 >> $TEMPFULL
echo "} " >> $TEMPFULL
echo " " >> $TEMPFULL
echo "checkdma () { " >> $TEMPFULL
cat $TEMP2 >> $TEMPFULL

#************************
# Build Bottom Part
#************************
cat <<EOF>>$TEMPFULL
}

case "\$1" in
	start)
		VALUE=1
		dma
		;;
	stop)
		VALUE=0
		dma
		;;
	status)
		checkdma
		;;
	*)
		echo "Usage: $0 <start|stop|status>"
		;;
esac
EOF

#************************
# Install the script
#************************
echo "Installing ultradma script"
cp -f $TEMPFULL /etc/rc.d/init.d/ultradma
chmod 750 /etc/rc.d/init.d/ultradma
/sbin/chkconfig --add ultradma
/etc/rc.d/init.d/ultradma start > /dev/null

