#!/bin/bash
# info: search domain owner
# options: DOMAIN [TYPE]
#
# example: v-search-domain-owner acme.com
#
# This function that allows to find user objects.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
domain=$1
type=${2-any}

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '1' "$#" 'DOMAIN [TYPE]'

is_format_valid 'domain'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Define conf
case $type in
	web) conf="$HESTIA/data/users/*/web.conf" ;;
	dns) conf="$HESTIA/data/users/*/dns.conf" ;;
	mail) conf="$HESTIA/data/users/*/mail.conf" ;;
	*) conf="$HESTIA/data/users/*/*.conf" ;;
esac

owner=$(grep -H "DOMAIN='$domain'" $conf | head -n 1 | cut -f7 -d '/')
if [ -z "$owner" ]; then
	exit "$E_NOTEXIST"
fi

echo "$owner"

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Logging
#log_event "$OK" "$ARGUMENTS"

exit