#!/bin/sh

. /usr/lib/lib-fliwi/ymc-common.sh

fliwi_get_drives_with_partition_tables()
{
  ls /sys/class/block/*/*/partition 2>/dev/null | cut -d '/' -f 5 | sort | uniq
  return 0
}

fliwi_get_harddrives()
{
  return_code=1

  min_clean=$(cat /proc/partitions 2>/dev/null | sed -r 's/^[0-9 ]+ //' | grep -v -E -e '(major minor)|(^[ ]*$|^dm-[0-9]+)|(^md[0-9]+)')
  block_devices=$(ls /sys/block/ | sed -r 's|cciss!c|cciss/c|g')
  for possible_disk in $min_clean
  do
    if [ $(ymc_contains $possible_disk $block_devices) -eq 1 ]; then
      return_code=0
      echo $possible_disk
    fi
  done
  return $return_code
}

fliwi_get_disk_size()
{
  drive=$1
  disk_info=$(fdisk -l /dev/$drive 2>/dev/null | grep -E -e '^Disk /dev/.*: .*, .*bytes$')
  disk_size=$(echo $disk_info | grep -E -e '^Disk /dev/.*: .*, .*bytes$' | sed -r 's/.*, ([0-9]+) bytes$/\1/' )
  if [ "$disk_size" != "$disk_info" ]; then
    echo $disk_size
    return 0
  else
    return 1
  fi
}

fliwi_get_non_block_device_disks()
{
  lsscsi -g \
   | grep -E -e '\][[:space:]]+disk[[:space:]]+.+[[:space:]]+-[[:space:]]+/dev/sg[0-9]+[[:space:]]*$' \
   | sed -r 's|.*(/dev/sg[0-9]+)[[:space:]]*|\1|' 2>/dev/null
}

