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

# ferreus_orbit Plugins for GIGABYTE "MZ01-CE1-00"

fo_get_sorted_name(){
  host=$(echo "$DEVPATH" | cut -d/ -f6)
  num=$(echo "${ID_PATH}" | cut -d- -f4 | awk -F'phy' '{print $2}')
  case "$host" in
    ata1|ata2)
      ata=$(echo "${host}" |awk -F'ata' '{print $2}')
      id=$((ata - 1))
      num=$id
      ;;
    host0)
      draw=0
      id=$((num + 2))
      ;;
    host17)
      draw=1
      id=$((num + 10))
      ;;
    host18)
      draw=2
      id=$((num + 18))
      ;;
    *)
        logger -t "udev" "Device $DEVPATH detected but not supported"; exit 1
      ;;
  esac

  if [ "$USE_FO_NAME" -eq 1 ] || [ "$1" = "-f" ]; then
    if [ -z "${draw}" ]; then
      echo "Disk${num}"
    else
      echo "Drawer${draw}-Disk${num}"
    fi
  else
    echo "sd$(fo_get_dev_letter "${id}")"
  fi
}

fo_get_did(){
  fo_get_id "$1"
}

fo_get_id(){
  host=$(echo "$DEVPATH" | cut -d/ -f6)
  num=$(echo "${ID_PATH}" | cut -d- -f4 | awk -F'phy' '{print $2}')
  case "$host" in
    ata1|ata2)
      ata=$(echo "${host}" |awk -F'ata' '{print $2}')
      id=$((ata - 1))
      ;;
    host0)
      id=$((num + 2))
      ;;
    host17)
      id=$((num + 10))
      ;;
    host18)
      id=$((num + 18))
      ;;
    *)
      logger -t "udev" "Device $DEVPATH detected but not supported"; exit 1
      ;;
  esac
  echo "$id"
}

fo_get_kname(){
  id=$((2 + $2 * 8 +$1 ))
  echo "sd$(fo_get_dev_letter "${id}")"
}

fo_write_by_fo_name(){
  fo_load_storcli -f
  name=$(basename "$1")
  if [ "${#name}" -le 4 ]; then
    id=$(fo_get_dev_ascii "$name")
    ctl=$(((id - 2) / 8))
    slt=$((id - (ctl * 8) - 2))
  else
    ctl=$(echo "$name"|sed -e 's/[^0-9]/ /g'|awk '{print $1}')
    slt=$(echo "$name"|sed -e 's/[^0-9]/ /g'|awk '{print $2}')
  fi
  fo_raid_path=$(jq -r ".|select(.!=null)[]
                            |to_entries[]|
                              select(.key|tostring|endswith(\"/Settings\"))
                                |select(.key|contains(\"/c${ctl}/\"))|
                                  select(.value.\"Connected Port Number\"==\"${slt} \").key" \
                  "/var/cache/ferreus-orbit/fo_cache_storcli" 2>/dev/null)
  fo_raid_path=$( echo "$fo_raid_path" | cut -d' ' -f2)
  fo_drive=$(mktemp)
  fo_jb=$(mktemp)
  jq -r ".|to_entries[]|select(.key==\"Drive ${fo_raid_path}\").value[]" /var/cache/ferreus-orbit/fo_cache_storcli > "$fo_drive"
	jq -r '.|.Type+","+."EID:Slt"+","+(.DID|tostring)+","+.Model+","+.DG+","+.State+","+.Size' "$fo_drive" > "$fo_jb"
  IFS=, read -r type eidslt did model dg state size < "$fo_jb"
  rm "$fo_drive" "$fo_jb"
  fo_drive_detailed="$(mktemp)"
  jq -r ".|to_entries[]|select(.key==\"Drive ${fo_raid_path} - Detailed Information\").value.\"Drive ${fo_raid_path} Device attributes\"|.\"Manufacturer Id\"+\",\"+.SN+\",\"+.\"Firmware Revision\"" "${FO_CACHE_FILE_STORCLI}" > $fo_drive_detailed
  IFS=, read -r vendor sn fw < "$fo_drive_detailed"
  rm "$fo_drive_detailed"
  descr="JBOD: ${state}"
	printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" \
		"$slt" \
		"" \
		"$1" \
		"$name" \
		"" \
		"" \
		"$vendor" \
		"$model" \
		"$sn" \
		"$fw" \
		"$size" \
		"$descr" >> "$FO_CACHE_DISKS_INFOS"
	unset did  name vendor model sn fw size descr
}

FO_NO_USE_STORCLI=1

FO_DRAWER_DISPOSITION="    /{Drawer{DRAWER}-Disk2}/{Drawer{DRAWER}-Disk3}|{Drawer{DRAWER}-Disk1}@{Drawer{DRAWER}-Disk0}@\
{new_line}   /{SN_Drawer{DRAWER}-Disk2}/{SN_Drawer{DRAWER}-Disk3} | {SN_Drawer{DRAWER}-Disk1}@{SN_Drawer{DRAWER}-Disk0}@\
{new_line}  --{MINUS}---{MINUS}-------{MINUS}---{MINUS}--\
{new_line} /{Drawer{DRAWER}-Disk6}/{Drawer{DRAWER}-Disk7}   |   {Drawer{DRAWER}-Disk5}@{Drawer{DRAWER}-Disk4}@\
{new_line}/{SN_Drawer{DRAWER}-Disk6}/{SN_Drawer{DRAWER}-Disk7}    |    {SN_Drawer{DRAWER}-Disk5}@{SN_Drawer{DRAWER}-Disk4}@\
{new_line}__{UNDERSCORE}___{UNDERSCORE} Drawer {DRAWER} __{UNDERSCORE}___{UNDERSCORE}__"

FO_DISPOSITION="""
HDD disposition (Front Panel)
    __{UNDERSCORE}___{UNDERSCORE}___{UNDERSCORE}___{UNDERSCORE}__
{DRAWER0}
{DRAWER1}
{DRAWER2}

HDD disposition (Rear Panel):
__{UNDERSCORE}___{UNDERSCORE}__
|{Disk0}|{Disk1}|
|{SN_Disk0}|{SN_Disk1}|
¯¯{HIGHER}¯¯¯{HIGHER}¯¯
"""

  for d in 0 1 2; do
    _fo_drawer=${FO_DRAWER_DISPOSITION}
    _fo_drawer=$(printf "${_fo_drawer}"|sed "s/{DRAWER}/${d}/g")
    FO_DISPOSITION=$(printf "${FO_DISPOSITION}"|sed "s%{DRAWER${d}}%${_fo_drawer}%g")
  done
