#!/bin/bash

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


##########################
function ymc_script_help
{
  echo ""
  echo "Get mount-information in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS] hostname|service"
  echo "====="
  echo ""
  echo "OPTIONS:"
  echo "========"
  echo "-s|--service-based-lookup"
  echo "  Get mounts for services. If not given, it is assumed to get"
  echo "  host-based mounts-information."
  echo ""
  echo "--show-[name|device|mountpoint|filesystem|options|dump|pass]"
  echo "  Only output a specific type of information. If not given, all available"
  echo "  will be shown."
  echo ""
  echo "--limit=MOUNT_NAME"
  echo "  Limit to a specific mount 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.mounts"
show_all=1
show_name=0
show_device=0
show_mountpoint=0
show_filesystem=0
show_options=0
show_dump=0
show_pass=0

TEMP=$(getopt -o hs --long help,service-based-lookup,nameserver:,limit:,show-name,show-device,show-mountpoint,show-filesystem,show-options,show-dump,show-pass \
              -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.mounts"
      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-device)
      show_device=1
      show_all=0
      shift 1
    ;;

    --show-mountpoint)
      show_mountpoint=1
      show_all=0
      shift 1
    ;;

    --show-filesystem)
      show_filesystem=1
      show_all=0
      shift 1
    ;;

    --show-options)
      show_options=1
      show_all=0
      shift 1
    ;;

    --show-dump)
      show_dump=1
      show_all=0
      shift 1
    ;;

    --show-pass)
      show_pass=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 mount-configs
what_to_lookup_for=$(echo $what_to_lookup_for | sed -r 's/-[0-9]+$//')

mounts_to_print_details=""

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

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

    mount_name=$(echo $mount_to_print_details | cut -d '.' -f 1)

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

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

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

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

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

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

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

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