#!/bin/sh
#
# rawdevices       This shell script assignes rawdevices to block devices
#
# chkconfig: - 56 44
# description: This scripts assignes raw devices to block devices \
#              (such as hard drive partitions). This is for the use \
#	       of applications such as Oracle. You can set up the \
#	       raw device to block device mapping by editing \
#	       the file /etc/sysconfig/rawdevices.
# config: /etc/sysconfig/rawdevices

WITHOUT_RC_COMPAT=1

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

CONFIG=/etc/sysconfig/rawdevices
[ -s "$CONFIG" ] &&
	grep -qs '^[[:space:]]*[^#[:space:]]' "$CONFIG" ||
	exit 0

LOCKFILE=/var/lock/subsys/rawdevices

assign_raw()
{
   grep -v '^ *#' "$CONFIG" | while read RAW BLOCK; do 
     if [ -n "$RAW" -a -n "$BLOCK" ]; then 
         if [ "${RAW%/*}" = "/dev" -a -d /dev/raw ]; then
           echo "  Please correct your $CONFIG:"
           echo "     rawdevices are now located in the directory /dev/raw/ "
           exit 0
         fi
         if [ "${RAW%/*}" = "/dev/raw" -a -f /dev/raw ]; then
           echo "  Please correct your $CONFIG:"
           echo "     rawdevices are not yet located in the directory /dev/raw/ "
           exit 0
         fi

       echo "           $RAW  -->   $BLOCK"; 
       raw $RAW $BLOCK
     fi
   done
}

# See how we were called.
case "$1" in
	start|reload|restart|condreload|condrestart)
		if grep -qsv '^ *#' "$CONFIG"; then
			echo $"Assigning devices: "
			assign_raw
			echo "done"
		fi
		touch "$LOCKFILE"
		;;
	stop|condstop)
		rm -f "$LOCKFILE"
		;;
	status)
		if [ "$(id -u)" -eq 0 ]; then 
			raw -qa
		else
			echo $"You need to be root to use this command."
	        fi
		;;
	*)
		msg_usage "${0##*/} {start|stop|status}"
		exit 1
esac

exit 0
