#!/bin/bash

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


##########################
function ymc_script_help
{
  echo "" 1>&2
  echo "Get logcial-volume information in Fliwi" 1>&2
  echo "" 1>&2
  echo "Usage: $(basename $0) [OPTIONS] hostname|service" 1>&2
  echo "=====" 1>&2
  echo "" 1>&2
  echo "OPTIONS:" 1>&2
  echo "========" 1>&2
  echo "--host-based-lookup" 1>&2
  echo "  Get logical-volume for a hosts." 1>&2
  echo "  Mutually exclusive with any other option '--[*]-based-lookup'" 1>&2
  echo "" 1>&2
  echo "-s|--service-based-lookup" 1>&2
  echo "  Get logical-volume for a service." 1>&2
  echo "  Mutually exclusive with any other option '--[*]-based-lookup'" 1>&2
  echo "" 1>&2
  echo "--vhost-based-lookup" 1>&2
  echo "  Get logical-volume used for vServers on a vHost." 1>&2
  echo "  Mutually exclusive with any other option '--[*]-based-lookup'" 1>&2
  echo "" 1>&2
  echo "--show-[config-name|volume-name|volume-group|device-path|size|filesystem]" 1>&2
  echo "  Only output a specific type of information. If not given, all available" 1>&2
  echo "  will be shown." 1>&2
  echo "" 1>&2
  echo "--limit=CONFIG_NAME" 1>&2
  echo "  Limit to a specific volume of a service or host." 1>&2
  echo "" 1>&2
  echo "" 1>&2
  echo "DEBUGGING OPTIONS:" 1>&2
  echo "==================" 1>&2
  echo "NOTE: The following are most likely only useful for debugging purposes." 1>&2
  echo "      Try not to use them on any productive environment!" 1>&2
  echo "" 1>&2
  echo "--nameserver=<someIP>" 1>&2
  echo "  Overrides the nameserver used in automatic detection of settings." 1>&2
  echo "  Default is to use the systems default nameserver." 1>&2
  echo "" 1>&2
}

lvm_group_prefix="fliwi_lvm_"
lvm_volume_host_prefix="host_"
lvm_volume_service_prefix="service_"
lvm_volume_vhost_prefix="vserver_"

dns_part="hosts.logical-volumes"
lvm_volume_prefix=$lvm_volume_host_prefix
show_all=1
show_config_name=0
show_volume_name=0
show_volume_group=0
show_device_path=0
show_size=0
show_filesystem=0

host_based_lookup=0
service_based_lookup=0
vhost_based_lookup=0

TEMP=$(getopt -o hs --long help,host-based-lookup,service-based-lookup,vhost-based-lookup,nameserver:,limit:,show-config-name,show-volume-name,show-volume-group,show-device-path,show-size,show-filesystem \
              -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
    ;;

    --host-based-lookup)
      host_based_lookup=1
      shift 1
    ;;

    -s|--service-based-lookup)
      dns_part="services.logical-volumes"
      lvm_volume_prefix=$lvm_volume_service_prefix
      service_based_lookup=1
      shift 1
    ;;

    --vhost-based-lookup)
      dns_part="vhosts.logical-volumes"
      lvm_volume_prefix=$lvm_volume_vhost_prefix
      vhost_based_lookup=1
      shift 1
    ;;

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

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

    --show-config-name)
      show_config_name=1
      show_all=0
      shift 1
    ;;

    --show-volume-name)
      show_volume_name=1
      show_all=0
      shift 1
    ;;

    --show-volume-group)
      show_volume_group=1
      show_all=0
      shift 1
    ;;

  --show-device-path)
      show_device_path=1
      show_all=0
      shift 1
    ;;

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

    --show-filesystem)
      show_filesystem=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

what_based_lookup_sum=$(expr $host_based_lookup + $service_based_lookup + $vhost_based_lookup)
if [ $? -gt 1 ]; then
   what_based_lookup_sum=-1
fi

if [ $what_based_lookup_sum -lt 0 ]; then
  ymc_error_msg "Can not determine which lookup method to use..."
  exit 1
elif [ $what_based_lookup_sum -eq 0 ]; then
  host_based_lookup=1
  ymc_warning_msg "Not specifying a '--[*]-based-lookup' option is deprecated.\n# Assuming '--host-based-lookup' for now..."
elif [ $what_based_lookup_sum -ne 1 ]; then
  ymc_script_help
  exit 1
fi

if [ $service_based_lookup -eq 1 ]; then
  ### We currently do not support per service-node lvm-configs
  what_to_lookup_for=$(echo $what_to_lookup_for | sed -r 's/-[0-9]+$//')
fi

volumes_to_print_details=""

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

if [ "$volumes_to_print_details" != "" ]; then
  for volume_to_print_details in $volumes_to_print_details
  do
    output=""
    ymc_get_config_from_dns $volume_to_print_details"."$dns_part
    if [ $? -ne 0 ]; then
      continue
    fi

    config_name=$(echo $volume_to_print_details | cut -d '.' -f 1)

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

    if [ $show_all -eq 1 ] || \
       [ $show_volume_name -eq 1 ]; then
      output=$output' '$lvm_volume_prefix$(ymc_var_value $config_var_prefix'_'volume_name)
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_volume_group -eq 1 ]; then
      output=$output' '$lvm_group_prefix$(ymc_var_value $config_var_prefix'_'volume_group)
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_device_path -eq 1 ]; then
      output=$output" /dev/$lvm_group_prefix""$(ymc_var_value $config_var_prefix'_'volume_group)/$lvm_volume_prefix""$(ymc_var_value $config_var_prefix'_'volume_name)"
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_size -eq 1 ]; then
      output=$output' '$(ymc_var_value $config_var_prefix'_'size_string)
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_filesystem -eq 1 ]; then
      output=$output' '$(ymc_var_value $config_var_prefix'_'filesystem)
    fi

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