#!/bin/sh
#
#    Sybase Raw disk I/O device setup and maintenance script
#

PATH="/sbin:/usr/bin:$PATH"

# SYBASE should be present
if [ ! "${SYBASE}" ]; then

    # If SYBASE was not setup try to setup by query RPM
    export SYBASE=`rpm -q --queryformat \
			'%{installprefix}\n' sybase-ase-11.0.3.3`
fi

if [ ! -x "${SYBASE}/install/sybdevices.sh" ]; then

    # If this does not lead to this script stop now.
    echo "The environment setup for SYBASE could not be determined"
    exit 1
fi


# Determine if the raw utility and kernel support is present
if [ ! -x /usr/bin/raw ]; then
    echo "This machine does not contain /usr/bin/raw"
    exit 1
fi

if [ ! -c /dev/raw ]; then
    echo "This machine does not contain the 'raw' administration device"
    exit 1
fi


# Determine which disk device are present in the system
DISK_DEV=/tmp/.sybdev.$$
PROC_IDE=/proc/ide
PROC_SCSI=/proc/scsi

if [ ! -d /proc/ide -o ! -d /proc/scsi ]; then

    # This can not be a valid configuration
    echo "The system does not allow for device determination"
    exit 1
fi
cp /dev/null ${DISK_DEV}

# 1st go through all EIDE and possible UDMA/66 devices
find ${PROC_IDE} -type l -print | (
    while read device_link
    do
	MEDIA=`cat $device_link/media`
	if [ "${MEDIA}" = "disk" ]; then
	    BLK_SPEC="/dev/"`basename $device_link`
	    if [ -b ${BLK_SPEC} ]; then
		echo ${BLK_SPEC}
	    fi
	fi
    done
) | sort >> ${DISK_DEV}
ls -1 /dev/sd? >> ${DISK_DEV}

case $1 in

    "show")
	for dev in `cat ${DISK_DEV}`
	do
	    fdisk -l $dev | tr -d '\*' | tr -d '\+' | \
		awk '{ if ($5 == 60) print $1"\t"$4" blocks" }'
	done
	;;

    "create")
	rawcnt=`$0 show | wc -l` ; let rawcnt=rawcnt+1
	let minor=1
	while [ $minor -lt $rawcnt ]
	do
	    if [ ! -c /dev/raw${minor} ]; then
		rm -f /dev/raw${minor}
		mknod --mode=660 /dev/raw${minor} c 162 $minor
	    fi
	    chown sybase.sybase /dev/raw${minor}
	    echo "/dev/raw${minor} c 162 $minor"
	    let minor=minor+1
	done
	;;

    "bind")
	let minor=1
	for bind in `$0 show | awk '{ print $1 }' | tr '\n' ' '`
	do
	   raw /dev/raw${minor} $bind
	   chown sybase.sybase /dev/raw${minor} $bind
	   chmod 660 /dev/raw${minor} $bind
	   let minor=minor+1
	done
	;;

    "list")
	raw -a
	;;

    *)
	echo "usage $0: [list|create|bind]"
	;;

esac

rm -f ${DISK_DEV}
