#!/bin/bash -pe
# Author: © 2010 Gert-jan Los <los@hacke.rs>
# License: GPL v3 or later (http://www.gnu.org/licenses/gpl-3.0.txt)

# for x in ffx fdx flx ;do ln fx "$x" ;done

shopt -s extglob

find=(find)

while [ $# -gt 0 ] && [[ "$1" != @(-*|!) ]] ;do
	find+=("$1")
	shift
done

find+=(-xdev)

case "${0##*/}" in
	ffx) find+=(-type f -a) ;;
	fdx) find+=(-depth -type d -a) ;;
	flx) find+=(-type l -a) ;;
	*) : ;;
esac

find+=('(' -true)

while [ $# -gt 0 ] ;do
	if [ "$1" = '--' ] ;then
		shift
		break
	else
		case "$1" in
			-n) find+=(-name)  ;;
			-i) find+=(-iname) ;;
			-p) find+=(-path)  ;;
			-s) find+=(-size)  ;;
			-l) find+=(-links) ;;
			*)  find+=("$1")   ;;
		esac
		shift
	fi
done

find+=(')' -print0)

if [ $# -ge 1 ] ;then
	exec "${find[@]}" |xargs -0r "$@"
elif [ -t 1 ] ;then
	exec "${find[@]}" |xargs -0r la -dF
else
	exec "${find[@]}" |xargs -0r ls -adF
fi
