#!/usr/bin/env bash

help() {
  echo "USAGE: makeraid [level] [device] [device] ... [ [--spares] [device] ... ]"
}

[[ :0:1:4:5:6:10: == *:${1}:* ]] && RL=${1} || { help; exit 1; }
shift

until [ "${1}" = "--spares" ]; do
  [ -b "${1}" ] && RD+=("${1}") || { help; exit 2; }
  shift
  [ ${#} = 0 ] && break 
done

if [ "${1}" == "--spares" ]; then
  shift
  for DV in ${@}; do
    SD+=("${DV}")
  done
fi

[ "${#SD[@]}" -gt "0" ] && SO=( "-x" "${#SD[@]}" )

for MD in {0..127}; do
  [ ! -b "/dev/md${MD}" ] && ND=${MD} && break
done

mdadm -Cve 1.2 /dev/md${ND} -l "${RL}" -c 32 -n ${#RD[@]} ${RD[@]} ${SO[@]} ${SD[@]}