#!/sbin/runscript
# METHOD
# ------
# if /dev/ide exists, find all block devices beneath it named disc, cd, or
# generic.
#
# for the disc and cd ones, if there is a a matching /dev/hdX symlink and
# hdX_args is set in the config file, use hdX_args.  otherwise, if there is a
# matching /dev/discs/discX or /dev/cdroms/cdromX symlink, and discX_args or
# cdromX_args is set in the config file, use discX_args / cdromX_args.  finally,
# if all_args is set in the config file, use that.
#
# for the generic ones, sort them and look for genericX_args in the config file
# or use all_args.
#
# if /dev/ide does not exist, check the /dev/hdX entries, and see which ones
# correspond to real devices by opening them for reading.  then check hdX_args
# and all_args in the config file.
#
# for each device considered, if no args are found in the config file, do not
# run hdparm.

depend() {
	before bootmisc
}

do_hdparm() {
	if [[ -z ${args}${all_args}${!extra_args} ]] ; then
		return 0
	fi
	if [[ -n ${args:=${all_args} ${!extra_args}} ]] ; then
		local orgdevice=$(readlink -f ${device})
		if [[ -b ${orgdevice} ]] ; then
			ebegin "Running hdparm on ${device}"
			hdparm ${args} ${device} > /dev/null
			eend $?
		fi
	fi
}

scan_devfs() {
	extra_args="pata_all_args"

	# devfs compatible systems
	for device in $(find /dev/ide -name disc)
	do
		args=''

		for alias in /dev/hd?
		do
			if [ $alias -ef $device ]
			then
				device=$alias
				eval args=\${`basename $alias`_args}
				break
			fi
		done

		[ -z "$args" ] && for alias in /dev/discs/*
		do
			if [ $alias/disc -ef $device ]
			then
				device=$alias/disc
				eval args=\${`basename $alias`_args}
				break
			fi
		done

		do_hdparm

	done

	for device in $(find /dev/ide -name cd)
	do

		args=''

		for alias in /dev/hd?
		do
			if [ $alias -ef $device ]
			then
				device=$alias
				eval args=\${`basename $alias`_args}
				break
			fi
		done

		[ -z "$args" ] && for alias in /dev/cdroms/*
		do
			if [ $alias -ef $device ]
			then
				device=$alias
				eval args=\${`basename $alias`_args}
				break
			fi
		done

		do_hdparm
	done

	local count=0
	# of course, the sort approach would fail here if any of the
	# host/bus/target/lun numbers reached 2 digits..
	for device in $(find /dev/ide -name generic | sort)
	do
		eval args=\${generic${count}_args}
		do_hdparm
		((++count))
	done
}

scan_nondevfs() {
	# non-devfs compatible system
	for device in /dev/hd? /dev/sd? ; do
		case ${device} in
			/dev/hd*)  extra_args="pata_all_args" ;;
			/dev/sd*)  extra_args="sata_all_args" ;;
			*)         extra_args="" ;;
		esac

		# check that the block device really exists by
		# opening it for reading
		local errmsg status
		errmsg=$(export LC_ALL=C ; : 2>&1 <$device)
		status=$?
		if [[ -b $device ]] && \
		   [[ ${status} == 0 || ${errmsg} == *": No medium found" ]]
		then
			local conf_var="${device##*/}_args"
			args=${!conf_var}
			do_hdparm
		fi
	done
}

start() {
	if get_bootparam "nohdparm" ; then
		ewarn "Skipping hdparm init as requested in kernel cmdline"
		return 0
	fi

	if [[ -e /dev/.devfsd ]] && [[ -d /dev/ide ]] ; then
		scan_devfs
	else
		scan_nondevfs
	fi
}
