#!/bin/bash

### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash


##########################
function ymc_script_help
{
  echo ""
  echo "Get drive-group-information in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS] hostname|service"
  echo "====="
  echo ""
  echo "OPTIONS:"
  echo "========"
  echo "-s|--service-based-lookup"
  echo "  Get drive-groups for services. If not given, it is assumed to get"
  echo "  host-based drive-group-information."
  echo ""
  echo "--show-[name|devices|sizes|partitions]"
  echo "  Only output a specific type of information. If not given, all available"
  echo "  will be shown."
  echo ""
  echo "--limit=DISK_GROUP_NAME"
  echo "  Limit to a specific drive-group of a service or host."
  echo ""
  echo ""
  echo "DEBUGGING OPTIONS:"
  echo "=================="
  echo "NOTE: The following are most likely only useful for debugging purposes."
  echo "      Try not to use them on any productive environment!"
  echo ""
  echo "--nameserver=<someIP>"
  echo "  Overrides the nameserver used in automatic detection of settings."
  echo "  Default is to use the systems default nameserver."
  echo ""
}


dns_part="hosts.drive-groups"
show_all=1
show_name=0
show_devices=0
show_partitions=0
show_sizes=0

TEMP=$(getopt -o hs --long help,service-based-lookup,nameserver:,limit:,show-name,show-devices,show-sizes,show-partitions \
              -n "$(basename $0)" -q -- "$@")
if [ $? != 0 ]; then
  ymc_script_help
  exit 1
fi

eval set -- "$TEMP"

while true
do
  case "$1" in
    -h|--help)
      ymc_script_help
      exit 1
    ;;

    -s|--service-based-lookup)
      dns_part="services.drive-groups"
      shift 1
    ;;

    --nameserver)
      DEBUG_DNS=$2
      shift 2
    ;;

    --limit)
      limit_string=$2
      shift 2
    ;;

    --show-name)
      show_name=1
      show_all=0
      shift 1
    ;;

    --show-devices)
      show_devices=1
      show_all=0
      shift 1
    ;;

    --show-sizes)
      show_sizes=1
      show_all=0
      shift 1
    ;;

    --show-partitions)
      show_partitions=1
      show_all=0
      shift 1
    ;;

    --)
      shift
      break
    ;;

    *)
      ymc_script_help
      exit 1
    ;;
  esac
done

what_to_lookup_for=$*
if [ "$what_to_lookup_for" == "" ] || \
   [ $(echo $what_to_lookup_for | wc -w) -ne 1 ]; then
  ymc_script_help
  exit 1
fi

### We currently do not support per service-node drive-group-configs
what_to_lookup_for=$(echo $what_to_lookup_for | sed -r 's/-[0-9]+$//')

disk_group_names_to_print_details=""

found_disk_group_names=$(ymc_get_hostname_from_dns_txt "$what_to_lookup_for" "$dns_part")
if [ $? -eq 0 ] && \
   [ "$found_disk_group_names" != '' ]; then
  for disk_group_name in $found_disk_group_names
  do
    if [ "$disk_group_name" != '' ]; then
      if [ "$limit_string" != '' ]; then
        if [ "$limit_string" != "$disk_group_name" ]; then
          continue
        fi
      fi
      disk_group_names_to_print_details=$disk_group_names_to_print_details' '$disk_group_name'.'$what_to_lookup_for
    fi
  done
else
  exit 1
fi

if [ "$disk_group_names_to_print_details" != "" ]; then
  for disk_group_name_to_print_details in $disk_group_names_to_print_details
  do
    output=""

    disk_group_name=$(echo $disk_group_name_to_print_details | cut -d '.' -f 1)

    if [ $show_all -eq 1 ] || \
       [ $show_name -eq 1 ]; then
      output=$output' '$disk_group_name
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_devices -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "devices" $disk_group_name_to_print_details.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_sizes -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "sizes" $disk_group_name_to_print_details.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_partitions -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "partitions" $disk_group_name_to_print_details.$dns_part) || continue
    fi

    echo $(echo -n $output | sed -r 's/[[:space:]]+/ /')
  done
  exit 0
else
  exit 1
fi
