#!/bin/bash

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


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


dns_part="hosts.raids"
show_all=1
show_name=0
show_level=0
show_raid_content=0
show_number_of_devices=0
show_number_of_spare_devices=0
show_size=0
show_chunk=0
show_layout=0
show_metadata_version=0




TEMP=$(getopt -o hs --long help,service-based-lookup,nameserver:,limit:,show-name,show-level,show-raid-content,show-number-of-devices,show-number-of-spare-devices,show-size,show-chunk,show-layout,show-metadata-version \
              -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.raids"
      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-level)
      show_level=1
      show_all=0
      shift 1
    ;;

    --show-raid-content)
      show_raid_content=1
      show_all=0
      shift 1
    ;;

    --show-number-of-devices)
      show_number_of_devices=1
      show_all=0
      shift 1
    ;;

    --show-number-of-spare-devices)
      show_number_of_spare_devices=1
      show_all=0
      shift 1
    ;;

    --show-size)
      show_size=1
      show_all=0
      shift 1
    ;;

    --show-chunk)
      show_chunk=1
      show_all=0
      shift 1
    ;;

    --show-layout)
      show_layout=1
      show_all=0
      shift 1
    ;;

    --show-metadata-version)
      show_metadata_version=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 raid-configs
what_to_lookup_for=$(echo $what_to_lookup_for | sed -r 's/-[0-9]+$//')

raid_names_to_print_details=""

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

if [ "$raid_names_to_print_details" != "" ]; then
  for raid_name_to_print_details in $raid_names_to_print_details
  do
    output=""

    raid_name=$(echo $raid_name_to_print_details | cut -d '.' -f 1)

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

    if [ $show_all -eq 1 ] || \
       [ $show_level -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "level" $raid_name_to_print_details.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_raid_content -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "raid-content" $raid_name_to_print_details.$dns_part) || continue
    fi

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

    if [ $show_all -eq 1 ] || \
       [ $show_number_of_spare_devices -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "number-of-spare-devices" $raid_name_to_print_details.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_size -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "size" $raid_name_to_print_details.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_chunk -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "chunk" $raid_name_to_print_details.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_layout -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "layout" $raid_name_to_print_details.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_metadata_version -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "metadata-version" $raid_name_to_print_details.$dns_part) || continue
    fi

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