#!/bin/bash

set -e
#set -x

if [ $(id -u) -ne 0 ]; then
	echo "$0 needs root: please use sudo."
	exit 1
fi

# Source Auxilium (args parser)
. /usr/share/auxilium/auxilium.sh

auxilium_init $(basename $0) -m 'Configure RAID 0 for a device'

auxilium_add did -t positional -m 'Disk ID '

if [ -f "/usr/share/ferreus-orbit/common.sh" ]; then
	. "/usr/share/ferreus-orbit/common.sh"
elif [ -f "share/common.sh" ] ; then
	. "share/common.sh"
else
	. "../share/common.sh"
fi

auxilium_parse "$@"

fo_lock

phy=$(cat ${FO_CACHE_FILE_STORCLI}| jq -r '.["PD LIST"][]|select(.DID=='${AUXILIUM_ARG_did}')["EID:Slt"]')

echo "Setting up RAID for device ${AUXILIUM_ARG_did} (EID:Slt=${phy})..."
readonly EID=$(echo ${phy}|cut -d':' -f1)
readonly Slt=$(echo ${phy}|cut -d':' -f2)
${STORCLI} /call/e${EID}/s${Slt} download src=${FW_DIR}/${FW} || true

es="/e$(echo ${phy}|awk -F':' '{print $1"/s"$2}')"
${STORCLI} /c0${es} set good force || true
${STORCLI} /c0 add vd r0 drives=${phy} direct wb ra ||true

