#!/bin/sh
#
#  diskdumpctl.sh
#
#  Copyright (C) 2004  FUJITSU LIMITED
#

PROGRAM_NAME=`basename $0`

usage()
{
	echo "Usage: diskdumpctl [-u] device" >&2
	exit 1
}

# Check argument
if [ $# -le 0 -o $# -ge 3 ] ; then
	usage
fi

if [ $# = 2 -a "$1" != "-u" ] ; then
	usage
fi

# Get device file
if [ $# = 1 ] ; then
	DEVFILE=$1
	CMD="add"
else
	DEVFILE=$2
	CMD="delete"
fi

if [ ! -e $DEVFILE ] ; then
	echo "$PROGRAM_NAME: $DEVFILE does not exist!" >&2
	exit 1
fi

if [ -L $DEVFILE ] ; then
	DEVFILE=`readlink $DEVFILE`
fi

if [ ! -b $DEVFILE ] ; then
	echo "$PROGRAM_NAME: $DEVFILE is not block device!" >&2
	exit 1
fi

# Get partition number
MINOR=`echo $DEVFILE | sed 's/^[^0-9]*//'`
if [ "$MINOR" = "" ] ; then
	MINOR=0
fi

# Get sysfs mounted point
SYSFSROOT=`grep sysfs /etc/mtab | cut -d\  -f 2`
if [ ! -d $SYSFSROOT ] ; then
	echo "$PROGRAM_NAME: sysfs is not mounted!" >&2
	exit 1
fi

DEV=`basename $(echo $DEVFILE | sed 's/[0-9]*$//')`
case $DEV in
	sd*)
		DUMPFILE=$SYSFSROOT/block/$DEV/device/dump
		if [ ! -f $DUMPFILE ] ; then
			echo "$PROGRAM_NAME: $DUMPFILE does not exist!" >&2
			exit 1
		fi

		if [ "$CMD" = "add" ] ; then
			echo $MINOR > $DUMPFILE
		else
			echo -$MINOR > $DUMPFILE
		fi
		;;
	*)
	echo "$PROGRAM_NAME: $DEVFILE is not supported!" >&2
	exit 1
	;;
esac

