#!/bin/bash

# shellcheck shell=sh
# shellcheck disable=SC2034 
# shellcheck disable=SC2059
# shellcheck disable=SC1091
# shellcheck disable=SC1090

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 'Reconfigure an HDD after replacing it'

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

if [ -z "$AUXILIUM_ARG_sh_completion" ]; then
	fo_init
fi

# shellcheck disable=SC2154
# phy=$(jq -r '.["PD LIST"][]|select(.DID=='"${AUXILIUM_ARG_did}"')["EID:Slt"]' "${FO_CACHE_FILE_STORCLI}")
# es="/e$(echo "${phy}"|awk -F':' '{print $1"/s"$2}')"
# shellcheck disable=SC2154
${STORCLI} "/call/vall" delete preservedCache
for phy in $(${STORCLI} /call show | grep " UGood " |cut -d' ' -f1); do
	echo "===> Create RAID0 for device ${phy}..."
	e=$(echo "$phy"| cut -d: -f1)
	s=$(echo "$phy"| cut -d: -f2)
	${STORCLI} "call/e${e}/s${s}" set good force  2>/dev/null || true
	${STORCLI} "/call" add vd r0 drives="${phy}" direct wb ra  2>/dev/null
done