#!/bin/bash
# Need bash because we use nullglob
#
# list-records
#
# Print, one per line, names of files in the current directory
# that match the shell glob patterns in /etc/resolvconf/interface-order

set -e

# Stores arguments (minus duplicates) in RSLT, separated by newlines
# Doesn't work properly if an argument itself contain whitespace
uniquify()
{
	RSLT=""
	while [ "$1" ] ; do
		for E in $RSLT ; do
			[ "$1" = "$E" ] && { shift ; continue 2 ; }
		done
		RSLT="${RSLT:+$RSLT
}$1"
		shift
	done
}

### Compile ordered list of resolv.conf-type files ###
PATTERNS="eth* ath* wlan* ppp* *"
if [ -r /etc/resolvconf/interface-order ] ; then
	PATTERNS="$(sed \
		-e 's/^[[:space:]]\+//' \
		-e 's/[[:space:]].*//' \
		-e 's/#.*//' \
		-e 's%/.*%%' \
		-e 's/^~.*//' \
		-e 's/^\..*//' \
		-e '/^$/d' \
		/etc/resolvconf/interface-order)"
fi
shopt -s nullglob
uniquify $PATTERNS
RCRDS=""
for FLNM in $RSLT ; do
	[ -s "$FLNM" ] && RCRDS="${RCRDS}${FLNM}
"
done
echo -n "$RCRDS"
exit 0
