#!/bin/bash

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


##########################
function ymc_script_help
{
  echo ""
  echo "Get system-users-information in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS] hostname|service"
  echo "====="
  echo ""
  echo "OPTIONS:"
  echo "========"
  echo "-s|--service-based-lookup"
  echo "  Get system-users for services. If not given, it is assumed to get"
  echo "  host-based system-users-information."
  echo ""
  echo "--show-[name|uid|shell|home]"
  echo "  Only output a specific type of information. If not given, all available"
  echo "  will be shown."
  echo ""
  echo "--limit=SYSTEM_USER_NAME"
  echo "  Limit to a specific system-user name 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.system-users"
show_all=1
show_name=0
show_uid=0
show_shell=0
show_home=0

TEMP=$(getopt -o hs --long help,service-based-lookup,nameserver:,limit:,show-name,show-uid,show-shell,show-home \
              -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.system-users"
      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-uid)
      show_uid=1
      show_all=0
      shift 1
    ;;

    --show-shell)
      show_shell=1
      show_all=0
      shift 1
    ;;

    --show-home)
      show_home=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 system-users-configs
what_to_lookup_for=$(echo $what_to_lookup_for | sed -r 's/-[0-9]+$//')

system_users_to_print_details=""

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

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

    system_user_name=$(echo $system_user_to_print_details | cut -d '.' -f 1)

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

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

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

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

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