#!/bin/sh -e
# NAME=Array configurator
# DESCRIPTION=It is not a real test. Simply, it can create specified array configuration on disk controller with hard drives. Currently it either passes command line string to einarc, or (if specified "passthrough", "optimal" or "clear") necessary raid-wizard will be used instead.
# DESTROYS_HDD=true
# IS_INTERACTIVE=false
# POWEROFF_DURING_TEST=false
# VERSION=0.1
# TAGS=hdd
# DEPENDS=HDD,Disk Controller
# VAR=CONFIGURATION:string:optimal:Configuration to be passed to einarc. Can be "clear", "optimal" or "passthrough" to run corresponding raid-wizard utility. Otherwise it will be command line string to einarc. If there are only identical hard drives, then EINARC_DISK1, EINARC_DISK2, etc words can be used to prevent absolute hard drive's identification using
# VAR=ADAPTER:string::Default adapter type to work with. Leave it empty if there only single or several identical adapters present
# VAR=ADAPTER_NUMBER:int:0:Default adapter number (-a option for einarc) to work with
# VAR=DISK_GROUP_SIZE:int:8:Number of disks per group for testing (for passthrough configuration)
# VAR=DISK_GROUP_NUMBER:int:1:Number of disk group (for passthrough configuration)

. _inq-config-global; . $SHARE_DIR/functions-test

# Get default adapter and create einarc_command to simplify script reading
[ ! -z "$ADAPTER" ] || ADAPTER=`einarc --list | sed -n '1p' | awk '{print $1}'`
einarc_command="einarc -t $ADAPTER -a $ADAPTER_NUMBER"

# Replace all EINARC_DISK1/2/3/etc with real ones
drives_quantity=`$einarc_command physical list | wc -l`
for i in `seq 1 $drives_quantity`; do
	disk=`$einarc_command physical list | sort | awk '{print $1}' | sed -n "${i}p"`
	CONFIGURATION=`echo $CONFIGURATION | sed "s/EINARC_DISK${i}/${disk}/g"`
done

case $CONFIGURATION in
"optimal")
	raid-wizard-$CONFIGURATION || test_failed 'Wizard failed'
	;;
"clear")
	raid-wizard-$CONFIGURATION || test_failed 'Wizard failed'
	;;
"passthrough")
	PASSTHROUGH_ARGS="$DISK_GROUP_SIZE $DISK_GROUP_NUMBER"
	raid-wizard-$CONFIGURATION $PASSTHROUGH_ARGS || test_failed 'Wizard failed'
	;;
*)
	$einarc_command $CONFIGURATION || test_failed 'Einarc failed'
	;;
esac

# We have to use such dirty hack, because einarc's operations are not
# waiting for real commands completion. So, for example, we can get
# hard drives appearing for kernel (in dmesg) during several seconds.
sleep 30

test_succeeded
