# 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)
  case "$host" in
    ata1|ata2)
      ata=$(echo "${host}" |awk -F'ata' '{print $2}')
      id=$((ata - 1))
      num=$id
      ;;
    *)
      draw=$(echo "${ID_PATH}" | cut -d- -f2 | cut -d: -f2)
      num=$(echo "${ID_PATH}" | cut -d- -f4 | awk -F'phy' '{print $2}')
      case "$draw" in
        81)
          draw=0
          id=$((num + 2))
          ;;
        41)
          draw=1
          id=$((num + 10))
          ;;
        01)
          draw=2
          id=$((num + 18))
         ;;
        *)
           logger -t "udev" "Device $DEVPATH detected but not supported"; exit 1
          ;;
      esac
      ;;
  esac
    
  if [ "$USE_FO_NAME" -lt 1 ]; then
    echo "sd$(fo_get_dev_letter "${id}")"
    exit 0
  else
    if [ -z "${draw}" ]; then
      echo "Disk${num}"
    else
      echo "Drawer${draw}-Disk${num}"
    fi
    exit 0
  fi
}

fo_get_did(){
  fo_get_id "$1"
}

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

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
