#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename "$0")
CMDVER="1.2"
CMDSTR="$CMD v$CMDVER (2020/06)"


usage()
{
	echo "
usage:
  $CMD [options] name		   	(get full user dbname -- aka 'id')
  $CMD [options] name field(s)	   	(get fields values)
  $CMD [options] name [kusa-conf options]

options:
  -v|--verbose		be verbose
  -q|--quiet		be quiet
  -h|--help		show help
  -D[n]|--debug[=n]	set debug, optionally with level
  -a|--all)		all users instead of single one
  -d char		set delimiter (mandatory if fields num > 1)
  --			stop processing options

* name can '(all)', works the same of using '-a' option
* name can be a list, comma separated
* kusa-conf options can be appended AFTER users and field lists

WHEN USED WITH FIELD(s) LIST:

those pseudo-fields can be used in field(s) list

- id		the full kusa-db section
- username	the last part of full kusa-db section

" >&2
	[ $# != 0 ] && echo -e "\n$@\n" >&2
	exit 127
}


cleanup()
{
	trap "" 1 2 3 ERR EXIT
	:
	trap 1 2 3 ERR EXIT
}



show_help()
{
	echo -e "
== $CMDSTR - searches kusa-db users and get/dump values
"
	return 0
}

pdebug()
{
	$DEBUG || return 0
	echo -n "D# " >&2
	echo -e "$@" >&2
}


get_conf()
{
	local sect=$1
	local fld=$2
	shift 2

	case $fld in
	  username)	echo ${sect/*./} ;;
	  id)		echo $sect ;;
	  *)
		if $abort_on_err
		then
			kusa-conf "$@" $sect $fld
		else
			kusa-conf "$@" $sect $fld 2>/dev/null || :
		fi
	esac
	return 0
}





# (MAIN)

trap 'echo -e "\n*INTR*\n"; exit 255' 1 2 3
trap 'echo -e "\nunexpected error $? at $LINENO\n"' ERR
trap 'cleanup' EXIT
set -e
set -u

VERBOSE=${VERBOSE:-true}
DEBUG=${DEBUG:-false}
DBGLEVEL=${DBGLEVEL:-0}

users=
fields=
delim=
abort_on_err=true

while [ $# != 0 ]
do
  case $1 in
    -h|--help)		show_help; exit 0 ;;
    -v|--verbose)	VERBOSE=true ;;
    -q|--quiet)		VERBOSE=false ;;
    -D|--debug)		DEBUG=true ;;
    -D[0-9]|--debug=[0-9])
    			DEBUG=true; DBGLEVEL=$(echo "X$1" | sed -e 's/^X-D//' -e 's/^X--debug=//')
			;;
    -a|--all|'(all)')	users="(all)"; abort_on_err=false ;;
    -d|--delimiter)
    	shift
	[ $# = 0 ] && usage
	delim=$(echo -e "$1")
	;;
    -*) [ "X$users" = "X" ] && usage
    	break
	;;
    *)	if [ "X$users" = "X" ]
	then
		users="$1"
	else
		fields="$fields $1"
	fi
	;;
  esac
  shift
done

[ "X$users" = "X" ] && usage "must pass an user(s) list or -a flag"

echo "$fields" | sed -e 's/^ //' | grep -q ' ' && {
	[ "X$delim" = "X" ] && usage \
		"you must set a delimiter (-d) when requesting multiple fields"
}
case $users in
 '(all)'|*,*)
	[ "X$delim" = "X" ] && usage \
		"you must set a delimiter (-d) when requesting multiple users"
 	users=$(echo "$users" | tr ',' ' ')
	;;
esac

# search user entries
#
user_sections=

if [ "X$users" = "X(all)" ]
then
	user_sections=$(kusa-conf --list user.)
else
	for section in $(kusa-conf --list user.)
	do
		for usr in $users
		do
			pdebug "eval $section against $usr"
			case $section in
		  	  *.$usr) user_sections="$user_sections $section" ;;
			esac
		done
	done
fi

pdebug "found sections:" $user_sections


cleaned_sections=
for section in $user_sections
do
	# ignore incomplete (no uuid) entries
	kusa-conf $section uid >/dev/null 2>&1 || continue
	cleaned_sections="$cleaned_sections $section"
done
pdebug "cleaned sections:" $cleaned_sections



if [ "X$fields" = "X" -a $# = 0 ]
then
	# no fields list and no parms, just output full section names
	#
	for section in $cleaned_sections
	do
		echo $section
	done
else
	# fields or parms, pass them to kusa-conf
	#
	if [ "X$delim" != "X" ]
	then
		# field list, must iterate because kusa-conf accepts
		# only one field at once; first field is alwas full
		# section (user) name
		#
		tmp_delim=
		for section in $cleaned_sections
		do
			out=
			for fld in $fields
			do
				out="$out$tmp_delim"$(get_conf $section $fld "$@")
				tmp_delim="$delim"
			done
			echo "$out"
		done
	else
		# single field
		#
		for section in $cleaned_sections
		do
			get_conf $section $fields "$@"
		done
	fi
fi

exit 0
